Spring 1995 Final Exam (100pts) Name________________________ 1.(6pts) Write the output from the following C++ program fragment : int n = 6; // Output: int counter = 0 do { cout << " " << counter; counter = counter + 2; } while ( counter <= n ); answer: 0 2 4 6 2.(15pts, 3each) Write the number of times each while loop repeats its iterative part. Assume counter, accumulator, and n are int objects. Hint: Zero, infinite and unknown are perfectly valid answers. // a. ______ // d.________ while ( counter <= n ) int n = 5; cout << " " << counter; counter = 1; while ( counter <= n ) // b._______ cout << counter; counter = 1; counter = counter + 1; n = 5; while ( counter <= n ) // e._________ cout << " " << counter; int counter = 1; int accumulator = 0; // c._______ while ( counter <= 5 ) { int counter = 1; accumulator = accumulator + counter; while ( counter <= 10 ) counter = counter + 1; counter = counter + 1; } answer: a. unknown, b. infinite, c. 10, d. infinite, e. 5 3.(8pts) Write a for loop that generates the following output (many middle terms are intentionally left out): -1000 -996 -992 -988 //... output deleted 988 992 996 1000 answer: for( int x = -1000; x <= 1000;x+=4) cout << x << endl; 4. (10pts, 2ea) True/False Write T if the statement is True. If the statement is False, write F. a___ The components of an array can be accessed in any order. b___ The elements of an array type may be different data types, that is list[1] could store an int and list[2] could store a float. c___ An array of doubles declared to hold 100 maximum values need not have all elements initialized. d___ The class of elements stored in an array is limited to int, char, float, and double. e___ Given int x[100], the element with the smallest subscript value is referenced as x[1]. answer: a. T, b. F, c. T, d. F, e. F 5. (8pts) Write the output of the following program (10pts): #include int main() { int j, n, x[10]; x[0] = 85; x[1] = 70; x[2] = 100; x[3] = 65; n = 4; cout << "Array: "; for(j = 0; j < n; j++) { cout << x[j] << " "; } cout << endl; int hi = x[0]; for(j = 1; j < n; j++) { if(x[j] > hi) hi = x[j]; } cout << "Highest: " << hi << endl; return 0; } answer: Array: 85 70 100 65 Highest: 100 6. (10pts) Complete the following program that declares an array of bankAccount Objects by computing & displaying the total balance of all 10 bankAccount objects no matter what the balances are: #include #include "ouracct.h" int main() { bankAccount acct[10]; acct[0] = bankAccount("CUST0", "0000", 000.00); acct[1] = bankAccount("YourLastName", "ANY4", 111.11); acct[2] = bankAccount("AUSTEN", "2222", 222.22); ... acct[8] = bankAccount("CUST8", "8888", 888.88); acct[9] = bankAccount("CUST9", "9999", 999.99); double total = 0.00; // Compute total of all 10 accounts answer: for( int x = 0; x < 10;x++) total = total + acct[x].balance(); cout << "The total of all 10 accounts is: " << total << endl; return 0; } 7. (2pts) Sequential search makes how many comparisons when the search element does not match any array element and there are 578 elements in the array? _________ answer: 578 8. (3pts) Binary search makes how many comparisons when the search element does not match any array element and there are 578 elements in the array? _________ answer: 10 9. (8pts) Write the output generated by the following program #include int main() { int j, n = 5, x[10]; n = 6; x[0] = 1; x[1] = 1; for(j = 2; j < n; j++) x[j] = x[j-1] + x[j-2]; for(j = 0; j < n; j++) cout << x[j] << " "; return 0; } answer: 1 1 2 3 5 8 10. (8pts) Write C++ code that computes the average of the first n (58) elements of x declared as follows double x[100]; n = 58; // Presume the first 58 elements of x have been assigned value answer: double sum = 0.0; for(j = 0; j < n; j++) sum = sum + x[j]; double average = sum/n; 11. (4pts) How do we know which member function is the default constructor of a class? answer: It has the same name as the class and no arguments. 12. (6pts) Given the following problem statement, list the objects that might prove useful in designing and implementing a solution. Problem: Implement a computer based software system that manages the inventory for a computer retail store. Each inventory item must maintain the product name, the cost, and the quantity on hand. Given the product name, we should be able determine the cost and the quantity and how many units are available for sale. We should be able to add or delete inventory items. The system should be able to generate a report of all items and how much is left. answer: objects might include: inventoryItem, product, store 13. (6pts) From the previous description, list at least three operations that are required and list the class to which we should add the operation as a member function. answer: store class might have additem and deleteitem inventoryItem might have sell(number of items) receive(number of items) 14. (6pts) Given a class Team with a private data member list as: private: player * list[9]; and a constructor whose code includes: for (int index = 0; index < 9; index ++) list[index] = new player( ); Declare the destructor for the class Team as it would appear in the team.h file. Implement the destructor for the class Team as it would appear in the team.cc file. answer: team::~team() { for (int index = 0; index < 9; index ++) delete list[index]; }