CUS 291

A few review questions

 

1.)  Functions: Given:

 

                         float  sample1[10];

 

      Explain the difference(s) between:

 

            fun1(sample1);

 

            fun2(sample[2]);

 

 

 

2.)  Recursion: Write a recursive function that computes

 

                        S(n) = 2 + 4 + 6 + .... + 2n

 

            [ Hint: S(n) = S(n - 1) + 2n ]

 

 

 

 

 

 

 

3.)  Recursion: Write a nonrecursive function to compute the nth fibonacci number.

 

 

 

 

 

 

 

 

4.)  Arrays:  Do each of the following declarations allocate the same number of cells:

 

                        char   some_chars[1000];

 

                        int   some_ints[10][100];

 

 

5.)  Pointers:  What is the error in this slice of code:

 

                        char var1, ptr1;

                        var1 = ‘x’;

                        ptr1 = &var1; 

 

6.)  Pointers:  What is printed:

 

                        char  var1 = ‘S’;

                        char  var2 = ‘X’;

                        char  *ptr1, *ptr2;

                        ptr1 = &var1;

                        ptr2 = &var2;

                        *ptr2 = *ptr1;

                        cout << *ptr1 << “  “ << var2;

 

 

 

7.)  Pointers:  Given:               int  var1 = 2121;

                                    int *ptr;

                                    ptr = &var1;

 

                                    and that var1’s address is 99999.

 

            What is the value of  var1?

 

 

            What is the value of ptr?

 

 

            What is the value of *ptr?

 

 

8.)  Pointers:  What is printed?

 

                        int nums[5] = {11, 22, 33, 44, 55}

                        int *ptr = nums + 5;

                        int i;

                        for (i = 0; i < 5; ++i)

                                    cout <<  *ptr;

 

9.)  Pointers:  Given:

 

                        int  numbs[10];

                        int  *ptr = numbs;

 

            which of the following expressions are equivalent?

 

            a)  numbs[3]

            b)  numbs + 3

            c)  *(numbs + 3)

            d)  *(ptr + 3)

            e)  *ptr + 3