Question

Im trying to get my program to output the Min, Max, and Average grades in main...

Im trying to get my program to output the Min, Max, and Average grades in main but im stuck on where it should be called. The code currently is :

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

//Read data from file and count into groups array

void GetDataFromFile(ifstream& inData, double array1[])

{

int i = 0;

while (inData >> array1[i])

{

inData >> array1[i];

i++;

}

inData.close;

}

//Display group counts

void DisplayDistribution(ifstream& inData, int groups[], string groupDesc[])

{

int processData;

while (inData >> processData)

{

cout << processData << endl;

if (processData >= 0 && processData <= 59)

{

groups[0]++;

}

if (processData >= 60 && processData <= 69)

{

groups[1]++;

}

if (processData >= 70 && processData <= 79)

{

groups[2]++;

}

if (processData >= 80 && processData <= 89)

{

groups[3]++;

}

if (processData >= 90 && processData <= 100)

groups[4]++;

}

inData.close();

cout << "****Grade Distribution ****" << endl;

for (int i = 0; i <= 4; i++)

{

cout << groupDesc[i] << "; " << groups[i] << endl;

{

cout << endl;

}

}

}

//Calculate Average Grades

double CalculateResults(double a[], double n)

{

// Find the sum of the array element

double sum = 0;

int countData = 0;

double min, max;

for (int i = 0; i<n; i++)

sum += a[i];

if (countData ==1)

{

min = n;

}

cout << "Minimum Grade: " << " Maximum Grade: " << fixed << setw(30) << "Average: " << CalculateResults << endl;

//Average to return

return sum / n;

}

int main()

{

int groups[5] = { 0,0,0,0,0 }; //group counts

ifstream inData; //stream file for data

double array1[20];

string groupDesc[5] = { "0~59", "60~69", "70~79", "80~89", "90~100" };

double n;

inData.open("input.txt");

if (inData)

{

cout << "Process file..." << endl;

cout << fixed << showpoint << setprecision(1); //decimal precision

GetDataFromFile(inData, array1); // Function Calls

DisplayDistribution(inData, groups, groupDesc);

}

else

{

cout << "Error Reading File" << endl;

}

system("pause");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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

//Read data from file and count into groups array
void GetDataFromFile(ifstream& inData, double array1[])
{
   int i = 0;
   while (inData && (i < 20))
   {
       inData >> array1[i];
       i++;
   }

}

//Display group counts
void DisplayDistribution(double inData[], int groups[], string groupDesc[])
{  
   double processData;
     for(int i=0; i< 20;i++)
   {
   processData = inData[i];   
       cout << processData << endl;
       if (processData >= 0 && processData <= 59)
       {
           groups[0]++;
       }else if(processData >= 60 && processData <= 69)
       {
           groups[1]++;
       }else if(processData >= 70 && processData <= 79)
       {
           groups[2]++;
       }else if (processData >= 80 && processData <= 89)
       {
           groups[3]++;
       }else if(processData >= 90 && processData <= 100)
       {
           groups[4]++;
       }
   }
  
   cout << "****Grade Distribution ****" << endl;
   for (int i = 0; i <= 4; i++)
   {
       cout << groupDesc[i] << "; " << groups[i] << endl;
   }
  
   cout<<endl;
}

//Calculate Average, Maximum and Minimum Grades
// n should be int since it is size of the array
void CalculateResults(double a[], int n)
{
   // Find the sum of the array element
   double sum = 0;
   int countData = 0;
   double min, max;
   // loop to calculate the sum of elements of a, maximum and minimum element of a
   for (int i = 0; i<n; i++)
   {
       sum += a[i];
       if(i == 0) // if first value, set max and min to this values
       {
           min = a[i];
           max = a[i];
       }else
       {
           // determine and update max, and min
           if(a[i] < min)
               min = a[i];
           if(a[i] > max)
               max = a[i];
       }  
   }
   // calculate average
   double avg = sum/n;
   // display the result  
   cout << "Minimum Grade: " <<min<<" Maximum Grade: " <<max<<" Average: " << avg << endl;
}

int main()
{
   int groups[5] = { 0,0,0,0,0 }; //group counts
   ifstream inData; //stream file for data
   double array1[20];
   string groupDesc[5] = { "0~59", "60~69", "70~79", "80~89", "90~100" };
   inData.open("input.txt");
  
   if (inData)
   {
       cout << "Process file..." << endl;
       cout << fixed << showpoint << setprecision(1); //decimal precision
       GetDataFromFile(inData, array1); // Function Calls
       DisplayDistribution(array1, groups, groupDesc);
       CalculateResults(array1,20); // call CalculateResults to display the maximum, minimum and average grade
       inData.close(); // close the file, do not close the file in any of the function
   }else
   {
       cout << "Error Reading File" << endl;
   }
  
   system("pause");
   return 0;
}      

Add a comment
Know the answer?
Add Answer to:
Im trying to get my program to output the Min, Max, and Average grades in main...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT