title: How to use recursion on an array to calculate average?
body: <p>I have a program that stores a variety of information about 10 universities as structures in an array. I have a function named 'inputData' that loads the information of the universities into the array by converting them to structures. I am trying to write a function that will recursively sum up the tuition of all the universities and then return the average. But I keep running into an error somewhere down the line.</p>
  <p>This is the structure:</p>
  <hr />
  <pre><code>struct University
  {
      char name[50];
      char city[20];
      char state[3];
      int rank;
      int tuition;
  };
  </code></pre>
  <p>This is the function to convert info to a structure:</p>
  <pre><code>struct University inputData(char Name[50], char City[20], char State[3], int Rank, int Tuition){
      struct University uni;
      strcpy(uni.name, Name);
      strcpy(uni.city, City);
      strcpy(uni.state, State);
      uni.rank=Rank;
      uni.tuition=Tuition;
      return uni;
  };
  </code></pre>
  <p>Then I created a structure array and added all the info:</p>
  <pre><code>struct University university[10];
  university[0]= inputData(&quot;Princeton University&quot;, &quot;Princeton&quot;, &quot;NJ&quot;, 1, 45320);
  university[1]= inputData(&quot;University of Virginia&quot;, &quot;Charlottesville&quot;, &quot;VA&quot;, 24, 52040);
  university[2]= inputData(&quot;Boston College&quot;, &quot;Chestnut Hill&quot;, &quot;MA&quot;, 31, 51296);
  university[3]= inputData(&quot;Georgia Institute of Technology&quot;, &quot;Atlanta&quot;, &quot;GA&quot;, 34, 32404);
  university[4]= inputData(&quot;Lehigh University&quot;, &quot;Bethlehem&quot;, &quot;PA&quot;, 44, 48320);
  university[5]= inputData(&quot;University of Chicago&quot;, &quot;Chicago&quot;, &quot;IL&quot;, 3, 52491);
  university[6]= inputData(&quot;Duke University&quot;, &quot;Durham&quot;, &quot;NC&quot;, 8, 51265);
  university[7]= inputData(&quot;University of Georgia&quot;, &quot;Athens&quot;, &quot;GA&quot;, 56, 29844);
  university[8]= inputData(&quot;University of Denver&quot;, &quot;Denver&quot;, &quot;CO&quot;, 86, 46362);
  university[9]= inputData(&quot;Loyola University Chicago&quot;, &quot;Chicago&quot;, &quot;IL&quot;, 99, 26270);
  </code></pre>
  <p>My function to calculate the average tuition is (I pass in the array and its length):</p>
  <pre><code>int recursionAvg(struct University* list, int length){
      if (length&lt;=0){
      return 0;
  }
      if(length!=0) return (recursionAvg(list, length -1) + list[length - 1].tuition)/10; 
  };
  </code></pre>
  <p>I call it and print the result by doing:</p>
  <pre><code>average = recursionAvg(university, 10);
  printf(&quot;the average is: %d&quot;, average);
  </code></pre>
  <p>I'm supposed to get 43561 as the answer but I keep getting 3126. I realize that the part of the problem is because I keep dividing by 10 in every recursive call, but how would I do this only once within the function itself?</p>
  <p>The code runs but I get the wrong result.</p>