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

in blue j The lab this week is to write a program that reads grades in the integ

ID: 3770305 • Letter: I

Question

in blue j

The lab this week is to write a program that reads grades in the integer range 1 – 100 from a file, writes out the grades, computes their average, and finds out how many grades are above average and how many grades are below average. All output should be written to the console window. The first number in the text document is the number of grades within the file, and the file will only have integers

Requirements: Write a method that calculates and then returns the average of the array of information. Write a method that determines how many are above the average and returns that number. Write a method that determines how many are below the average and returns that number. The main method should handle output (Methods to calculate the average and how many grades are above and below the average should return values, and not print out anything).

Explanation / Answer

This below script is working fine reading the grades from file and storing the data into array including
the size of the grades..written three functions for getAverage, getAboveAverage and getBelowAverage funtions
as per given in probelm statement. main mathod will handle all the outputs..see the below code with commnets
easy to understand.

Code:

#include <iostream>
#include <fstream>
using namespace std;

// Return the Average of grades .
double getAverage(int grade[], int size)
{
   double sum = 0, avg =0;
   for(int i=1; i<=size;i++)
       sum = sum + grade[i];
   avg = sum/size;
   return avg;
}

// Return the number of grades Above Average.
int getAboveAverage(int grade[], int size, double avg)
{
   int count = 0;
   for(int i =1; i<=size; i++)
       if(grade[i] >avg)
           count++;

   return count;
}

// Return the number of grades below Average.
int getBelowAverage(int grade[], int size,double avg)
{
   int count = 0;
   for(int i =1; i<=size; i++)
       if(grade[i] < avg)
           count++;
   return count;
}

int main()
{
   // Declaring File Pointer
   ifstream in_file;
   in_file.open("C:\Users\jnarasim\Desktop\quiz_grades.txt");
   // Checking the File is exit or not
   if(!in_file)
   {
       cout<<"file could not be found!";
   }
   // Declaring temporary Buffers
   int grade1[20], temp = 0, i =0, count = 0;
   int size = 0;
   double avg;
   // Reading data from file storing into the array
   while(!in_file.eof())
   {
           in_file>>size;
           grade1[i] = size;
           i++;
           count++;
   }

   // Assigning size of the grades to temp variable
   temp = grade1[0];
   cout<<"The Total Grades are : "<<temp<<endl;
   // Print the Grades
   cout<<"The Grades are : "<<endl;
   for(int i =1; i<count;i++)
       cout<<grade1[i]<<" ";

   cout<<endl;
   //Get the Avarage of all Grades
   avg = getAverage(grade1, temp);
   cout<<"Avarge of All grades is: "<<avg<<endl;
   //Get the Count for Above Avarage of Grades
   int temp2 = getAboveAverage(grade1,temp,avg);
   cout<<"Count for Above Average is : "<<temp2<<endl;
   //Get the Count for Below Avarage of Grades
   int temp3 = getBelowAverage(grade1,temp,avg);
   cout<<"Count for Below Average is : "<<temp3<<endl;

   // Closeing the File
   in_file.close();
   return 0;
}


Output: