The purpose of this program will be to read in data from a file and put it into
ID: 3659326 • Letter: T
Question
The purpose of this program will be to read in data from a file and put it into an Array and then work with the data that is in the Array. You will prompt the user to type in the filename. If that file does not exist, you will warn the user and stop the program. If the file exists, your program will read in the data from the file into a one dimensional array. The file will always have at least one integer and the file will contain only integers. Your array will be declared to hold a maximum of 25 elements of type int. Use a named constant to represent this maximum size. Once the array is declared (or as it is declared), you will zero it out. That is, you will set each element initially to zero. Your program must work for situations where there are less than 25 integers in the original data file, exactly 25 integers in the file, and more than 25 integers in the file, so be sure to use a counter variable that will keep up with how many integers are put into the array. Once the data is in the array you will first display all 25 elements of the array using a count controlled loop. Your program will then : display the sum of the integers in the array that were read in from the file display the average of the integers in the array that were read in from the file (be sure to use a data type that allows decimals) display the standard deviation of the integers in the array that were read in from the file (use a data type that allows decimals) display a random element from the array (do this before you sort) display the highest number of the integers in the array that were read in from the file display the lowest number of the integers in the array that were read in from the file and sort the values in the array from smallest to largest and then display the whole array (all 25 elements)Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#define constant 25
using namespace std;
int main()
{
int data[constant],i,j;
int counter;
int temp;
int randomIndex;
int max,min;
string filename;
float sum=0.0, mean, variance, standardDev;
ifstream fin;
cout<<" Enter the Filename with extension :";
cin>>filename;
fin.open(filename.c_str());
if (!fin)
{
cout << "I could not open the file.";
return EXIT_FAILURE;
}
/*Initialize the array with zero*/
for(i=0;i<constant;i++)
{
data[i]=0;
}
counter=0;
/*copy the data from file to array*/
while(counter<constant && !fin.eof())
{
fin>>data[counter];
counter++;
}
fin.close();
/*Print the array*/
cout<<" Elements of array : ";
for(i=0;i<constant;i++)
{
cout<<data[i]<<" ";
}
/*Sum of element of array*/
for(i=0;i<counter;i++)
{
sum=sum+data[i];
}
/*claculating mean*/
mean=sum/counter;
cout<<" Sum:"<<sum;
cout<<" Mean:"<<mean;
sum=0.0;
/*calculating variance and standard deviavtion*/
for(i=0;i<counter;i++)
{
sum=sum+((mean-data[i])*(mean-data[i]));
}
variance=sum/counter;
standardDev=sqrt(variance);
cout<<" Standard Deviation:"<<standardDev;
/*calculating random element*/
randomIndex=rand()%counter;
cout<<" Random Number in array:"<<data[randomIndex];
/*calculating maximun and minimun*/
max=data[0];
min=data[0];
for(i=1;i<counter;i++)
{
if(data[i]>max)
max=data[i];
if(data[i]<min)
min=data[i];
}
cout<<" Maximun number in array :"<<max;
cout<<" Minimun number in array :"<<min;
/*sorting the array*/
for(i=0;i<counter;i++)
{
for(j=i+1;j<counter;j++)
{
if(data[i]>data[j])
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
/*printing the array*/
cout<<" Array after the Sorting : ";
for(i=0;i<constant;i++)
{
cout<<data[i]<<" ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.