All pastes #57349 Raw Edit

Mine

public text v1 · immutable
#57349 ·published 2006-05-20 20:44 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STUDENTS 100
#define N 128
#define M 2

void get_data_from_user() {
  FILE *fp;
  char epitheto[N];
  char onoma[N];
  float grade;
  int i;

  fp = fopen("eisagwgi.txt", "w");
  if (fp == NULL) {
    fprintf(stderr, "Error opening file for output.\n");
    exit(1);
  }

  printf("Dwse to epitheto: ");
  scanf("%s", epitheto);

  while (strcmp(epitheto, "*")) {
    fprintf(fp, "%s ", epitheto);

    printf("Dwse onoma: ");
    scanf("%s", onoma);
    fprintf(fp, "%s ", onoma);

    for (i=0; i<M; i++) {
      printf("Dwse thn %di vathmologia: ", i+1);
      scanf("%f", &grade);
      fprintf(fp, "%f ", grade);
    }

    fprintf(fp, "\n");
    printf("Dwse to epitheto: ");
    scanf("%s", epitheto);
  }

  fclose(fp);
}

int extract_averages_from_file(float *av) {
  FILE *fp;
  char epitheto[N], name[N];
  float score1, score2;
  int cnt;

  fp = fopen("eisagwgi.txt", "r");
  if (fp == NULL) {
    fprintf(stderr, "Error opening file for input.\n");
    exit(1);
  }

  cnt = 0;
  while(fscanf(fp, "%s%s%f%f", epitheto, name, &score1, &score2) != EOF) {
    av[cnt++] = (score1 + score2)/2;
  }

  return cnt;
}

int main() {
  float av[MAX_STUDENTS];
  int i, total;

  get_data_from_user();
  total = extract_averages_from_file(av); 

  printf("%d\n", i);
  for (i=0; i<total; i++) {
    printf("%d mesos oros: %f\n", i+1, av[i]);
  }
  return 0;
}