Introduction to Computer Science ITCS 1214-091 (Barnes) Practice for Test 3 Spring 2007

This is a 75 minute test.

Write your name on all sheets. THIS IS A CLOSED-BOOK, CLOSED NOTE EXAM.

Problem Possible Points Points Earned

1 12 2 20 3 40 4 10 5 20 6 10 Total 112 1. (12 points) Functions

The guidelines for building functions are: Organization Autonomy Encapsulation Reusability

Define each of these terms and explain how to apply these guidelines in writing programs. 2. (20 points) Function example The code below is from Knowlton, 11.1. On pg. 183 in your text a diagram is given for how to break this program into functions. Draw squares around parts of the code below to be broken into separate functions and determine what variables each function will need to know. Write each of the functions in the diagram and rewrite series.cpp to call these functions (don’t forget to include function prototypes at the top). Bonus: replace print_odd, print_even, and print_all functions with a single function called with different parameters.

// series.cpp #include

int main() { int choice; // variable for user input int i; // variable for loops and outputs

do // loop until a valid choice is entered { cout <<"Which series do you wish to display?\n"; cout <<"1 - Odd numbers from 1 to 30\n"; cout <<"2 - Even numbers from 1 to 30\n"; cout <<"3 - All numbers from 1 to 30\n";

cin>> choice; // get choice from user if ((choice < 1) || (choice > 3)) // check entry { cout << "Choice must be 1, 2, or 3\n"; } } while ((choice < 1) || (choice > 3));

switch (choice) { case 1: for (i=1; i<=30; i=i+2) cout << i << ' '; cout << endl; break; case 2: for (i=1; i<=30; i=i+2) cout << i << ' '; cout << endl; break; case 3: for (i=1; i<=30; i++) cout << i << ' '; cout << endl; break; } return 0; } 3. (40 points) Answer Knowlton, p174-5, questions 1-20 Knowlton, p 188-189, questions 1-20 Knowlton p 191, Matching 1-10 Knowlton p 191-Written Questions 1-10 Knowlton p 228-230 Questions 1-20 4. (10 points) Passing parameters Predict the output of the code in Code List 12-3, and explain why this output is the way it is. Next, do Knowlton Step-by-Step 12.2 and predict the new program output, and explain why this output is different.

Give two reasons to use pass by value to send data to functions. Give two reasons to use pass by reference to send data to functions. List the advantages and disadvantages of using global variables in your programs. 5. (20 points) Files Write a program that will determine gas mileage between fill-ups. Input : A file where each row/record contains data for one gas tank: miles driven (as a float) gallons of gas (as a float) Output : Display the computed Miles per gallon (as a float) with an appropriate label for each record in the data file and accumulated MPG for all records.

Sample data file : 100.0 5.0 449.5 16.7

Sample output : Number of miles driven : 100.0 Number of gallons used : 5.0 You averaged 20.0 MPG for this tank Number of miles driven : 449.5 Number of gallons used : 16.7 You averaged 26.916 MPG for this tank ------You averaged 23.458 MPG for all tanks in this data file. 6. (10 points) Augment the program in problem 5 to save the MPGs to a file.