Homework 5 UCR CS10: Introduction to Computer Science Spring Quarter 2004, Lecturer: Kris Miller Due Wednesday, May 12 before 8:00pm by web turnin. ------------------------------------------------------------------------------- Turnin: Turnin this assignment to the folder, hw5, using the electronic turnin linked to from the class webpage. For homeworks only, you may turnin .doc (Microsoft Word) or .txt (Notepad) files. You may also turnin the homework as a .cc or .cpp (C++ source) file. Be sure to turn it into the correct folder. At best, you will be penalized 20%. At worst, your homework may not be graded at all. ------------------------------------------------------------------------------- Note: Collaboration policy for this homework assignment Collaboration on this homework is strongly ENCOURAGED. The homework is intended for practice, not assessment -- most people who turn in decent work should get most if not all points for this assignment. Forming study groups is strongly encouraged. You shouldn't simply copy answers from any source -- each other, the book, the web, other books, previous solutions, etc. But you can certainly discuss how to solve the problems, look over each others solutions and help "debug" them, compare answers and redo problems if you determine your answer is wrong, and most importantly, explain concepts related to the problems and solutions. ------------------------------------------------------------------------------- 0. For 1 point bonus (20%), enter the names (first and last name) of the CS10 students you collaborated on this homework with. 1. Self-test Exercise #5-1 (Chapter 5, Exercise #1) 2. Self-test Exercise #5-2 3. Self-test Exercise #5-3 4. Self-test Exercise #5-4 5. Self-test Exercise #5-5 6. Self-test Exercise #5-7 7. Self-test Exercise #5-12 8. Self-test Exercise #5-13 9. Self-test Exercise #5-18 **Self-test exercises have solutions in the back of each chapter** 10. Write a program that takes 10 doubles as input from a file called input.txt and outputs them to a file called output.txt. Your program should output the double values to a precision of 1 (1 number after the decimal point). You should also print to the **screen** (NOT output.txt) the avg of these numbers, but with a precision of 2. **SOLUTION** #include #include #include using namespace std; const int NUMBERS_IN_FILE = 10; int main() { // connect and verify input file ifstream fin; fin.open("input.txt"); if (fin.fail()) { cout << "Error opening input file\n"; exit(1); } // connect and verify output file ofstream fout; fout.open("output.txt"); if (fout.fail()) { cout << "Error opening output file\n"; exit(1); } // output formatting for output file fout.setf(ios::fixed); fout.setf(ios::showpoint); fout.precision(1); // output formatting for screen cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); int count = 0; double number; double sum = 0; // sum must start at 0 to work properly // loops NUMBERS_IN_FILE times while (count < NUMBERS_IN_FILE) { // get number from input file and put into output file fin >> number; fout << number << endl; // add number to sum of all numbers sum = sum + number; // keep track of how many numbers brought in count++; } // output avg to screen cout << "The average of the " << NUMBERS_IN_FILE << " numbers is: " << (sum / NUMBERS_IN_FILE) << endl; return 0; }