CS211 - Algorithms and Data Structures II Department of Computer Science, NUIM T Naughton, NUIM Lab Exam 1 Problem 1A.1 #include void main(void){ int n; // integer read in int line; // counter for lines int count; // counter for blanks and 'a's cout << "Please enter a nonnegative integer:" << endl; cin >> n; cout << endl << "Here we go:" << endl; line = 0; while (line < n){ count = 0; while (count < line){ cout << ' '; count++; } count = 0; while (count < (line+1)){ cout << 'a'; count++; } cout << endl; line++; } cout << "Goodbye!" << endl; } Problem 1B.1 #include void main(void){ int n; // integer read in int line; // counter for lines int count; // counter for blanks and '*'s cout << "Please enter a nonnegative integer:" << endl; cin >> n; cout << endl << "Here we go:" << endl; line = 0; while (line < n){ count = 0; while (count < line){ cout << ' '; count++; } count = 0; while (count < (n-line)){ cout << '*'; count++; } cout << endl; line++; } cout << "Goodbye!" << endl; } Problem 1.2 One possible answer: int * p = NULL; //Create a pointer called p that points to an integer int a = 42; //Create an integer x p = &a; //Assign to the pointer p the address of integer x cout << *p; //Output the contents of the address pointed to by p Another example: int * p = NULL; int a = 42; p = &a; *p = 43; //Overwrite the contents of the address pointed to by p with 43 cout << a; //Output contents of the address pointed to by p (i.e. variable a). Problem 1.3 #include void main(void){ int iarray[10]; double darray[10]; int counter; cout << endl; cout << "Hi! Please enter 10 integers, one on each line:"; cout << endl; counter = 0; while (counter < 10) { cin >> iarray[counter]; counter = counter + 1; } cout << "Thanks." << endl; counter = 0; while (counter < 10) { darray[counter] = double(iarray[counter]) / 2.0; counter = counter + 1; } counter = 0; while (counter < 10) { cout << iarray[counter] << "\t"; cout << darray[counter] << endl; counter = counter + 1; } }