CS211 - Algorithms and Data Structures II Department of Computer Science, NUIM T Naughton, NUIM Lab Exam 2 Problem 2A.1 #include void main(void){ int n; // input from user int row; // counter for rows int counter; cout << endl << endl << "Please enter a nonegative integer:" << endl; cin >> n; cout << endl << "Here we go:" << endl; /* ** Write out the upper triangle. This will entail, at each row, ** writing out (row) spaces and (n - row) stars. */ row = 0; while(row < n){ // write out the correct number of spaces counter = 0; while (counter < row) { cout << ' '; counter++; } // write out the correct number of stars counter = row; while (counter < n) { cout << '*'; counter++; } cout << endl; row++; } /* ** Write out the lower triangle. This will entail, at each row, ** writing out (n-row-1) spaces and (row+1) stars. */ row = 0; while(row < n){ // write out the correct number of spaces counter = 0; while (counter < (n-row-1)) { cout << ' '; counter++; } // write out the correct number of stars counter = 0; while (counter < (row+1)) { cout << '*'; counter++; } cout << endl; row++; } cout << "Goodbye!" << endl; } Problem 2B.1 #include void main(void){ int n; // input from user int count1; // counter for stars & rows int count2; // counter for spaces cout << endl << endl << "Please enter a nonegative integer:" << endl; cin >> n; cout << endl << "Here we go:" << endl; /* write first line */ count1 = 0; while(count1 < n){ cout << '*'; count1++; } cout << endl; /* write middle lines */ count1 = 0; while (count1 < (n-2)){ count2 = 0; cout << '*'; while (count2 < (n-2)){ cout << ' '; count2++; } cout << '*'; cout << endl; count1++; } /* write last line */ count1 = 0; while(count1 < n){ cout << '*'; count1++; } cout << endl; cout << endl << "Goodbye!" << endl; } Problem 2C.1 #include void main(void) { int n; // input from user int row; // counter for rows int counter; cout << endl << endl << "Please enter a nonegative integer:" << endl; cin >> n; cout << endl << "Here we go:" << endl; /* ** At each row, write a star, a space, and (n-row) stars. */ row = 0; while (row < n) { cout << "* "; counter = 0; while (counter < (n-row)) { cout << '*'; counter++; } cout << endl; row++; } cout << "Goodbye!" << endl; } Problem 2A.2 int findhighestlessthan100(int * list, int len){ /* This function returns the index of the element with the ** highest value in 'list' that is less than 100. 'list' is ** an integer array of length 'len'. ** If no suitable values are found then an index to the first ** element is returned. */ int highest; // the index with the highest value < 100 int count; highest = 0; count = 1; while (count < len){ if ((list[count]>list[highest]) && (list[count]<100)){ highest = count; } count++; } return highest; } Problem 2C.2 (Note: One can also solve this problem using the absolute function abs() from #include) int findclosestto10(int * list, int len){ /* This function returns the index of the element in 'list' ** that has a value closest to 10. 'list' is an integer ** array of length 'len'. ** If several values are equidistant from 10 then the first ** one encountered is returned. */ int closest; // the element with a value closest to 10 int diff; // the difference between list[closest] and 10 int newdiff; // the difference between list[count] and 10 int count; closest = 0; if (list[closest]>10){ diff = list[closest] - 10; } else { diff = 10 - list[closest]; } count = 1; while (count < len){ if (list[count]>10){ newdiff = list[count] - 10; } else { newdiff = 10 - list[count]; } if (newdiff void readints(int * array, int len); /* This program reads 10 integers from the user and stores them ** in an array. Using a second array of type double of length 2, ** the program stores the sum and the average of the list of ** integers. Finally, the program outputs the sum and average. */ void readints(int * array, int len){ int count; cout << endl << endl << "Please enter " << len; cout << " integers, one per line:" << endl; count = 0; while (count < len){ cin >> array[count]; count++; } } void main(void){ const int len = 10; int array[len]; // holds 10 integers double info[2]; // holds sum and mean int count; readints(array, len); info[0] = double(array[0]); count = 1; while (count < len){ info[0] = info[0] + double(array[count]); count++; } info[1] = info[0] / double(len); cout << endl << "Sum is " << info[0] << endl; cout << "Average is " << info[1] << endl; } Problem 2C.3 #include void readints(int * array, int len); /* This program reads 31 integers from the user and stores them ** in an array. Using a second array of type int, of length 31, ** the program stores half the value of each of the first ** array's integers. Finally, the program outputs each int int ** pair where a rounding error has occurred. */ void readints(int * array, int len){ int count; cout << endl << endl << "Please enter " << len; cout << " integers, one per line:" << endl; count = 0; while (count < len){ cin >> array[count]; count++; } } void main(void){ const int len = 31; int array[len]; // holds integers int half[len]; // holds half of values in array[] int count; readints(array, len); count = 0; while (count < len){ half[count] = array[count] / 2; count++; } cout << endl << endl << "Rounding errors occurred with the"; cout << " following pairs:" << endl; count = 0; while (count < len){ if ((half[count]*2) != array[count]){ cout << array[count] << ' ' << half[count] << endl; } count++; } }