
A) C++ ARRAYS
1)What gets printed? #include <iostream.h> void fn( int q[], int c[] ); int main ( void ) { int i, qa[]= {1, 1, 2, 2}, count[3] ={4, 4, 4}; fn ( qa, count ); for ( i = 0 ; i < 3; ++i ) cout << count[i] << endl ; return 0; } void fn( int q[], int c[] ) { int i, j; for ( i = 0 ; i < 4 ; ++i ) for ( j = 0; j < 3 ; ++j ) if ( q[i] != j ) c[j] -= 1; }
2) What gets printed? #include#include void fn(int a1[]); void swap(int & r1, int & r2); int main ( void ) { int i, qa[] = {7, 9, 6, 8}; fn(qa); return 0; } void fn( int a1[] ) { int i, j, k; for ( i = 0 ; i < 3 ; ++i ) for ( j = i + 1; j < 4 ; ++j ) if ( a1[i] > a1[j] ) { swap ( a[i], a[j] ); for ( k = 0 ; k < 4 ; ++ k ) cout << setw(3) << a[k]; cout << endl; for ( k = 0 ; k < 4 ; ++ k ) if ( k == j || k == i ) cout << "---"; else cout << " "; cout << endl; } } void swap(int &r1, int &r2) { int tmp; tmp = r1; r1 = r2; r2 = tmp; }
B) C++ CLASS 1) What gets printed? #includeclass Time { public: void get_time(int h){ hr = h; } void diff(Time d){ cout << hr - d.hr << endl; } private: int hr; }; int main ( void ) { Time dawn, now; dawn.get_time(6); now.get_time(13); now.diff(dawn); return 0; }
C) C++ CONSTRUCTORS 1) Use the program above to answer the following questions. a) Which is/are the: default constructor(s)? type conversion constructor(s)? copy constructor(s)? b) What gets printed? c) What happens in main(), if 2nd ctor, is changed to a default initializer? (e.g. Book(int p = 0); ) #includeclass Book { public: Book(void){pg = 100;} Book(int p){pg = p;} Book(Book &b){b.pg = pg; cout << "hi\n";} void diff(Book bb){ cout << bb.pg - pg << endl; } private: int pg; }; int main ( void ) { Book moby_dick, hamlet(300); Book sea_wolf(moby_dick); sea_wolf.diff(hamlet); return 0; }
D) C++ I/O STREAMS a) If "dear_john" looks like this: --------------------------------------------------------- DEAR JOHN I love C++ more than YOU !! Farewell --------------------------------------------------------- What does "dear_mom" look like after program is run? b) What gets printed on the terminal screen? c) Why is the " extern "C" " inserted in the program? extern "C" { #include} #include int main ( void ) { char ch; ifstream ifs("dear_john"); ofstream ofs("dear_mom"); while ( ifs.get(ch) ) { if ( ch == 'J' || ch == 'H' ) ofs.put('M'); else ofs.put(ch); } ofs << "The Real End...\n"; cout << "The End...\n"; return 0; }