Question: Consider the following C++ program. It has a while loop that reads integers from the user and calculates and prints the min, max, and average

Consider the following C++ program. It has a while loop that reads integers from the user and calculates and prints the min, max, and average of the input values.

#include  using namespace std; int main() { // Initialize variables int val = 0; cin >> val; int min = val; int max = val; int ave = val; int cnt = 1; // Loop reading input while (cin >> val) { cout << val << endl; if (val < min) min = val; if (val > max) max = val; ave += val; cnt++; } // Print results cout << "min = " << min << endl; cout << "max = " << max << endl; cout << "ave = " << ave/cnt << endl; return 0; } 

Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages.

Step 2: Run your program type in "3 1 4 1 5 9 2 6 5 3 5" followed by ^D (control D) to see what happens. Your program should print the input values followed by the min, max, and ave values of the input sequence.

Step 3: If you look at the while loop in the code above, you will see the condition "(cin >> val)". This statement returns "true" if the read is successful and "false" when the read fails. When you type ^D, that ends the input to the program, so the read fails and the loop terminates. Run you program again and you will see that the read will also fail if you type in "hello mom" instead of integers.

Step 4: Use nano to create a text file called "numbers.txt" and type in the integers above. Now run your program using the command "./a.out < numbers.txt". You should see the same results you got when you typed the values in by hand. In the Linux shell, when we type "program_name < file_name" the operating system will run the program specified and "redirect" the contents of the file into the program as if the user typed it in manually.

Step 5: Edit your program and add "#include " at the top of the file. Then add the following two lines of code at the beginning of the main function.

ifstream infile; infile.open("numbers.txt"); 

The first line declares "infile" as an input file variable. The second line connects the "infile" variable to the text file "numbers.txt" and opens the file so we can read its contents.

Step 6: If you compile and run your program at this point it would still read data from the user. To change this so it reads data from the text file, you need to change "cin >> val" to "infile >> val" in two places in the program.

Step 7: You need to make one final change to your program above to close your input file after you have finished reading it. Add the line "infile.close();" just before the "return 0;" line to tell C++ you are finished reading the input file.

Step 8: Compile and run your modified code. You do not need to use file redirection this time because your program knows the name of the file to process. You should see the same output you saw in steps 2 and 4. Congratulations, you just finished your first program using input files.

Step 9: Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!