C++ Program: Using Visual Studio The attached text data file contains an unknown
ID: 3767845 • Letter: C
Question
C++ Program: Using Visual Studio
The attached text data file contains an unknown quantity of positive integers. 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 the minimum value, maximum value, average, and standard deviation. Also count how many values are divisible by 5. Of those values, count how many are even and how many are odd. Output all the results (including the count of how many total values) to a separate text file.
The data file is called Data5c.txt and here is the link address to the data.
https://learn.vccs.edu/bbcswebdav/pid-82105464-dt-content-rid-76116912_2/xid-76116912_2
Here is the data:
Explanation / Answer
Oops. The file link is not opening. Its a password protected link it seems.
Writing separate functions for each and every operation you specified will increase the processing time, as the data file /array is huge enough. So, I'm combining all the operations in a single function to the extent possible. If you need any refinement, just get back to me.
Here is the logic for you.
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double Statistics(int numbers[], int count)
{
int min = numbers[0];
int max = numbers[0];
double sum = 0;
for(int i = 0; i < count; i++) //For each value in the array.
{
if(numbers[i] < min) //If necessary, update the minimum.
min = numbers[i];
if(numbers[i] > max) //If necessary, update the maximum.
max = numbers[i];
sum += numbers[i]; //Add the number to sum.
}
cout<<"The minimum of given numbers is: "<<min<<endl;
cout<<"The maximum of given numbers is: "<<max<<endl;
cout<<"The average of given numbers is: "<<sum / count<<endl;
return sum / count;
}
void StandardDeviation(int numbers[], int count, double average)
{
double stdDev = 0;
for(int i = 0; i < count; i++)
stdDev += pow((numbers[i] - average), 2); //Sum of (number - average) whole square.
stdDev = sqrt(1/(double)count * stdDev);
cout<<"The standard deviation of the given numbers is: "<<stdDev<<endl;
}
int main()
{
string fileName;
int numbers[2000]; //Declare an array of size 200.
int count = 0;
cout<<"Enter the name of the file: "; //Read the name of the file that has integers.
cin>>fileName;
ifstream file; //Open that file.
file.open(fileName);
while(!file.eof()) //As long the eof is not reached.
file>>numbers[count++]; //Read each number into the array, and increment the counter.
double average = Statistics(numbers, count); //Prints the minimum, maximum, and average of the elements, and returns average.
StandardDeviation(numbers, count, average); //Prints the Standard Deviation of the array.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.