Objectives:
In this lab, we will practice:
- Using a while loop to read from a data file, and process the information read.
- Decomposing the program into modules and implementing each module using a user defined function.
- Practice writing C++ user defined functions with value and reference parameters.
Assignment:
Design an algorithm that reads values from a data file, display the values, and computes and displays the minimum, maximum and average for a list of numbers.
Implement your algorithm in a new project (within your existing CodeLite workspace).
Use the 2170 template for this and all CodeLite projects.
You need to write 3 user defined functions for this program.
Develop each function one by one. Write one function, compile and run the program with that function. If successful, move on to add the 2nd function, compile and run that program. If all is correct, move on to add the 3rd function. Do not try to implement all the functions at once.
- Write a function named openFile(). This function is responsible for:
- Prompting the user to enter the data filename
- Opening the data file
- Verifying that the input file opened properly
- Passing the input file stream back to the callee of the function with a reference parameter
- Write a second function named computeStatistics().
This function is responsible for:
- Reading in integer values (one number at a time) from the data file (Remember, file streams are always passed to functions as reference parameters)
- Displaying the values as they are read in
- Computing the statistics:
- smallest value
- largest value
- average value
- number of entries
- Pass all of the statistics back to the calling function through reference parameters.
- Write a third function named displayResults().
This function is responsible for:
- Displaying the statistics calculated in computeStatistics (see the Examples section).
- Question: Should the parameters to this function be value parameters or reference parameters?
As you are writing these three functions, think:
- What should be the type of this function: void or value returning?
- What are the parameters of this function?
- Should each parameter be passed by value or by reference?
Examples
You can use the following files as example inputs:
You can either download these and save them into your project directory, or in a terminal window on
ranger,
cd to the project directory and execute the following command:
cp /nfshome/hcarroll/public_html/2170/private/closedLabs/lab8A-test*.txt .
Your program should match the following output exactly: (user input in bold text)
Please enter a filename: invalid.txt
You entered invalid.txt
ERROR: Can not open data file!
Please enter a filename: /nfshome/hcarroll/public_html/2170/private/closedLabs/lab8A-test1.txt
You entered /nfshome/hcarroll/public_html/2170/private/closedLabs/lab8A-test1.txt
Values from the data file:
-72
64
-81
Read in 3 values
The smallest value is -81
The largest value is 64
The average value is -29.7
Please enter a filename: /nfshome/hcarroll/public_html/2170/private/closedLabs/lab8A-test2.txt
You entered /nfshome/hcarroll/public_html/2170/private/closedLabs/lab8A-test2.txt
Values from the data file:
63
-39
28
-2
44
24
98
-76
-12
Read in 9 values
The smallest value is -76
The largest value is 98
The average value is 14.2
Submission
Submit your source code using the
handin program. For
handin, for this lab, type the following in a terminal window exactly as it appears:
handin lab8A main.cpp
To verify your submission, type the following in a terminal window:
handin lab8A
Rubric:
Points Item
------ --------------------------------------------------------------
10 Documentation
Header comment block at the beginning of each file with:
+ Your full name
+ Date(s) code was written
+ Description
Comments explaining the role of each variable and major section of code
40 Correctness
Program solves the assigned problem using methods described in program description
Program compiles without errors
Program executes without crashing
Program produces the correct output
------ --------------------------------------------------------------
50 Total