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

You will be writing a program that reads a list of integers from a file, calcula

ID: 3758183 • Letter: Y

Question

You will be writing a program that reads a list of integers from a file, calculates the sum, average, and standard deviation of those integers, and then outputs the sum, average, and standard deviation to a second file.

Copy the following data file to your directory for input: lab7_input.txt

Use the loop shown in lecture to read from the file into an int array. You will need additional variables to keep track of the sum and of the number of integers in the file. The value (-1) will act as a sentinel and not be included in the array or calculations.

After you exit the input loop, your sum variable should have the overall sum and your counter variable should contain the number of integers in the file. That will give you the required information to compute the average. Make sure to use double division to compute the average.

Once you've calculated the average, next you'll calculate the standard deviation. Consider using a for loop and a double named 'numerator' that will be a running variable

Explanation / Answer

/**C++ program that reads an input file "lab7_input.txt"
file and calculates the sum, average, standard deviation values
and write to "lab7_output.txt" */
//files.cpp
//headerfiles
#include<iostream>
#include<iomanip> //header file for setw function
#include<fstream> //header file for ifstream, ofstream classes
using namespace std;

//set MAX size of array
#define MAX 100
int main()
{


   //set name of input file
   char fileName[20]="lab7_input.txt";
   //declare an array of size MAX
   int inputed[MAX];


   //open an input file stream
   ifstream fin;
   fin.open(fileName);


   //check if file exist
   if(!fin)
   {
       cout<<"File does not exist."<<endl;
       system("pause");
       return 0;
   }

   int count=0;
   int value;
   int sum=0;


   //read values from file until -1 is encountered
   while(fin>>value && value !=-1)
   {
       //store in array inputed
       inputed[count]=value;
       //add to sum
       sum+=value;
       //increment the count by one
       count++;
   }

   //calculate the average and standard deviation
   double avg=sum/count;
   double numerator=0 ;

   //calculate the numeration of standard deviation
   for(int i=0;i<count;i++)
       numerator += pow( (inputed[i] - avg), 2);


   double stdev ;
   stdev= sqrt(numerator/count);

   //close input file
   fin.close();


   //open an ouput file object
   ofstream fout;
   fout.open("lab7_output.txt");

   //check if file is opened
   if(!fout)
   {
       cout<<"File does not exist."<<endl;
       system("pause");
       return 0;
   }


   //write sum,average and standard deviation of integers
   fout<<setw(40)<<"Sum of Integers : "<<sum<<endl;
   fout<<setw(40)<<"Average of Integers : "<<avg<<endl;
   fout<<setw(40)<<"Standared deviation of Integers : "<<stdev<<endl;
  

   //close output file
   fout.close();

   system("pause");
   return 0;

}

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

Input file

lab7_input.txt

1 2 3 4 5 6 7 8 9 10

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

output file

lab7_output.txt

                      Sum of Integers : 55
                  Average of Integers : 5
      Standared deviation of Integers : 2.91548

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