GustavoValim

aula de manipulação de arquivos

Apr 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define MAX 40
  4.  
  5. int main()
  6. {
  7.  
  8.     FILE *arq0;
  9.  
  10.     if((arq0 = fopen("notas.txt", "w")) == NULL)
  11.     {
  12.         printf("ERRO\n");
  13.         exit(0);
  14.     }
  15.  
  16.     int i;
  17.     for(i = 1; i <= MAX; i++)
  18.     {
  19.         int a = rand() % 10;
  20.         fprintf(arq0, "%d\n", a);
  21.     }
  22.  
  23.     fclose(arq0);
  24.  
  25.  
  26.     FILE *arq;
  27.     int soma = 0;
  28.  
  29.  
  30.     if((arq = fopen("notas.txt", "r")) == NULL)
  31.     {
  32.         printf("ERRO\n");
  33.         exit(0);
  34.     }
  35.  
  36.  
  37.     for(i = 1; i <= MAX; i++)
  38.     {
  39.         int a;
  40.         fscanf(arq, "%d", &a);
  41.         soma += a;
  42.     }
  43.  
  44.  
  45.     float media = soma / (i - 1);
  46.     printf("MEDIA %.2f", media);
  47.  
  48.  
  49.     fclose(arq);
  50.  
  51.  
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment