More CS110 Practice Problems 11/15/2000 Use this declaration for the first several questions: vector <double> numbers(40, 2.50); 1. How many elements may be stored in numbers? [2 pts] __40____ 2. What is the value of numbers[22]? [2 pts] __2.50____ 3. What integer is used as the subscript to reference the first element in numbers? [2 pts] ___0___ 4. What integer is used as the subscript to reference the last element in numbers? [2 pts] ___39___ 5. What integer is used as the subscript to reference the 20th element in numbers? [2 pts] ___19___ 6. What type of data are stored in numbers? [2 pts] ___double_________ 7. Write code to make the 20th element be 0. [3 pts] numbers[19] = 0; 8. Write code to output all of the elements of numbers, one per line. [6 pts] for(int j = 0; j <40; j++) { cout << numbers[j] << endl; } 9. Write code to reassign the vector so its values are: [8 pts] 1, 3, 5, 7, 9, 11 ….. (i.e. all the odd numbers) int value = 1; int n = 0; while(n < 40) { numbers[n] = value; value += 2; n++; } OR for(int j = 0; j < 40; j++) { numbers[j] = 2*j+1; } 2 10. Write a complete C++ program that inputs an undetermined number (up to 100) of integer values and stores those values in a vector. [15 pts] #include <iostream> #include <vector> using namespace std; int main() { vector <int> inputs(100); int n; int x; // number of inputs // a single input cout << "Enter numbers of -1 to quit" << endl; while((cin >> x) && (x!=1) && (n < 100)) { inputs[n] = x; n++; } return 0; } 11. Building on the previous problem, write more code here that determines and displays the largest and smallest values that are stored in the vector. [12 pts] int max = -9999; int min = 9999; for(int j = 0; j < n; j++) { if(inputs[j] > max) max = inputs[j]; else if(inputs[j] < min) min = inputs[j]; } cout << "Max: " << max << endl; cout << "Min: " << min << endl; OR, better, int maxIndex = 0; int minIndex = 0; for(int j = 1; j < n; j++) { if(inputs[j] > inputs[maxIndex]) maxIndex = j; else if(inputs[j] < inputs[minIndex]) minIndex = j; } cout << << cout << << "Max: " << inputs[maxIndex] endl; "Min: " << inputs[minIndex] endl; 3 12. What is the output from each of the following code fragments? Show your intermediate steps, such as keeping track of variables. [12 pts = 4 * 3] a. int sum = 0; for(int i = 0; i <= 4; i+=1) { if( sum % 2 == 0 ) sum += i; cout << i << “ “; } only gets added for i = 0 and i = 1 cout << “Sum: “ << sum; counter = 1; n = 5; while(counter < = n) { counter++; } sum 0 1 1 1 1 Output: 0 1 2 3 4 Sum: 1 this line is always output because the braces are missing on the if statement b. i 0 1 2 3 4 the indentation doesn't cause it to be output selectively counter 1 2 3 4 5 6 n 5 5 5 5 5 5 counter ≤ n TRUE T T T T F cout << counter; Output: 6 c. Given the inputs 0 10 20 30 50 -1 double sum = 0.0; int num = 0; cout << “Enter numbers or –1 to quit:”; cin >> num; while(num != -1) { cin >> num; sum = sum + num; } cout << sum; Output (and input): Enter numbers or -1 to quit: 0 10 20 30 50 -1 <end of input part> 99 note that the cin and sum statements in the loop would need to be reversed for the program to work as advertised sum num initially: 0.0 0 before loop: 0.0 10 in loop 20 20 num!=-1 n/a true true 30 50 true 50 100 true -1 99 false 4 13. Assuming you are within main() and fstream has been included, write code to ask the user to enter a filename. Check if the computer can open the file. If the file is there, output “File found.” If it’s not, output “File not found.” [10 pts] #include <iostream.h> #include <fstream.h> int main() { char filename[20]; cout << "Enter a filename: "; cin >> filename; ifstream inFile(filename); if(!inFile) { cout << "File not found."; } else { cout << "File found."; } return 0; } (alt soln) (with the string class) string filename; cout << "Enter a filename: "; cin >> filename; ifstream inFile(filename.c_str()); if(!inFile) { cout << "File not found."; } else { cout << "File found."; } Don't worry about this solution for exam 2. It'll be covered afterward. Study the char array solution instead of string class solution. 14. Write code to create a vector of 10 integers, all with an initial value of 0. [5 pts] vector <int> x (10, 0); 15. (Note that this problem is NOT a continuation of #14.) You have initialized a vector called vec, but it turns out that you underestimated the number of integers that you wanted to store in that vector. Make vec larger so that it has room for 10 more integers than it currently holds. [5 pts] vec.resize(vec.capacity() + 10); ^ | get current size 16. inputStrings has been defined as a vector of 20 strings. Starting from the beginning of the vector, write a code fragment that will output any strings in this vector that are of length 5. If the string “Sentinel string” is found in the vector, stop looking for any further strings of length 5. [10 pts] int j = 0; //lcv and vector subscript while(inputStrings[j] != "Sentinel string" && j < 20) { if(inputStrings[j].length() == 5) { cout << inputStrings[j] << endl; } j++; } Don't worry about this solution for exam 2. It'll be covered afterward. Use the strcmp solution to the right. try it without using the string class, which is what you'll be more likely to be expected to know! that solution: int j = 0; //lcv and vector subscript while(!strcmp(inputStrings[j], "Sentinel string")) && j < 20) { if(strlen(inputStrings[j]) == 5) { cout << inputStrings[j] << endl; } j++; } 5 17. The following segment of code prompts the user for a char object. Add a switch statement to this code (no need to implement a full program or function), which will output strings based on the following three conditions: [10 pts] if if if inputChar inputChar inputChar is ‘a’, print the word first is ‘b’, print the word second isn’t any of the above, ouput 0 you may assume the input is a valid character (no need to verify) char inputChar; cout << “Enter a character: “; cin >> inputChar; NO comments required switch(inputChar) { case 'a': cout << "first"; break; case 'b': cout << "second"; break; default: cout << 0 }