A data file contains a series of integer values (positive, negative, and zero).
ID: 3825156 • Letter: A
Question
A data file contains a series of integer values (positive, negative, and zero). The maximum number of values in the file that are greater than or equal to 0 will be 30. Please note, there may be more than 30 numbers in the file. Each value will be separated by at least one blank space and the last value in the file will be followed by a linefeed.
Design a C++ program (using functions to modularize the code) that will
interactively prompt the user for the name of the input file and read it
open the file for reading, using a C++ filestream
read the integer values from the file
count and sum the negative values, compute the average if possible
store the other values (>=0) in an array, counting them as they are read
interactively prompt the user for the name of the output file and read it
open the file for writing using a C++ filestream
write the following to the output file you created - leave at least one blank line between each set of outputs and clearly label all output
your name, lecture section #, assignment #
if there were no negative values in the file, display an appropriate message
if there were negative values in the file display
the sum and count of the negative values
the average of the negative values (set precision to 5 decimal places)
if there were no values >= 0 in the file, display an appropriate message
if there are values >= 0 stored in the array
use bubblesort to arrange the values in the array into ascending order
list the sorted values one per line, right justified, values may have up to 7 digits
display the number of values stored in the array, the average of the values, the variance and the standard deviation of the values (set precision to 5 decimal places)
list all the values in the array that are prime (one per line, right justified) or an appropriate message if there are none
list each nonprime numbers greater than 0 found in the array along with how many factors each has (arrange in 2 columns - right justified) or an appropriate message if there are none
Definitions and Formulas
A prime number is a positive, nonzero integer that has exactly 2 different factors.
if n=# values stored in the array and datai represents the ith value in the array,
average = (data1 + data2 + ... + datan) / n
variance = [(data1-average)2 + (data2-average)2 + ... + (datan-average)2] / (n-1)
standard_deviation = sqrt(variance)
Assumptions
The input file will not be empty. Each line in the file will be terminated by a linefeed (' ').
All values in the file will be integers and they will be separated by whitespace.
Maximum number of values in the file greater than or equal to 0 will be 30.
Maximum number of columns needed to display an integer will be 7.
All values in the file could be negative.
All values in the file could be greater than or equal to 0.
If the file contains values greater than or equal to 0, there will be at least 2 of them (so that variance can be computed).
Requirements
The program MUST make use of functions (at least 4 meaningful functions in addition to main).
The program MUST PASS PARAMETERS to communicate values. No global variables are allowed.
No goto statements may be used.
Program must make use of filestream variables to represent the input and output files. The input file may only be read one time.
The program must prompt for the name of the input file first. Then prompt for the name of the output file.
Failure to adhere to the 5 previous requirements will result in up to a 60% deduction of assignment's point value.
Data values read from the input file and factor counts must be displayed as integers. Averages, variance, and standard deviation must be displayed with 5 digits to the right of the decimal.
Program must include preprocessor directives for all header files used.
Program must use a static array (do not use a variable for the size of the array).
Write your own code - reread the CS 135 academic integrity policy (see syllabus) if necessary.
Test your program adequately!
Documentation
When the program compiles and runs correctly, add the following documentation (comments) to your source file (.cpp file).
At the start of the program file,
Place a comment with your name, lecture section#, and assignment # at the beginning of the file.
List the expected input to the program. Be specific. Describe the content of any input files.
List the expected output of the program. Be specific. (If the input values are supposed to be displayed, include them in the output list.)
When a named constant and/or major variable is declared, provide a meaningful description of what it represents in the program.
For each function, clearly state what will be passed into the function and what will be passed out or returned by the function. Document important local variables. (See function documentation handout.)
Sample terminal session:
[lee@bobby keys]$ more data4four
17 35 -8 12 24 0
23 428 -34 144
-7
[lee@bobby keys]$ g++ assign04.cpp
[lee@bobby keys]$ ./a.out
Please enter the name of the input file: data4four
Please enter the name of the output file: results4four
[lee@bobby keys]$ more results4four
Lee Misch Sec# 10__ Assignment #4
Negative Number Data
====================
Count: 3
Sum: -49
Average: -16.33333
Numbers >= 0 Data
=================
Positive #'s in File
0
12
17
23
24
35
144
428
Average: 85.37500
Variance: 21195.98214
Standard deviation: 145.58840
Prime Numbers
17
23
Nonprime Numbers and Factor Counts
12 6
24 8
35 4
144 15
428 6
[lee@bobby keys]$ more data4four2
26 1 9
42
[lee@bobby keys]$ ./a.out
Please enter the name of the input file: data4four2
Please enter the name of the output file: results2
[lee@bobby keys]$ more results2
Lee Misch Sec# 10__ Assignment #4
Negative Number Data
====================
No negative values found in the file.
Numbers >= 0 Data
=================
Positive #'s in File
1
9
26
42
Average: 19.50000
Variance: 333.66667
Standard deviation: 18.26655
Prime Numbers
No prime numbers found.
Nonprime Numbers and Factor Counts
1 1
9 3
26 4
42 8
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
string getFileName() {
string fileName;
cout << "Enter the file name : ";
cin >> fileName;
return fileName;
}
ofstream readFileInput(string fileName) {
ofstream outClientFile;
outClientFile.open(fileName.c_str());
//error checking
if (!outClientFile)
{
cerr << "File could not be opened." << endl;
exit(1);
}
return outClientFile;
}
vector<int> getValues(ofstream fp) {
vector<int> numbers;
int number;
if(fp.is_open()){
while(fp >> number){
numbers.push_back(number);
fp.get();
}
}
fp.close();
return numbers;
}
void writeonFile(vector<int> numbers, string fileName) {
ofstream myfile (fileName);
int count = 0;
float sum = 0;
float average = 0;
for (int i=0; i < numbers.size(); i++) {
if(numbers[i] < 0) {
count++;
sum+= numbers[i];
}
cout << numbers[i] << ' ';
}
if(count != 0)
average = sum/count;
if (myfile.is_open()) {
myfile << "Number of negative values" << count ;
myfile << "Sum of negative values" << sum ;
myfile << "Average of negative values" << average ;
}
}
int main()
{
string fileNameInput, fileNameOutput;
ofstream outClientFile;
vector<int> arrayInput;
fileNameInput = getFileName();
outClientFile = readFileInput(fileNameInput);
arrayInput = getValues(outClientFile);
fileNameOutput = getFileName();
writeonFile(arrayInput, fileNameOutput);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.