Objectives: reading from sequential access files with proper use of priming read
ID: 3762632 • Letter: O
Question
Objectives: reading from sequential access files with proper use of priming read, loop read, and test for end-of-file; one-dimensional arrays, defining functions. In this program you will be writing two functions which you will compile and link with a test driver provided by your instructor. A short main() program is provided below if you would like to unit test your functions before linking with the test driver. The first function, readData(char const *, double [], int) accepts as arguments an input file name, a one-dimensional array of double, and an int to indicate the maximum number of triplets to read from the file into the array. For example, you will probably read values from the file three at a time. The first value will be loaded into the first element, the second value the second element, etc. Continue loading values in groups of three from the file into the array, but do NOT load values into the array if you are unable read a complete set from the file. So, you will need to be testing for End-Of-File, as well as input failure after each individual value is read in (Note: assume all test input files will contain one blank line at the end). Ultimately, the function must calculate and return an int code as follows: Return Code Message 0x0100 Error: Input file does not exist 0x0200 Input file exists, but contains 0 complete triplets 0x0400 Input file has less than MAX triplets 0x0800 Input file has exactly MAX triplets 0x1000 Input file has more than MAX triplets 0x2000 Error: incomplete number triplet; EOF reached in middle of triplet 0x4000 Error: A value was attempted to be read in, but input failed 0x8000 Error: Input value not within proper range. Your program must react correctly to each of the above conditions. In each case, make sure the proper return code(s) is/are returned. Combinations of codes are possible, i.e. if the input file exists, you will always report whether there were less than, more than, or exactly MAX number triplets. If you encounter an input value not in the proper range, continue processing the current triplet, and but do not process any additional triplets. If you encounter an incomplete number triplet, or input fails, your program must stop processing to return immediately with no further input file processing. However, in all cases, immediately prior to returning, add the number of triplets successfully read in to the Return Code. readData() will open and read values from the input text file, and copy them into the provided array. The input file is expected to contain up to MAX triplets of floating point values. Your program should be written to only process up to MAX number triplets. When your program processes the required data, it must return the proper code(s) combined with the number of triplets processed. Though not required, it may be useful to display descriptive output messages during processing. Example: Suppose the input file contains the values 1 2 3 4 5 6 -7 x 1 2 3 4. The return code would be: 0xc402, and your function might display the messages: Error: Input value -7 not within proper range. Error: invalid character 'x' read while reading triplet #3 Program aborting. 2 triplets processed. IMPORTANT: It is assumed that all input text files end with a blank line. Without this assumption, you will not be able to get consistent, correct results. Your second function, displayDataTable(double [], int) accepts a one-dimensional array of double, followed by an int indicating the number of triplets contained within. e.g. 5 indicates that there are 15 values in the one-dimensional array. This function returns void. For each set of three numbers in the array, it will: Display the three numbers Display the sum of the three numbers Display a running total of all numbers read For example, suppose the array contains the values 1 2 3 4 5 6. The output for your program would appear as: First Second Third Running Value Value Value Sum Total 1.00 2.00 3.00 6.00 6.00 4.00 5.00 6.00 15.00 21.00 Make sure to output appropriate table headers for your data, and organize the data into nicely formatted columns as shown above. You could unit test your functions with the following program code, but do not submit this code as part of your assignment. #include #include using namespace std; int main() { int retCode=0; double darray[30]; retCode = readData("sum.txt", darray, 10); cout << "Return code is :" << hex << retCode << endl; displayDataTable(darray, retCode&0xFF); cin.get(); return 0; } Consider the following sample input files which could be potentially passed to your readData(): :::::::::::::: sumEmpty.txt :::::::::::::: :::::::::::::: sumEQ10.txt :::::::::::::: 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 11.0 22.0 33.0 :::::::::::::: sumGT10.txt :::::::::::::: 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 1.0 2.0 3.0 -1.0 -3.0 -5.0 1.0 1.0 1.0 :::::::::::::: sumInc.txt :::::::::::::: 1.0 2.0 3.0 4.0 :::::::::::::: sumLT10.txt :::::::::::::: 1.0 2.0 3.0 4.0 2.0 -1.0 Sample evaluation rubric: 25 (+0.00) Nice work. (-2.50) Fails to return 1 error code correctly (-5.00) Fails to return 2 error codes correctly (-10.00) Fails to return 4 error codes correctly (-5.00) Fails to report correct number of triplets processed (-2.00) Numbers, sum, running total not displayed in organized tabular format (-1.00) Incorrect sum for each number triplet (-1.00) Running total not displayed or not calculated properly Milestone submission 1. Create the two stubs for readData() and displayDataTable() 2. Implement readData() so that it opens the provided file, and reads and displays all values from the file until End-Of-File, or input failure occurs. You are being provided with a pre-compiled main() program to link with your functions for testing. Your functions must compile, link, and run properly with this program for full credit. You will submit your properly compiling source file containing only your readData() and displayDataTable() functions. Here is what you need to do: Write the functions as described above. Make sure all required header files are included at the top of your program file. You may wish to write a main() program to test your function initially before proceeding with the following steps. If you do so, you must remove it or comment it out before continuing. Since you will not be submitting the main() function, You will be compiling your program by doing the following: Type your program code into Code::Blocks Save the linkable testdriver smfnMain.a to the same folder where your C++ program file is. (Right-click, then Save Link As ... or similar.) In Code::Blocks, go to the Settings menu, and select Compiler to bring up a dialog box Make sure the Global Compiler Settings is selected on the left, then click on the Linker Settings tab Just below the Link libraries box, click the Add button, then navigate to where you saved smfnMain.a. Click Ok to close the library selection dialog box, and then Ok to close the Global Compiler Settings. You can now Compile and Run your program using the test driver. If your program does not compile, then you probably have not defined your function correctly, or forgot to include all appropriate header files. When running with the test driver, the program output will tell you if it is working correctly. Your program will be evaluated using the exact same test driver. Submit only the file containing your function. You should not provide any test main() that you may have written for your own test purposes. When you submit your program on the class website, you should receive no compiler or linker errors.
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int
main()
{
int retCode=0;
double darray[30];
retCode = readData("sum.txt", darray, 10);
cout << "Return code is :" << hex << retCode << endl;
displayDataTable(darray, retCode&0xFF);
cin.get();
return 0;
}
Sample input files:
::::::::::::::
sumEmpty.txt
::::::::::::::
::::::::::::::
sumEQ10.txt
::::::::::::::
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
11.0 22.0 33.0
::::::::::::::
sumGT10.txt
::::::::::::::
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
1.0 2.0 3.0
-1.0 -3.0 -5.0
1.0 1.0 1.0
::::::::::::::
sumInc.txt
::::::::::::::
1.0 2.0 3.0
4.0
::::::::::::::
sumLT10.txt
::::::::::::::
1.0 2.0 3.0
4.0 2.0 -1.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.