title: How to calculate the average of last 100 numbers of increasing quantity of numbers pool
body: <p>I have a data source.  It gives a number each minute.  I want to compute the average value of the last  100 numbers.  How do I do this with C?</p>
  
  <p>For example, the input is <code>1,2,3,4, ... 150</code> then I want to get the average value of <code>50 . . . 150</code>.</p>
  
  <p>One minute later, the number pool changes to <code>1,2,3,4.....150,151</code>, then I need to get the  average value of <code>51. . . 151</code>, as the concept is same,  get the last 100 numbers to calculate the average value.</p>
  
  <p>I have tried to use a list structure, first get get total sum of all the numbers ,then subtract the sum from the first number to <code>count-100</code> to get the last 100 numbers . </p>
  
  <p>Here is my code i have tried:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;malloc.h&gt;
  #define N 5
  
  int i,sum;
  
  int main(void)
  {
  struct node //定义一个链表
  {
      float num;  //链表的元素
      struct node *next;//下一个元素的指针
  };
  struct node *first=NULL;//第一个数据
  struct node *current=NULL;//当前的数据
  struct node *previous=NULL;//上一个数据
  
  struct node *currentT=NULL;//
  //    char temp='\0';
  
  while (1==1)//循环输入数据，相当于每秒往链表中添加一个数据
  {
      // printf("continue ?Y/N:  ");
      // scanf(" %c",&amp;temp);
  
      // if (tolower(temp)=='n')
      //  break;
      current=(struct node*) malloc(sizeof(struct node));//获取链表的首地址
      if (first==NULL)//如果第一个数据为空就把当前的地址赋值给first
          first=current;
      if (previous!=NULL)//把当前的地址赋值给上一个数据的next指针
          previous-&gt;next=current;
      printf("please enter the num:");
      scanf("%f",&amp;current-&gt;num);//输入数据
      current-&gt;next=NULL;//移动指针
      previous=current;
  
      currentT=first;//指针指向第一个数据
      float avg=0,sum=0,count=0;
      while (currentT!=NULL)//循环链表中所有的数据
      {
          printf("node's num is:%f \n",currentT-&gt;num);
          count=count+1;
          sum= sum+currentT-&gt;num;//求总和
          currentT=currentT-&gt;next;//指针下移
  
      }
      avg=sum/count;//求平均
  
      if(count&gt;N)//如果链表长度大于N则减去链表前端的数据
      {
          currentT=first;//指针指向第一个数据
          int remove_count=0;
          while (currentT!=NULL)//循环链表中所有的数据
          {
              remove_count=remove_count+1;
              sum= sum-currentT-&gt;num;//求总和
              if(remove_count==count-N){//减到链表长度等于N时停止
                  break;
              }
              currentT=currentT-&gt;next;//指针下移
          }
           avg=sum/N;//求平均
      }
  
      printf("sum is:%f \n",sum);
  
      printf("avg is:%f \n",avg);
  
  }
  
  return 0;
  }
  </code></pre>