1. LAB # 15 HANDOUT
Objectives:
Pointers:
Just like we can reference the cabinets of a cupboard with their order i.e. first cabinet or the second cabinet,
the memory in a computer can be referenced by address. These addresses can be viewed in HEX if we want.
By using pointers, we can use the addresses to manipulate the values of the variables.
Pointer are declared by doing int *pointer_name. Just like there can be different data types of variables, there
can different data types of pointers i.e. float pointer, double pointer, char pointer etc. Creating a pointer
reserves a slot in the memory that can store the address of another memory location.
Once we have created a pointer to store the address of another memory location, we now need to store the
address in it. This can be done by something we already know: using the &, we can save the address of a
variable in the memory slot that we have reserved earlier. The line of code that will accomplish this is: int
*my_ptr = &my_variable where my variable has been declared and initialized earlier.
In order to manipulate (or display) the data that a pointer points to can be accomplished by using the asterisk
sign with the pointer name i.e. cout<<*my_pointer. Similarly, the same syntax can be used to add or subtract
something that the pointer points to i.e. *my_ptr++. On the other hand, displaying the value of the pointer
itself will only display the value of the address i.e. cout<<my_pointer will display the address of the variable
that my_pointer holds.
Pointers can be passed to the functions and they can also be the return data type of a function. For example, I
can create a function with the prototype as: int * my_function (int *, float *). Whenever any pointer is passed,
it is the same as passing a value via variable.
The name of an array is a pointer to its first location.
Remember the values of the variables that we passed via reference? We can recreate that functionality using
pointers.
Practice Questions:
1. Create pointers for datatypes int, float, double, char. Then declare four variables of the datatypes that we
have declared the pointers for. Display the pointers and then the values that they are pointing to.
2. Write a function that takes an int pointer as an input and cubes it. The function shouldn’t return anything.
3. Create a variable of datatype float. Then check whether you can assign multiple pointers to this variable
or not. Is there any way to check it?
4. Write a function that takes two integer pointers as an input and switches the values that those two pointers
point to.
Recursion:
Up until now, we have used loops to do a task that needs to be done repeatedly. We can accomplish the same
thing using recursion. Whenever we intend to use recursion to solve a problem, we need to break down the
problem itself. In other words, we need divide the problems into sub problems and then we solve the original
problem by solving the sub parts.
2. Put simply, we write a solution that is capable of solving the simplest case of a problem. For any other case
of that problem, we simply call the function itself again until the simplest case is reached.
Some of the famous problems that can be solved with recursion are finding the factorial of a number (the
function is called again and again until we reach the base case), finding the Fibonacci Series (calling the
function again and again until the base case is met) etc.
Recursion can be used in place of loops but it may or may not result in a more efficient solution.
Practice Questions:
1. Write a function that computes the factorial of a number recursively.
2. Write a function that generates the Fibonacci series up to a given number.
3. Write a function that takes two arguments as an input and recursively computes the power of first number
to the second number i.e. function1 (a,b) where a = 2 and b =3.
4. Write a function that takes two arguments as input and multiplies them recursively.
5. Take an array as an input. Then write a recursive function that prints the array in reverse order.