1. ## Fscanning Results

Im writiing a program that will load in a outside txt file of exams results for candidates in the form
Name rslt1 rslt2 reslt 3 rslt 4
with there being up to 40 candidates and a min of 4.
program then must calculate mean score for exm standard deviation for each exm , and each candidates avrg, eg
Exm1 exm2 ex3 Exm4 Avrg
Name rslt rslt rslt rslt avrg rslt
name " " " " "
Mean eavrg eavrg eavrg eavrg
S.D Esd esd esd esd.
im thinking of creating a structure for putting each candidates reuslts in and then making a array of structures. But im not sure how to make this work for a variable number of candidates and how to also display the above table. im guessing by making my arary of structs bigger to add on the avrgs and assging a value for the avrgs to part of the array?
Any suggestions would be greatly apprciated

2. To make it work for a variable number of candidates do you mean like a random number? If so you could just dynamically allocate memory for structures. If you use an array of say 10 structures that limits the amount of candidates. Like have 1 structure, and once your done reading into it, allocate memory for another with malloc and so on. To print them I would make a function that does just that, prints the values of the variables in the structure. Goodluck.

3. Just did a first attempt at coding the reading in and storing in the array, but when i attempt to display the array i just a number or if i ask for [0]'s value which should be the first name i also get back a number , i tried changing the %d in the printf at the end to %c but then i just get back one letter.
heres the code
Code:
#include <stdio.h>

typedef struct
{
char Name[20];
int Exm1;
int Exm2;
int Exm3;
int Exm4;
} data;

int main (void)
{     FILE *my_in, *my_out;
int i;
data Dat[41];

if ((my_in = fopen ("datafile.txt","rt")) == NULL)
{
printf("\nError opening input file, program exiting\n");
return(1);
}

for(i=0; i<41; i++){
while (fscanf(my_in, "%c %d %d %d %d",&Dat[i].Name,&Dat[i].Exm1,&Dat[i].Exm2,&Dat[i].Exm3,&Dat[i].Exm4) !=EOF)
{
printf(".");
}
}
printf("\n%d",Dat [0]);

return(0);

}

4. >>fscanf(my_in, "%c %d %d %d %d",&Dat[i].Name
.Name is a string, not a char as you're trying to get it, change it to %s and remove the & operator from Dat[i].Name. And check out the returf value of the fscanf() function, instead EOF, you should pass the number of parameters you're trying to get, in this case 5.

5. it doesnt seem to store things propely, changing to all for the string allows it a print a name but only the last name its read and the int values read in for the exm results are wrong

6. you don't need to change all the things, change just the name, from %c to %s. Post your last code here.

7. something like this?
Code:
#include <stdio.h>

typedef struct
{
char Name[20];
int Exm1;
int Exm2;
int Exm3;
int Exm4;
} data;

int main (void)
{     FILE *my_in, *my_out;
int i, j;
data Dat[41];

if ((my_in = fopen ("datafile.txt","rt")) == NULL)
{
printf("\nError opening input file, program exiting\n");
return(1);
}

for ( i = 0 ; i < 41 ; i++ ) {
if ( fscanf(my_in, "%s %d %d %d %d", Dat[i].Name, &Dat[i].Exm1, &Dat[i].Exm2, &Dat[i].Exm3, &Dat[i].Exm4) == EOF) break;
}

{
printf(".");
}

printf("\n%d",Dat);

return(0);

}

8. Can anyone suggest how to do this? I cant get it to scan the results in and display them correctly and thats just the start! i can prob figure how to calc the means etc when they are stored in the array, but how can i input these calc values back into the array so i can display everything in one array so it displays as a nice table.sorry to sound desperate

9. Thanks a lot, now to do the calcs, decided to do the figures being displayed using printf above and below the array. ok basically to caculate the avrg of a canddates score i have no idea but to caculate the mean mark in each exam am i right in thinking
Code:
sum += Exm1;
count = i;
avrg = sum/count
would this be ok ?