MECH 415 Assignment #7
Question #1
What is the output of the following program (hint: you can use the identity *(p+i) = p[i] to simplify some of the expressions, e.g. *(p+2) = *((A-1)+2) = *(A+1) = A[1]):
int A[] = {1,2,3,5,6,7}, *p, *q, *x, y=1;
char str[] = “orange”, *z;
p = A-2;
q = A+2;
x = &y;
*x = --(A[3]);
z = str;
cout < *z < z+3 < “\n”; // hint a string is an array of characters and an array is a pointer to the first element of the array
cout < y < *x < sizeof(int) < “\n”;
cout < (int)p-(int)q < “\n”;
cout < p[3] < “\n”;
cout < *(p+5) < “\n”;
cout < *(q+2) < “\n”;
cout < *(p+1);
Note: If you cut and paste this into the compiler you will need to fix the screwed up quotation marks.
If you need more practice with this type of problem try changing the integer constants in the problem and see if you can predict the output.
Question #2
Develop a dMatrix class with the following public member variables:
double **A; // a pointer-pointer used to represent a dynamic 2D array
int N; // the number of rows
int M; // the number of columns
It has the following public member functions:
A constructor that takes the arguments N, M and sets the elements A[i][j] = i+j.
A destructor.
print() - A function that prints out the elements of A with each row on a new line.
save(file_name) – A function that saves the member variables into a binary file, where the file name is stored in the string variable file_name. The format of the file is N and M followed by the elements of A (row by row). Do not store N and M as doubles in the binary file (i.e. use integers).
load(file_name) – Loads the type of binary file produced by save.
Write a main function that illustrates the usage of dMatrix.