



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The code and instructions for a c++ programming exercise focused on output formatting using different widths and precisions, as well as reading and writing to files. The exercise consists of four tasks, including copying code, writing custom formats, handling input and output files, and making modifications to the program.
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




After completing this exercise you should be able to write commands that (1) use output formatting and (2) read input from and write output to files.
In previous exercises you have used various cin and cout commands for input and output, respectively. You have accepted the default format for numbers provided by C++. In this exercise you will learn new ways to space output, ways to format the appearance of numerical output and using files in place of the keyboard for input and in place of the screen for output. All these tasks will extend what you have learned about cin and cout.
This exercise has four tasks. The first and third tasks only require you to copy code from the assignment and execute the code. The second and fourth tasks require you to modify the code in the first and third tasks, respectively. In the third and fourth tasks you will be using input from and output to files. Use the same procedure you followed for exercises two and three to create a single file, called the submission file, to which you will copy all your listings and the screen displays from the program execution. See the notes for the second exercise for details about copying the screen to another file and preparing a single file to submit all your program listings and output. In this exercise you will have input and output files to view in addition to the source and submission files. Follow the instructions on which of these input and output files you should copy to the submission file.
Task one: Output formats – Copy the code below into Visual C++ then compile and execute it. Change the name and date in the comments to your name and lab date. Review the output and compare the results to the various statements. You should be able to account for the differences in the format of the numbers and the differences in spacing between the cases run.
Comp 106L Programming exercise four Myname Goeshere Place date here Task one: Output formats */ #include
int main() { double x1 = 123.45678, y1 = -98.76543, z1 = 1413.746352; double x2 = 23.45321, y2 = -8.7, z2 = 3.745; double x3 = 1.23e-16, y3 = -5.3245e22, z3 = 938457483e-3;
cout << "Example of output from different formats.\n"; cout << "\nDefault output:\n"; cout << "\nx1 = " << x1 << ", y1 = " << y1 << ", z1 = " << z1; cout << "\nx2 = " << x2 << ", y2 = " << y2 << ", z2 = " << z2; cout << "\nx3 = " << x3 << ", y3 = " << y3 << ", z3 = " << z3;
cout << "\n\nDefault output except using setw(w):\n"; cout << "\nx1 = " << setw(9) << x1 << ", y1 = " << setw(12) << y << ", z1 = " << setw(7) << z1; cout << "\nx2 = " << setw(9) << x2 << ", y2 = " << setw(12) << y << ", z2 = " << setw(7) << z2; cout << "\nx3 = " << setw(9) << x3 << ", y3 = " << setw(12) << y << ", z3 = " << setw(7) << z3;
cout << "\n\nDefault output except using setprecision(7):\n" << setprecision(7); cout << "\nx1 = " << x1 << ", y1 = " << y1 << ", z1 = " << z1; cout << "\nx2 = " << x2 << ", y2 = " << y2 << ", z2 = " << z2; cout << "\nx3 = " << x3 << ", y3 = " << y3 << ", z3 = " << z3;
cout << "\n\nOutput using setw(w) with setprecision(7) still in effect:\n"; cout << "\nx1 = " << setw(9) << x1 << ", y1 = " << setw(12) << y << ", z1 = " << setw(7) << z1; cout << "\nx2 = " << setw(9) << x2 << ", y2 = " << setw(12) << y << ", z2 = " << setw(7) << z2; cout << "\nx3 = " << setw(9) << x3 << ", y3 = " << setw(12) << y << ", z3 = " << setw(7) << z3;
cout << "\n\nSame as previous except "fixed" output specified:\n" << fixed; cout << "\nx1 = " << setw(9) << x1 << ", y1 = " << setw(12) << y << ", z1 = " << setw(7) << z1; cout << "\nx2 = " << setw(9) << x2 << ", y2 = " << setw(12) << y << ", z2 = " << setw(7) << z2; cout << "\nx3 = " << setw(9) << x3 << ", y3 = " << setw(12) << y << ", z3 = " << setw(7) << z3;
cout << endl << endl; // create blank lines before "Press any key" return EXIT_SUCCESS; }
Do not copy the code, but copy the output from this code to the submission file. Write your name, lab day and exercise number at the top of the submission file. You may want to print the code and the results as a study guide.
Task two: Write your own formats – Revise the code in task one so that the results are printed out only one time, using a revised format as shown below.
No. x value y value z value 1 123.46 -98.77 1413. 2 23.45 -8.70 3. 3 0.00 -0.05 938457.
In the revised format, the data form a table with labels at the top and the data set numbers (1, 2,
T2 = c0 + c1 * eT2 + c2 * eT2 * eT2; T3 = c0 + c1 * eT3 + c2 * eT3 * eT3; T4 = c0 + c1 * eT4 + c2 * eT4 * eT4;
// Write output to file and exit
outFile << "Data reduction of spatial temperature measurements\n\n"; outFile << "x location y location z location " << "r coordinate temp reading Temperature\n" << " (meters) (meters) (meters) " << " (meters) (millivolts) (Celsius)\n";
outFile << fixed; outFile << setprecision(3) << setw(10) << x << setw(13) << y1 << setw(13) << z << setw(13) << r1 << setprecision(2) << setw(13) << eT << setprecision(1) << setw(14) << T1 << endl; outFile << setprecision(3) << setw(10) << x << setw(13) << y2 << setw(13) << z << setw(13) << r2 << setprecision(2) << setw(13) << eT << setprecision(1) << setw(14) << T2 << endl; outFile << setprecision(3) << setw(10) << x << setw(13) << y3 << setw(13) << z << setw(13) << r3 << setprecision(2) << setw(13) << eT << setprecision(1) << setw(14) << T3 << endl; outFile << setprecision(3) << setw(10) << x << setw(13) << y4 << setw(13) << z << setw(13) << r4 << setprecision(2) << setw(13) << eT << setprecision(1) << setw(14) << T4 << endl; return EXIT_SUCCESS; }
Next create the data file; start with the Project->Add New Item command to create a blank text file. This command will open the usual dialog box that you use when you are starting a new source file. Choose the Visual C++ folder in the Catagories: menu in the upper left and choose Text File (.txt) from the Templates: menu in the upper right. (You may have to use the slider in this menu to find this selection.) Next, enter the file name data.in in the next to last text box near the bottom (to the right of the word Name: ). This is the file name used in the program statement ifstream inFile("data.in"); that associates the operating system file name, data.in, with the program variable name for the file, inFile. Once you have opened the file, copy and paste the following data file into the blank text file.
Review the program listing. You have already seen that the input file has the operating system file name, data.in, and the program variable name, inFile. The output file also has two names: the operating system file name, results.out and the program variable name, outFile. Note that the program variable names, inFile and outFile, are the names used in the subsequent input and output statements in the program.
Compile, link and run the program. Because the output is written to a file there will be no screen output, except for the usual “Press any key to continue” message. You can open the output file in
Visual C++. To do this you will have to select the “All files (.) option from the pull down menu next to “Files of type: “ near the bottom of the Open menu.
For this task, copy only the output file ( not the listing) to your submission file.
Task four: Modifications to program in task three and (optional) exchanging data with a spreadsheet – In this task you will make several modifications to the program in task three. These are listed separately below.
a. Divide all the x values by 2.
b. Subtract 1 from all the y values.
c. Add 2 to all the z values.
d. Multiply all the temperature measurements by 1.
e. Round your results from steps a to d to six decimal places.
See instructions below for the option of using Excel to make these modifications.
Temperature data reduction
x (m) y (m) z (m) r (m) T (mV) T (C) 13.4 -0.6 0.4 13.5 2.26 53. 13.0 -0.8 2.8 13.3 6.58 130. 17.3 0.2 5.1 18.1 1.15 33. 13.5 -0.2 3.9 14.1 0.68 24.
Compile, link, and run the revised code with the revised input file. Copy your code listing, your input file and your output file to the submission file.
To transfer data from a computer file to Excel, copy the data and paste it into a spreadsheet. This will usually place all the data in one column. Use the following steps to get each data item in separate columns.
Select the cells with the data you want to move into columns. Then choose Text to Columns from the Data menu. This initializes the text to columns wizard.