Question

You will be given a file that will contain averages for classes which are divided into...

You will be given a file that will contain averages for classes which are divided into sections in the following format. The name of this file will be "grades.txt".

A 93
B 82
A 86
C 75
F 83

You are to write a program that computes the averages for the sections of classes, they are divided into groups that are denoted by a capital letter. You are to use arrays to calculate the average. I recommend using two arrays one to keep a sum of the averages and the other a counter for how many classes have been added to the sum.

The equation for calculating the average is:

(sum of numbers in list) / (number of numbers in list)

Given this you can have one array to keep a sum of the numbers that you are given and the other to keep a count of the number of classes in that section. The section names will be from A - Z and will always be capital letters. The averages will be from 0 - 100 inclusively.

Your program should output to the console the highest average section, lowest average section, and the average score across all of the sections. Output both the section name and the score for that section. To a file you will print the average for each section in alphabetical order. Always output the scores with two decimal places.

To the console:

HIGHEST AVERAGE:................A:98.00
LOWEST AVERAGE:.................H:73.00
AVERAGE SCORE:....................85.00

To a file named "results.txt":

A: 98.00
B: 93.00
C: 85.21
D: 95.12
.
.
.
Z: 78.25

in C++ please

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

Note: I only take few data in the input file...but this program can work if how many no of records in input file

Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

_________________
// grades.txt

A 93
B 82
A 86
C 75
F 83

___________________

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;

// Function Declarations
int findHighestAvg(double grades[], int cnt);
int findLowestAvg(double grades[],int cnt);
double calcGrade(double grade[],int cnt);
int main() {
  
  
   //Declaring variables
   int cnt=0;
ifstream dataIn;
char ch;
   double grade;
  
//Opening the input file
   dataIn.open("grades.txt");
  
   //checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the data from the file
while(dataIn>>ch>>grade)
{
   cnt++;
   }
dataIn.close();
  
// Creating array dynamically
char* groups = new char[cnt];
double* grades=new double[cnt];
  
   dataIn.open("grades.txt");
//Reading the data from the file
for(int i=0;i<cnt;i++)
{
dataIn>>ch>>grade;
groups[i]=ch;
grades[i]=grade;
   }
dataIn.close();
  
//calling the functions
int highIndx=findHighestAvg(grades,cnt);
int lowIndx=findLowestAvg(grades,cnt);
double avg=calcGrade(grades,cnt);
  
//Displaying the output
cout<<"HIGHEST AVERAGE:................"<<groups[highIndx]<<":"<<grades[highIndx]<<endl;

   cout<<"LOWEST AVERAGE:................"<<groups[lowIndx]<<":"<<grades[lowIndx]<<endl;
   cout<<"AVERAGE SCORE:...................."<<avg<<endl;
}
   return 0;
}
int findHighestAvg(double grades[],int cnt)
{
   double max=grades[0];
   int maxIndx=0;
  
   for(int i=0;i<cnt;i++)
   {
       if(max<grades[i])
       {
           max=grades[i];
           maxIndx=i;
       }
      
   }
   return maxIndx;
}
int findLowestAvg(double grades[],int cnt)
{
   double min=grades[0];
   int minIndx=0;
  
   for(int i=0;i<cnt;i++)
   {
       if(min>grades[i])
       {
           min=grades[i];
           minIndx=i;
       }
      
   }
   return minIndx;
}

double calcGrade(double grade[],int cnt)
{
   double sum=0;
   for(int i=0;i<cnt;i++)
   {
       sum+=grade[i];
   }
   return ((double)sum)/cnt;
}

______________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
You will be given a file that will contain averages for classes which are divided into...
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
  • Do it on (c) pls..... 1. Assume you have an input file with 6 integer values....

    Do it on (c) pls..... 1. Assume you have an input file with 6 integer values. Scan these values into an appropriate array. 2. Create a user defined function that takes your array as an input and order your array from lowest to highest. Ex. If you have an array called num, then num[0] should contain the smallest value while num[5] will contain the largest value. 3. Output the values between 50-75 into an output file called “results.txt” 4. Any...

  • The input file should contain (in order): the weight (a number greater than zero and less...

    The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values. The program should also write to an output file (rather than the console, as in Horstmann). So you should also prompt the user for the name of the output file, and then print your results to an output file...

  • You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questio...

    You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questions. The text file has 10 numbers. 1. You will scan the 10 values located in “sensor.txt” to an array of variable type double, using a loop of your choice. 2. You will create a user defined function that will take your array as an input and find the average value. (Ex. arr[] = {15, 12, 13, 10}, the sum will be 50 then...

  • I am confused on how to read valid lines from the input file. I am also...

    I am confused on how to read valid lines from the input file. I am also confused on how to count the averages from these valid input lines to keep running the total from these valid lines. If you could show an example of an input and output file that would be amazing as well. Purpose        Learn how to use Java input/output. Due Date       Per the Course at a Glance. Can be resubmitted. Submissions           In this order: printed copy of...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

    Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error. For...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • Please use pseudocode for this response, any additional advice for making this as easy to understand...

    Please use pseudocode for this response, any additional advice for making this as easy to understand as possible is greatly appreciated. Problem Statement Assume the Scores array is parallel to the Players array (both arrays are below). Scores array Scores[0] = 198 Scores[1] = 486 Scores[2] = 651 Scores[3] = 185 Scores[4] = 216 Scores[5] = 912 Scores[6] = 173 Scores[7] = 319 Scores[8] = 846 Scores[9] = 989 Players Array Players[0] = "Joe" Players[1] = "Ann" Players[2] = "Marty"...

  • Write a class that analyzes data you have stored in a text file. Your data represents...

    Write a class that analyzes data you have stored in a text file. Your data represents the ages of the individuals in your fictitious community. The data is stored, one per line, in a text file. For example, File name: myData.txt Contents: 45 18 6.5 3 5 sdtypo# 18 10 The number of lines in your text file must fall somewhere between 20 and 100 lines. Some of the lines must hold character strings that are not convertible to integers,...

  • Please have the function written in python, thank you! Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class P...

    Please have the function written in python, thank you! Preliminaries For this assignment you will be working with the following classes, which are available in the file homework5.classes.py: class Pharmacy: det init (selt, inventory, unit_prices) self.inventory -inventory self.unit_pricesunit_prices class Prescription: definit_(self, patient, drug_name, quantity): self.patient patient self.drug_name - drug_name self.quantity -quantity You will be asked to write several functions that work with the Prescription and Pharmacy classes. To complete this assignment you may write any helper functions you like inside...

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
ADVERTISEMENT