In: Computer Science
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and
another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std.
#include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++ is stricter - it rquires it for exit using namespace std; int max(int a[],int dsize){ if (dsize>0){ int max=a[0]; for(int i=0;i<dsize;i++){ if(a[i]>max){ max = a[i]; } } return max; } return -9999999; } int min(int a[],int dsize){ if (dsize>0){ int min=a[0]; for(int i=0;i<dsize;i++){ if(a[i]<min){ min = a[i]; } } return min; } return -9999999; } void printArray(int array[],int dsize){ for(int i=0;i<dsize;i++){ cout << array[i]; if(i<dsize-1) //to avoid printing a space after the last one cout << " "; } cout << endl; } void fillArray(int array[],int &dsize, ifstream &fin){ int i=0; while(!fin.eof()){ fin >> array[i]; i++; } dsize =i; } int main(){ ifstream fin; ofstream fout; string ifilename="/home/ec2-user/environment/quizzes/numbers.txt"; int const asize=100; int a[asize]; int dsize=0; fin.open(ifilename.c_str()); //make sure you use the c_str the new g++ uses it if(!fin){ cout<< "Error opening input file"<<endl;exit(1);} fillArray(a,dsize,fin); printArray(a,dsize); cout <<"Min Value:" << min(a,dsize) << endl; cout <<"Max Value:" << max(a,dsize) << endl; return 0; }
Please look at my code and in case of indentation issues check the screenshots.
--------------quiz3.cpp---------------
#include <iostream>
#include <string>
#include <fstream>
#include <cmath> //this header file is used for sqrt()
#include <cstdlib> //new g++ is stricter - it
rquires it for exit
using namespace std;
int max(int a[], int dsize)
{
if (dsize > 0)
{
int max = a[0];
for (int i = 0; i < dsize;
i++)
{
if (a[i] >
max){
max = a[i];
}
}
return max;
}
return -9999999;
}
int min(int a[], int dsize)
{
if (dsize > 0)
{
int min = a[0];
for (int i = 0; i < dsize;
i++)
{
if (a[i] <
min){
min = a[i];
}
}
return min;
}
return -9999999;
}
void printArray(int array[], int dsize)
{
for (int i = 0; i < dsize; i++){
cout << array[i];
if (i < dsize - 1)
//to avoid printing a space after the last one
cout << "
";
}
cout << endl;
}
void fillArray(int array[], int &dsize, ifstream
&fin)
{
int i = 0;
while (!fin.eof()){
fin >> array[i];
i++;
}
dsize = i;
}
///////////////Updated code here/////////////////////////
//this function calculates mean of the array elements
double meanAvg(int array[], int arraySize)
{
double sum = 0;
//initialize sum to 0
for (int i = 0; i < arraySize; i++)
sum = sum + array[i];
//calculate sum of all
elements
double mean = sum/arraySize; //get the
mean by dividing sum by arraySize
return mean;
//returns
mean
}
//this function calculates standard deviation of arrayc
elements
double std_deviation(int array[], int arraySize)
{
double mean = meanAvg(array, arraySize);
//get the mean of array
//Variance = ∑(array[i] – mean)2 / n
double squareDiff = 0;
//get sum of squared differences from mean
for (int i = 0; i < arraySize; i++)
squareDiff = squareDiff + (array[i]
- mean)*(array[i] - mean);
double variance = squareDiff/arraySize; //calculate
variance
double std = sqrt(variance); //standard
deviation = sqrt of variance
return std;
//returns standard deviation
}
int main()
{
ifstream fin;
ofstream fout;
//string ifilename =
"/home/ec2-user/environment/bte320/numbers.txt";
string ifilename = "numbers.txt"; //You can comment
this line and use the above line
int const asize = 100;
int a[asize];
int dsize = 0;
fin.open(ifilename.c_str()); //make
sure you use the c_str the new g++ uses it
if (!fin)
{
cout << "Error opening input
file" << endl;
exit(1);
}
fillArray(a, dsize, fin);
printArray(a, dsize);
cout << "Min Value:" << min(a, dsize)
<< endl;
cout << "Max Value:" << max(a, dsize)
<< endl;
//Updated code from here------------
cout << "Mean :" << meanAvg(a, dsize)
<< endl;
cout << "Standard deviation :" <<
std_deviation(a, dsize) << endl;
ofstream outfile1("myquiz3outf.txt");
//open output files for writing
ofstream outfile2("myquiz3outc.txt");
outfile1 << "Min Value:" << min(a,
dsize) << endl; //write output to files
outfile1 << "Max Value:" << max(a, dsize)
<< endl;
outfile1 << "Mean :" << meanAvg(a, dsize)
<< endl;
outfile1 << "Standard deviation :" <<
std_deviation(a, dsize) << endl;
outfile2 << "Min Value:" << min(a,
dsize) << endl;
outfile2 << "Max Value:" << max(a, dsize)
<< endl;
outfile2 << "Mean :" << meanAvg(a, dsize)
<< endl;
outfile2 << "Standard deviation :" <<
std_deviation(a, dsize) << endl;
return 0;
}
----------Screenshots-----------------
----------Input--------------
----------Output--------------
--------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou