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.
side note: ***for the string ifilename, the folder name that the fils are under is "bte320" and the "numbers.txt" that it is referencing is pasted below***
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/bte320/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;
}
ARRAY FILE (numbers.txt):
55 9870 33 28 44 77 8756 312 2 78
Look at my code and in case of indentation issues check the screenshots.
---------main.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/////////////////////////
//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----------------
---------------------------------
Do comment if you need any clarification.
Please let me know if you are facing any issues.
Please Rate the answer if it helped.
Thankyou