Midterm review #1

advertisement

MECH 415 Review Questions #1

Question #1

Briefly explain the operation of the program below. What is the output of the program ? double *p, x[4]={1.1, 2.2, 3.3, 4.4}; p = x – 2; cout << “\n” << x[2]; cout << “\n” << p[3]; cout << “\n” << *(p+4);

Question #2

Briefly explain the operation of the program below. What is the output of the program ? double **p; double x[4]={1.0, 2.0, 3.0, 4.0}; double y[4]={1.1, 2.1, 3.1, 4.1}; double z[4]={1.2, 2.2, 3.2, 4.2}; p = new double * [10] + 1; p[-1] = x + 1; p[0] = y - 1; p[1] = z + 2; cout << p[-1][2] << “\n” << p[0][3] << “\n“ << p[1][1]; delete (p – 1);

Question #3

Complete the function below so that it returns the magnitude of the dVector associated with x. double magnitude(dVector *x)

{

}

Question #4

Consider the program below: int NQ = 4; dVector *Q, MagQ(1,NQ);

Q = new dVector [NQ] - 1; magnitude(Q,MagQ); delete [NQ] (Q+1);

Write the function (including the prototype) magnitude(Q,MagQ) that calculates the magnitude

(stored in MagQ) of each dVector stored in Q.

Question #5

Indicate which labeled statements are correct or incorrect. If it is incorrect briefly explain why. double x1=3.14, x2=7.7, *p1, *p2; p1 = &x1; p2 = &x2; dVector v1(1,10), v2(1,10), *p3, *p4; p3 = &v1; p4 = &v2; a) dVector v3; b) x1 = *p1; c) v1.e = v2.e; d) v1.a = v2.a; e) v1 = v2; f) x2 = *(&x1+1); g) &x1 = p2; h) p1 = &x2 + x1; i) p3 = p4; j) p3->a = (*p4).a; k) p1 = (*p4).a = p4->a; l) *p3 = *p4;

Question #6

Write a class called String tha t contains a dynamic 1D character array just long enough (it can’t be longer) to hold a string supplied to the constructor (including the NULL character). The public member variables are given by: char *Str; // pointer to the string character array int N; // length of the string not including the NULL

The public member functions include:

String(char *str); // constructor that takes a NULL terminated string

~String(); // destructor

Download