C++ Program: The name of the data file is \"Data5b.txt\". I attached the data va
ID: 3765944 • Letter: C
Question
C++ Program:
The name of the data file is "Data5b.txt". I attached the data values from the text file at the bottom of this question.
The attached text data file contains an unknown quantity of integers whose values are between 100 and 200. Write a program that opens the file, counts how many values it contains, and creates a 1D array to store the values. Once the array is populated, calculate average and standard deviation. Also count how many values are between 100 and 119 (inclusive), 120 and 139 (inclusive), 140 and 159 (inclusive), 160 and 179 (inclusive), and 180 and 200 (inclusive). Output all the results (including the count of how many total values there are and the counts of how many values are in each range) to a separate text file.
Explanation / Answer
This is the program as per your specificatrions.
I recommend that you create example.txt i.e. input file.
It will create automatically output.txt file in which you will find all the output.
#include<iostream.h>
#include<fstream.h>
int main()
{
int count=0;
int ar[500];
int c1=0,c2=0,c3=0,c4=0,c5=0;
ifstream myReadFile;
ofstream myOutFile;
myReadFile.open("example.txt");
myOutFile.open("output.txt");
char output[100];
int n=0;
while (!myReadFile.eof())
{
myReadFile >> output;
// cout<<output << " "; this line you can remove cooments if you want the output on the screen.
n=(int) (output); // converting character to integer.
ar[count]=n;
if (n >=180)
{
c5++;
}
else if(n >=160)
c4++;
else if(n >=140)
c3++;
else if(n >=120)
c2++;
else
c1++;
count++;
}
myOutFile << c1 << c2 << c3 << c4 << c5 << count;
// here yoiu can print the array into the file if you wish in to the file.
myReadFile.close();
myOutFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.