title: Find the average of an unknown number of inputs
body: <p>I need to write a C program that calculates the average, using a function, of grades entered by the user. The number of grades entered is unknown so I have to use a sentinel value -1.</p>
  
  <p>This is my code and it compiles fine, however it doesn't tell me the average and the for loop doesn't call the function four times. What am I missing?</p>
  
  <pre><code> #include &lt;stdio.h&gt;
  
   float calculateAverage (int sumofGrades, int numberofGrades);
  
   int main(void) {
  
   int i;
   int sumofGrades = 0;
   int numberofGrades = 0;
   int grade = 0;
   float average;
  
   for (i = 1; i &lt;= 4; ++i) {
  
      calculateAverage(sumofGrades, numberofGrades);
  
   }
   return 0;
   }
  
  float calculateAverage (int sumofGrades, int numberofGrades) {
  
  float average;
  int grade;
  
  printf("Enter grades and -1 to stop: ");
  scanf("%d", &amp;grade);
  while (grade &lt;= 100 &amp;&amp; grade != -1) {
      numberofGrades++;
      sumofGrades += grade;
      average = sumofGrades / numberofGrades;
  
  }
  printf("The average is %.2f", average);
  
  return average;
  
  }
  </code></pre>
  
  <h3>Update</h3>
  
  <p>Thanks for the help! But I can't get it right yet. For the assignment I need the program to ask me 4 times to input the unknown number of grades and each time the average is calculated. </p>
  
  <p>Also, why doesn't it stop and gives me the average once I type -1?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  float calculateAverage(int sumofGrades, int numberofGrades);
  
  int main(void) {
      int i;
      int sumofGrades = 0;
      int numberofGrades = 0;
  
      for (i = 1; i &lt;= 4; ++i) {
          calculateAverage(sumofGrades, numberofGrades);
      }
      return 0;
  }
  
  float calculateAverage (int sumofGrades, int numberofGrades) {
  
      float average;
      int grade = 0;
  
      while (grade != -1 &amp;&amp; grade &gt;= 0 &amp;&amp; grade &lt;= 100) {
          printf("Enter grades and type -1 to stop: ");
          scanf("%d", &amp;grade);
  
          if (grade != -1) {
              numberofGrades++;
              sumofGrades += grade;
          }
      }
  
      average = sumofGrades / numberofGrades;
      printf("The average is %.2f\n", average);
  
      return average;
  }
  </code></pre>
  
  <h3>Update</h3>
  
  <p>I'm still having issues! I can't get the program to ask me to enter the grades four times and calculate the average each time!! I tried and I tried and it's so frustrating!! What am I doing wrong??</p>