HELP! with creating C program to read student names and get average of marks

+1 low dharm · September 28, 2014
Hey guys so i am pretty new to programming and was wondering if i could get some help.
My objective is to create a program that reads 10 students name first and last from .txt file from my pc and then have the user input a score for each student in the commmand prompt. After the input i need the program to calculate the averare mark. then lastly i need to create a similar program and have the program output the students name in alphabetical order with their test results.

so far i have done this and dont really no where to go from here.
Any hep would be much appreciated.

int main()
FILE * fPointer;
fPointer = fopen("list.txt", "r");
char singleLine[150]; 
int score1;
int score2;
int score3;
int score4;
int score5;
int score6;
int score7;
int score8;
int score9;
int score10;

int average;

while(!feof(fPointer)) { 
    fgets(singleLine, 150, fPointer); 
    printf("What score?\n");
    scanf(" %d", &score1);



Post a Reply


Oldest  Newest  Rating
+2 George Williams · September 28, 2014
I understand everything but the last block of code...can you fix the comments and code and place it in a code block(right under S with the dash)

Thanks. it will help a lot
0 low dharm · September 28, 2014
Just fixed it, so yea i realy dont know what to do here. At the present time the code runs and it displaying the first name and then requesting user input for the score then it follows by displaying the second name and then requesting user input for score etc until the 10th name. 

What i want to do is now collect those scores and get the total average of the scores (0-100).

And then present the names and scores in alphabetical order.

Please help guys i really need it and thanks in advance
0 low dharm · September 28, 2014
ok i have made it clearer
0 Franz Schmidt · September 29, 2014
First of all it doesn't make sense to make a extra variable for every user, therefor you could use an array.

And if you make an array it's piece of cake to calculate the average score:
for (int x = 0; x < n; x++){
average += score[x];
average /= n;

And if you want to sort a amount of values you could use the "Bubble-Sort" algorithm:
Therefor you need an array so you need to save the names of the file in a matrix e.g.: name[10][150] 
Then you just need to compair the first letter of every name in your algorithm:
if(name[x][0] > name[x + 1][0])

With the swap function (need to code that on your own) you have to swap the two names in your matrix.

After that you can print out the array in the orderd form

Btw: don't forget to swap the scores to if you wan't to print them out too.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator