Lecture 20 Exercise

(1)
For the following program:
#include<iostream>
using namespace std;

int main() {
    int a[] = {1,2,3};
    int *pt;
    int x = a[0];

    return 0;
}
Which of the followings are pointers?
  1. a
  2. pt
  3. a[1]
  4. x
  5. a+1
  6. pt+2

(2)
For the following program, if the output of the line cout << a << endl; is 0012FEC4, what will be the output of the following program?
#include<iostream>
using namespace std;

int main() {
    int a[] = {2,4,6,8,10};
    int *pt = &a[0];
    cout << a << endl;
    cout << &a[0] << endl;
    cout << pt << endl;
    cout << *pt << endl;
    cout << *(pt+1) << endl;
    cout << *pt+1 << endl;
    cout << pt[1] << endl;
    cout << a[1] << endl;
    cout << *(a+1) << endl;
    cout << *(pt+2) << endl;
    cout << pt[2] << endl;
    cout << a[2] << endl;
    cout << *(a+2) << endl;

    return 0;
}

(3)
Rewrite the function sum in the following program so that no [ and ] are used in the source code. (You shouldn't change the main function).
#include<iostream>
using namespace std;

int sum(int a[], int size); // remove [ and ]

int main() {
    int a[] = {2,4,6,8,10};
    cout << sum(a,5) <<endl;
    return 0;
}

// remove [ and ]
int sum(int a[], int size) {
    int result=0;
    for(int i = 0; i < size; i++) {
        result+=a[i];
    }
    return result;
}

(4)

(a)
  1. For int a[] = {0,1,2,3,4}, do you agree that a is a pointer?
  2. What does a point to?
  3. What does a+1 point to?
  4. What does a+2 point to?
  5. Do you agree that a+i points to a[i]?
  6. Do you agree that a[i] is same as *(a+i)?
  7. If b=a+1, what is b[0], what is *b, what is *(b+1)?
(b) What is the output of the following program?
#include<iostream>
using namespace std;

void printArray(int a[], int size);

int main() {
    int a[] = {0,1,2,3,4};
    int* pt = a;
    printArray(a,5);
    printArray(pt,5);
    pt[2] = 7;
    a[3] = 9;
    printArray(a,5);
    printArray(pt,5);
    *pt = 3;
    *(a+4) = 0;
    printArray(a,5);
    printArray(pt,5);

    // tricky
    pt=a+2;
    printArray(pt,3);
    printArray(a+1,2);
}

// print elements of an array in a row
void printArray(int a[], int size) {
    for(int i=0; i < size; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

(c)
Explain why printArray(pt,5) and printArray(a,5) always have the same anser?

(5)
(a) Explain the meanings of (i) int *pt = new int and (ii) int *pt = new int[5]
(b) What is the output of the following program
#include<iostream>
using namespace std;


int main() {
    char *pt, ch1='a', ch2='b';
    pt=&ch1;
    cout << ch1 << "," << ch2 << "," << *pt << endl;
    pt=&ch2;
    *pt='c';
    cout << ch1 << "," << ch2 << "," << *pt << endl;
    pt=new char;
    *pt='d';
    cout << ch1 << "," << ch2 << "," << *pt << endl;
    ch2=*pt;
    *pt='e';
    cout << ch1 << "," << ch2 << "," << *pt << endl;

    pt = new char[3];
    *pt='a';
    *(pt+1)='b';
    *(pt+2)=ch2;
    cout << *pt << "," << *(pt+1) << "," << *(pt+2) << endl;

    return 0;
}


(6)
#include<iostream>
using namespace std;

void printArray(int a[], int size);

int main() {
    int a[] = {0,1,2,3,4,5,6};
    int b[] = {0,-1,-2,-3,-4,-5,-6};
    int *pt;
    pt = a;
    printArray(a,7);
    printArray(b,7);
    printArray(pt,7);
    cout << endl;

    *pt=3;
    a[2]=4;
    b[3]=9;
    printArray(a,7);
    printArray(b,7);
    printArray(pt,7);
    cout << endl;

    pt=b;
    *pt=1;
    a[4]=7;
    b[5]=6;
    printArray(a,7);
    printArray(b,7);
    printArray(pt,7);
    cout << endl;

    pt=pt+1;
    *(a+3)=2;
    *(b+5)=100;
    *(pt+3)=6;
    printArray(a,7);
    printArray(b,7);
    printArray(pt,3);
    cout << endl;

    pt=a;
    *(pt+2) = 200;
    pt=b;
    *(pt+2)=300;
    printArray(a,7);
    printArray(b,7);
    printArray(pt,7);
    cout << endl;

    pt = new int[7];
    for(int i=0; i < 7; i++) {
        pt[i] = i + 1;
    }
    *(pt+3) = 100;
    *(a+1) = *(pt+1);
    b[3] = pt[3];
    printArray(a,7);
    printArray(b,7);
    printArray(pt,7);
    cout << endl;

    return 0;
}

void printArray(int a[], int size) {
    for(int i=0; i < size; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}


(7)
(a) Explain how to use typedef ?
(b) What is the advantage of using typedef ?

(8)
(a) Find out the compiling error of the following program. Explain
#include<iostream>
using namespace std;


int main() {
    float a[5] = {1,2,3,4,3.7f};
    float *pt;
    pt = a; 
    cout << pt[1];
    a = pt;
    cout << pt[1];
    return 0;
}

(b) The following program has no compiling error and no runtime error but it is not quite right. What's wrong with the program?
#include<iostream>
using namespace std;


int main() {
    float a[5] = {1,2,3,4,3.7f};
    float *pt;
    pt = a + 2;
    cout << pt[5];
    return 0;
}
(c) The following program has no compiling error but it is not quite right. What's wrong with the program?
#include<iostream>
using namespace std;

int main() {
    float *pt;
    cout << pt;
    float *pt2 = new float;
    cout << pt2[3];

    return 0;
}

(d)Find out the compiling error and correct the program.
#include<iostream>
using namespace std;


int main() {
    int num;
    cin >> num;
    char array[num];

    for(int i=0; i < num; i++) {
        cin >> array[i];
    }

    return 0;
}
(e) What is wrong with the following code? Correct it.
int* doubler(int a[], int size) {
    int temp[size];

    for(int i = 0; i < size; i++) {
        temp[i] = 2*a[i];
    }

    return temp;
}
     
(9)
Write a function double* multiply(double array[], int size, int num);. The function returns a new array with the same size, the i-th element is the i-th element of array multiply by num.
When properly implemented, the output of the following program will be:
#include <iostream>
using namespace std;

double* multiply(double array[], int size, int num);

int main() {
    double a[] = {1.1, 2.2, 0.5, 0.5, 1.3};
    int size = 5;
    double* b = multiply(a,5,4);
    for(int i=0; i < size; i++) {
        cout << b[i] << " ";
    }
    cout << endl;
    b = multiply(a,5,3);
    for(int i=0; i < size; i++) {
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}

double* multiply(double array[], int size, int num) {
   // your code
}

      
Output
4.4 8.8 2 2 5.2
3.3 6.6 1.5 1.5 3.9

(10)
Write a function
int* positive(int array[], int size, int& positiveSize)
The function stores the number of positive numbers of array in positiveSize and returns a new array consists of all the positive numbers of array. When the function is properly implemented, the output of the following program will be
#include <iostream>
using namespace std;

int* positive(int array[], int size, int& poitiveSize);

int main() {
    int a[] = {1,-2,-3,4,5,6,0,-9,7,3};
    int size = 10;
    int positiveSize;
    int* b = positive(a,10,positiveSize);
    cout << "There are " << positiveSize << " positive numbers.\n";
    for(int i=0; i < positiveSize; i++) {
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}

int* positive(int array[], int size, int& positiveSize) {
    // your code
}
Output
There are 6 positive numbers.
1 4 5 6 7 3