/*ogrenci.txt dosyasindan notlari okuyup ortalamasini hesaplar */
/*hesaplanan ortalamayi ogrenci.txt dosyasina yazdirir */

#include <stdio.h>
#include <stdlib.h>


int main()
{ 
   char name[ 30 ]; 
   int grade=0;  
   
      
   FILE *cfPtr; 
   
    if ( ( cfPtr = fopen( "students.txt", "r+" ) ) == NULL ) {
      printf( "File could not be opened\n" );
   } 
   
   
   else { 
      
   float average;
   int sum = 0; 
   
   int c = -1; 
           
      while ( !feof( cfPtr ) ) { 
         
         sum = sum + grade;    
         fscanf(cfPtr, "%s %d", name, &grade);
         c++;     
      } 

      average = (float) sum/c;
      
      fprintf(cfPtr, "average %0.2f", average);
      fprintf(stdout, "average = %0.2f\n", average);   //printf("average = %0.2f\n", average);
            
      fclose( cfPtr ); 
   } 
   

return 0; 

}
