Name CPTR246 Spring '17 (100 total points) Exam 2 1. Pointer parameters (the old C way) In the following program, make all of the changes to convert the call-by-reference parameters in the function computeoptions to be call by reference using pointer parameters (known as the old C way). (8 points) #include <iostream> using namespace std; void computeoptions(double itemcost, int percent, double kohlscash, { double & costwithcoupon, double & costwithkohlscash) costwithcoupon = itemcost - (itemcost * percent / 100.0); costwithkohlscash = itemcost - kohlscash; int main() { double price, coupon, kohlscash, cost1, cost2; cout << "What is the cost of the item: "; cin >> price; cout << "Is your coupon 20%, 30%, or 40%? "; cin >> coupon; cout << "How much Kohl's Cash do you have? "; cin >> kohlscash; computeoptions(price, coupon, kohlscash, cost1, cost2); if (cost1 < cost2) cout << "Use the coupon." << endl; else cout << "Use your Kohl's Cash." << endl; return 0; 1
2. Pointer Basics In the box provided, write what would be displayed on the screen. (23 points) #include <iostream> #include <cstring> using namespace std; void what(char * x){ *(x + 5) = 't'; int main() { int x = 10; int * aptr; int * bptr; char * cptr; int numbers[6] = {3, 1, 4, 1, 5, 9; char adage[30] = "spring forward"; aptr = &x; bptr = numbers; cptr = adage; cout << *aptr << " " << *bptr << " " << *cptr << endl; cout << aptr[0] << " " << bptr[1] << " " << cptr[2] << endl; cout << *numbers << " " << (*numbers + 2) << " " << *(numbers + 2) << endl; if (numbers + 1 == numbers + 3) cout << "true" << endl; else cout << "false" << endl; Enter output here. bptr[2] = 0; bptr = aptr; *aptr = 5; *(cptr + 10) = '\0'; bptr = numbers + 2; cout << *aptr << " " << *bptr << endl; cout << x << " " << numbers[0] << endl; cout << adage << endl; for (int i = 0; i < 6; i++) cout << numbers[i] << " "; cout << endl; if (*bptr == numbers[2]) cout << "true" << endl; else cout << "false" << endl; what(adage); cout << adage << endl; return 0; 2
3. Structures: Assume that the following code appears in your program. struct employee { int ID; double hourlyrate; char name[30]; double hours; int weeks; ; // defines employee "type" // employee id number // pay rate // employee's first name // hours this week // number of weeks of vacation employee employee1 = {123, 12.75, "Mary", 40, 1; employee employee2 = {567, 10.75, "John", 50, 2; employee * empptr = & employee1; // first employee // second employee (a) For each of the following, indicate if the reference is a syntax error or not. If not, give the value of the reference? (8 points) Reference Error? (yes or no) If no, give its value employee1->id empptr->name[0] employee2.hours employee2.name[0] (b) Write if/else statement(s) that determine which employee has the highest hourly rate (assuming the hourlyrate has changed and you don t know the values) and displays his/her name on the screen. (Do not use literals, like Mary or John but use the name field instead. Display the literal none if they have the same hourly rate.) (4 points) (c) Complete the function below called GiveRaise that takes two parameters, an employee pointer and a double. The function is to increase the employee s hourly rate by the amount passed. It returns nothing. (4 points) void GiveRaise(employee * empptr, double raiseamount){ (d) Using the function in the previous question, write one line of code that gives employee2 a $3.00 an hour raise. (2 points) 3
4. Null-terminated Character Arrays: Use the handout (copies of pages 923-935 plus page 918) as needed for the following questions. (a) To the right of the following code, write what would be displayed on the screen. Reference Figure 22.21 on page 923. (12 points) #include <iostream> #include <cstring> using namespace std; int main() { char s1[20] = "apple"; char s2[20] = "peach"; char s3[20] = "orange"; char s4[20] = "pear"; char s5[20] = "banana"; char s6[20] = "grapes"; strcpy(s1, s2); strncpy(s3, s4, 3); strcat(s5, s6); cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; cout << s5 << endl; cout << s6 << endl; return 0; (b) Complete the following function that takes a null-terminated character array as a parameter and returns the number of uppercase letters in that string. For example, the function call howmanyuppercase( Hello World! ) would return a 2. Reference Figure 22.17 on page 918 (the last page of the handout). (4 points) int howmanyuppercase(char * str){ 4
5. Dynamically Allocated Arrays Assume that the following code has been written: int r,c; cout << "How many rows do you have?"; cin >> r; cout << "How many columns do you have?"; cin >> c; a. Write code to dynamically allocate an array of doubles called values that has r rows and c columns. (4 points) b. Write code that counts the number of positive values and negative values stored in the array values defined in the previous question. Zeroes are ignored. It should display those counts in a message such as There are positives and negatives. (6 points) 5
6. Command Line Arguments, File Processing, and Strings: (25 points) Write a program that takes a file of strings as input. There is one string per line, with some having a mix of characters, numbers, and whitespace. However, some of the lines may be blank (aka empty strings, i.e. their length is zero). The program is to determine (1) the number of strings in the file, (2) the number of those strings that are empty, and (3) the average length of the non-empty strings in the file. Display those values (identifying which is which) at the end. Important points: The name of the file MAY come from the command line. If it is NOT there, ask the user to enter it at the keyboard. Do NOT display any totals if the input file did not open properly. Display an appropriate error message and end the program instead. Do not assume that the file contains any strings (i.e., your program should handle an empty file gracefully that is, give appropriate messages). Do not assume that the file contains any non-empty strings (i.e., your program should handle this situation gracefully that is, give appropriate messages). You do NOT need to have more than the main function. You do NOT need to include any comments. Hints: This program can be written using either string objects or null-terminated character arrays (or a mixture of the two if you are so inclined). To get the length of a null-terminated character array, use the free function strlen - reference Figure 22.21 on page 924. To get the length of a string object, use the public method length( ). (An additional blank page has been added for your use. You may remove it if you like.) 6
7