Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ progam Using the provided text file, write a program that opens the file, co

ID: 3715219 • Letter: C

Question

c++ progam

Using the provided text file, write a program that opens the file, counts how many real numbers it contains, and creates a dynamic array based on the count. Copy all the values into the array. Using the statistics library and other calculations that you may need, calculate and write the following into an output text file: total number of values, maximum, minimum, mean, median (sort the array first), standard deviation, count of how many values are within +/- 2% of the mean. Also print to the output file the first 10 sorted values of the array and the last 10 sorted values of the array. Your output file must have descriptions of all the content.

the data file is

Explanation / Answer

Here is the code for your question. Completed all the requirements, and defined methods for helping the process of finding statistics about the data. Before running the code, replace the file name in main method with your file name. Thanks.

//code.cpp

#include<iostream>

#include<fstream>

#include <iomanip>

#include<math.h>

using namespace std;

//method to find the maximum value in a double array of given size

double findMaximum(double array[], int size){

                double max;

                for(int i=0;i<size;i++){

                                if(i==0){

                                                max=array[i];

                                }else if(array[i]>max){

                                                max=array[i];

                                }

                }

                return max;

}

//method to find the minimum value in a double array of given size

double findMinimum(double array[], int size){

                double min;

                for(int i=0;i<size;i++){

                                if(i==0){

                                                min=array[i];

                                }else if(array[i]<min){

                                                min=array[i];

                                }

                }

                return min;

}

//method to find the mean value in a double array of given size

double findMean(double array[], int size){

                double total;

                for(int i=0;i<size;i++){

                                total+=array[i];

                }

                double mean=(double)total/size;

                return mean;

}

//method to sort a double array of given size in ascending order

//used bubble sort algorithm

void sort(double array[], int size){

                for(int i=0;i<size;i++){

                                for(int j=0;j<size-1;j++){

                                                if(array[j]>array[j+1]){

                                                                double tmp=array[j];

                                                                array[j]=array[j+1];

                                                                array[j+1]=tmp;

                                                }

                                }

                }

}

//method to find the median value in a double array of given size

double findMedian(double array[], int size){

                sort(array,size);//sorting the array first

                int middleIndex=size/2;

                double median=array[middleIndex];

                return median;

}

//method to find the number of values in a double array which are within

// +/- 2% of the mean value

int countValuesNearMean(double array[], int size){

                double mean=findMean(array,size);//mean value

                double tolerance=2.0;

                double min=mean-mean*(tolerance/100.0);

                double max=mean+mean*(tolerance/100.0);

                int count=0;

                for(int i=0;i<size;i++){

                                if(array[i]>=min && array[i]<=max){

                                                count++;

                                }

                }

                return count;

}

//method to find the standard deviation value in a double array of given size

double findStandardDeviation(double array[], int size){

                double sd=0;

                double delta;//difference between element and the mean value

                double sumOfDeltaSquare=0; //sum of squares of all differences

                double mean=findMean(array,size);//mean value

                for(int i=0;i<size;i++){

                                delta=abs(array[i]-mean);

                                sumOfDeltaSquare+=delta*delta;

                }

                //dividing sumOfDeltaSquare by count minus 1

                double sumOfDeltaSquareByCountMinusOne=sumOfDeltaSquare/(size-1);

                //square root of this value will be the standard deviation

                sd=sqrt(sumOfDeltaSquareByCountMinusOne);

                return sd;

}

int main(){

                char fileName[]="numberdata.txt";//input file name, replace it with your file name

                char outputFileName[]="output.txt";//output file name

                ifstream inFile(fileName);

                if(!inFile){

                                //input file not found or not readable

                                cout<<"Input file not found"<<endl;

                                return 1;

                }

                //array pointer

                double *array;

                int count=0;

                double number;

                //counting the number of elements in the input file

                while(inFile>>number){

                                count++;

                }

                //defining a dynamic array with the calculated count

                array=new double[count];

                //resetting the input file pointer to start position

                inFile.clear();

                inFile.seekg(0,ios::beg);

                int i=0;

                //looping through the file and adding data to the array

                while(inFile>>number){

                                array[i]=number;

                                i++;

                }

                //closing input file

                inFile.close();

                //opening output file

                ofstream outFile(outputFileName);

                //setting precision to 4 digits after decimal point

                outFile<<fixed;

                outFile<<setprecision(4);

                //appending all the stats to the output file

                outFile<<"Total number of values: "<<count<<endl;

                outFile<<"Maximum value: "<<findMaximum(array,count)<<endl;

                outFile<<"Minimum value: "<<findMinimum(array,count)<<endl;

                outFile<<"Mean value: "<<findMean(array,count)<<endl;

                outFile<<"Median value: "<<findMedian(array,count)<<endl;

                outFile<<"Standard deviation: "<<findStandardDeviation(array,count)<<endl;

                outFile<<"Count of values within +/- 2% of the mean: "<<countValuesNearMean(array,count)<<endl;

                //sorting the array

                sort(array,count);

                outFile<<"First 10 sorted values: "<<endl;

                for(int i=0;i<10;i++){

                                outFile<<array[i]<<" ";

                }

                outFile<<" Last 10 sorted values: "<<endl;

                for(int i=count-10;i<count;i++){

                                outFile<<array[i]<<" ";

                }

                outFile<<endl;

                outFile.close();

                cout<<"Stats have been written to the output file!"<<endl;

                //freeing the memory

                delete[] array;

                return 0;

}

/*OUTPUT*/

Stats have been written to the output file!

--------------------------------

Process exited after 2.028 seconds with return value 0

Press any key to continue . . .

/*output.txt after running the program*/

Total number of values: 1550

Maximum value: 1999.4404

Minimum value: 1000.2128

Mean value: 1493.8749

Median value: 1483.4613

Standard deviation: 292.7480

Count of values within +/- 2% of the mean: 84

First 10 sorted values:

1000.2128 1000.8782 1001.9696 1003.7141 1003.9013 1004.1051 1004.2168 1004.2698 1004.6345 1005.2995

Last 10 sorted values:

1993.4945 1993.6777 1993.7584 1994.9161 1995.2260 1996.4318 1997.3115 1998.3024 1998.3716 1999.4404

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote