title: How to calculate the average within an array of struct?
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  #include &lt;string&gt;
  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&amp;);
  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&amp; myfile)
  {
      int num_students;
      for (int i = 0; i &lt; 5; i++) {
          myfile &gt;&gt; statistics_array[i].semester_name;
          myfile &gt;&gt; statistics_array[i].year;
          myfile &gt;&gt; num_students;
          statistics_array[i].num_grades=num_students;
          for (int j = 0; i &lt; num_students; i++) {
              myfile &gt;&gt; statistics_array[j].grades_array[i];
          }
      }
  }
  
  void calculate_statistics(ExamType statistics_array[])
  {
      for (int index = 0; index &lt; 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 &lt; statistics_array[index].num_grades; i++)
      {
        statistics_array[index].grades_array+sum;
      }
  
  }
  </code></pre>