In: Computer Science
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. a. For the file myquiz3outf.txt, you will use an ofstream. b. For the file myquiz3outc.txt, you will capture the cout output in a file using linux commands:./quiz3 > myquiz3outc.txt string ifilename="/home/ec2-user/environment/quizzes/numbers.txt" I've posted this question before but there was a mistake in the original file and I've been given different instructions I'll also have to save my outputs using terminals in cloud9, so any help or advice with that would be great! CODE: #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; }
OUTPUT:
55 9870 33 28 44 77 8756 312 2 78 Min Value:2 Max Value:9870 Average:1925.50 Std:3703.04
ARRAY FILE(numbers.txt):
55 9870 33 28 44 77 8756 312 2 78
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
code
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
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)
cout << " ";
}
cout << endl;
}
void fillArray(int array[], int &dsize, ifstream &fin)
{
int i = 0;
while (!fin.eof())
{
fin >> array[i];
i++;
}
dsize = i;
}
double meanAvg(int array[], int arraySize)
{
double sum = 0;
for (int i = 0; i < arraySize; i++)
sum = sum + array[i];
double mean = sum / arraySize;
return mean;
}
double std_deviation(int array[], int arraySize)
{
double mean = meanAvg(array, arraySize);
double squareDiff = 0;
for (int i = 0; i < arraySize; i++)
squareDiff = squareDiff + (array[i] - mean) * (array[i] - mean);
double variance = squareDiff / arraySize;
double std = sqrt(variance);
return std;
}
int main()
{
ifstream fin;
ofstream fout;
string ifilename = "numbers.txt";
int const asize = 100;
int a[asize];
int dsize = 0;
fin.open(ifilename.c_str());
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;
cout << "Mean :" << meanAvg(a, dsize) << endl;
cout << "Standard deviation :" << std_deviation(a, dsize) << endl;
ofstream outfile1("myquiz3outf.txt");
ofstream outfile2("myquiz3outc.txt");
outfile1 << "Min Value:" << min(a, dsize) << endl;
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;
}