#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define FILE_NAME 20
    #define LIST_SIZE 50

    typedef struct
    {
    char *name;
    int score;
    }RECORD;

    float calMean(RECORD list[], int count);
    void sortData(RECORD list[], int count);

    int main (void)
{
       // Declarations
         float mean;
         float median;
         FILE *fp;
           char fileName[FILE_NAME];
           RECORD list[LIST_SIZE];
           char buffer[100];
           int count = 0;
           int i;
       // Statements
           printf("Enter the file name: ");
           gets(fileName);
           fp = fopen(fileName, "r");
           if(fp == NULL)
           printf("Error cannot open the file!\n");
           while(fgets(buffer, 100, fp) != NULL)
           {
             if( count >= LIST_SIZE)
             {
                printf("Only the first 50 data will be read!\n");
                 break;
             }
             if( count < LIST_SIZE)
            {
                list[count].name = (char*) malloc(strlen(buffer)*sizeof(char));
                   sscanf(buffer,"%[^,], %d", list[count].name, &list[count].score);
                  printf("name is %s and score is %d\n", list[count].name, list[count].score);
                  count++;
            }
                for( i =0; i < (LIST_SIZE - count); i++)
            {
                list[count + i].name = 0;
                list[count + i].score = 0;
            }
         }
           printf("Read in %d data records\n", count);
           mean = calMean(list, count);
         sortData(list, count);
          // median = calMedian(list, count);
           printf("%2.2f\n", mean);
           fclose(fp);
           return 0;
}

float calMean(RECORD list[], int count)
{
       float tempMean;
        int sum = 0;
        int i;
        for(i = 0; i < count; i++)
        sum += list[i].score;
        tempMean = (float) sum/count;
        return tempMean;
}

void sortData(RECORD list[], int count)
{
    int temp;
    int current;
    int walker;
    for(current = 0; current  < count+1; current++)
    {
       for( walker = count; walker > current; walker--)
          if(list[walker].score < list[walker -1].score)
          {
             temp = list[walker].score;
             list[walker].score = list[walker -1].score;
             list[walker -1].score = temp;
          }
          printf("%d\n", list[current].score);
    }
    return;
}

this code is working but for sort fucntion somehow i got an extra zero printout at the beginning when trying to printout the sorted list[current].score
so the output is like this:

Enter the file name: in.txt
name is Ada Lovelace and score is 66
name is Linus Torvalds and score is 75
name is Peter Norton and score is 82
name is Ken Thompson and score is 82
name is Steve Wozniak and score is 79
name is Marc Andreessen and score is 60
name is Donald Knuth and score is 60
name is Adele Goldberg and score is 71
name is Grace Hopper and score is 82
name is Bill Joy and score is 91
name is Andrew Tanenbaum and score is 71
name is Brian Kernighan and score is 72
Read in 12 data records
0
60
60
66
71
71
72
75
79
82
82
82
91
74.25
Press any key to continue . . .

there is an extra zero after the line read in 12 records
how can i fix this sort fucntion?

thank you I solved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.