简体   繁体   中英

How to calculate the average within an array of struct?

So my question has to deal with the calculate average function in my code. The function takes in the statistics_array and a semester_index. This function loops through the grades_array in the struct indicated by the semester_index and sums all the grades. This sum is divided by the no_of _grades in the member struct (indexed by semester_index) to compute the average grade for that semester's final exam. Finally, this average is stored in the average_grade member of the struct indexed by the semester_index.

The problem is how do I do this is my code I created a variable name sum to add on to this but that method is not working how do I fix this error my code is below.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct ExamType {
    string semester_name;
    int year;
    int num_grades;
    float average_grade;
    int grades_above_average;
    int grades_below_average;
    int grades_array[35];
};

void input_data(ExamType[], ifstream&);
void calculate_statistics(ExamType[]);
void print_results(ExamType[]);
void calculate_average(ExamType[], int);
void calculate_above_average(ExamType[], int);
void calculate_below_average(ExamType[], int);


int main()
{
    char make_type;
    ifstream myfile;
    ExamType statistics_array[5];
    myfile.open("infile.txt");
    input_data(statistics_array,myfile);
}

void input_data(ExamType statistics_array[], ifstream& myfile)
{
    int num_students;
    for (int i = 0; i < 5; i++) {
        myfile >> statistics_array[i].semester_name;
        myfile >> statistics_array[i].year;
        myfile >> num_students;
        statistics_array[i].num_grades=num_students;
        for (int j = 0; i < num_students; i++) {
            myfile >> statistics_array[j].grades_array[i];
        }
    }
}

void calculate_statistics(ExamType statistics_array[])
{
    for (int index = 0; index < 5; index++) {
        calculate_average(statistics_array, index);
        calculate_above_average(statistics_array, index);
        calculate_below_average(statistics_array, index);
    }
}

void calculate_average(ExamType statistics_array[], int index)
{
    int sum = 0;
    for (int i = 0; i < statistics_array[index].num_grades; i++)
    {
      statistics_array[index].grades_array+sum;
    }

}
statistics_array[index].grades_array+sum;

This line does nothing to any of your variables. You're basically saying:

void foo(){
    /*some stuff*/
    1+1;
}

See what I'm getting at? You're not assigning the value calculated anywhere, so the outcome of the aforementioned line is not stored anywhere.

To fix this, change the line into

sum += statistics_array[index].grades_array;

or

sum = sum + statistics_array[index].grades_array;

But this is not the only error, since grades_array is an array, and you are trying to add it to a normal int. So you have to specify an index within grades_array.

sum += statistics_array[index].grades_array[another_index];

Modify your function like this to calculate the average and save it to average_grade .

void calculate_average(ExamType statistics_array[], int index) {
    float sum = 0;
    int n = statistics_array[index].num_grades; // total no. of grades
    for (int i = 0; i < n; i++) {
        sum += statistics_array[index].grades_array[i]; // summing all grades
    }
    statistics_array[index].average_grade = sum/n; // average = total_sum / (num_of_grades)
}

Note: I recommend using float for sum as the average is a float, so that the calculation will result in a float value, or typecast it.

 for (int i = 0; i < statistics_array[index].num_grades; i++) { statistics_array[index].grades_array + sum; } 

You are adding statistics_array[index].grades_array to sum , but you aren't storing the value anywhere. Judging from your question, I'm assuming that you need to increment statistics_array[index].grades_array to sum at every iteration of the for-loop:

for (int i = 0; i < statistics_array[index].num_grades; i++)
{
    sum += statistics_array[index].grades_array[i];
}

While all these solutions using a for-loop work, I suggest to use one of the standard algorithms (albeit in the <numeric> header) instead: std::accumulate .

With its help you can rewrite calculate_average as

void calculate_average(ExamType statistics_array[], int index) {
    auto const n = statistics_array[index].num_grades;
    auto const sum
        = std::accumulate(std::cbegin(statistics_array[index].grades_array),
                          std::cend(statistics_array[index].grades_array),
                          0);

    statistics_array[index].average_grade = static_cast<float>(sum)/n;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM