how do I sort this structure???

This is a discussion on how do I sort this structure??? within the C Programming forums, part of the General Programming Boards category; Does anyone know what I am doing wrong with this code below, I am trying to sort my grades program. ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    how do I sort this structure???

    Does anyone know what I am doing wrong with this code below, I am trying to sort my grades program. I am trying to sort this structure?

    I am getting these error messages:
    errors:
    `student' undeclared (first use in this function)
    `grade' undeclared (first use in this function)
    `lettergrade' undeclared (first use in this function)


    Jose

    Code:
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10
    
    struct list {
           char student[MAXN][MAX];
           int grade[MAXN];
           char lettergrade[MAXN];
     };
    
    void display_grades(void);
    void sort_list (struct list a[], int n);
    
    int main (void)
      {
         display_grades();
         return 0;
      }
    
    
      void display_grades(void)
      {
         //open text file, read and display grades
         char ch;
         //char lettergrade[MAXN];
         FILE *myFile;
         //char student[MAXN][MAX];
         //int grade[MAXN];
         int i;
         int j;
    
         struct list values[MAX] = {
                {student[MAXN][MAX],grade[MAXN],lettergrade[MAXN]}};
    
         //open file and check for file error
         if (( myFile = fopen("c:\\temp\\grades.txt","r")) == NULL)
            {
             printf ("Error Opening with File.");
             scanf ("%c",&ch);
             return;
             }
    
         //display grades
        printf ("\n");
        printf ("\n");
        printf ("\n");
        printf (" ******************************\n");
        printf ("     NAME     GRADE                  \n");
        printf ("  -----------------------  \n");
        i=0;
    
         fscanf (myFile, "%s", student[0]);
               //...until last ending character in file
               while (student[i][0] != '@' && i < MAXN -1)
                     {
                     fscanf (myFile, "%i", &grade[i]);
                         //convert number grades to letter grades
                         if (grade[i] >= 90 && grade[i] <=100)
                             lettergrade[i] = 'A';
                         else if (grade[i] >= 80)
                             lettergrade[i] = 'B';
                         else if (grade[i] >= 70)
                            lettergrade[i] = 'C';
                         else if (grade[i] >= 60)
                            lettergrade[i] = 'D';
                         else
                            lettergrade[i] = 'F';
                         //end conversion
                     i++;
                            fscanf (myFile, "%s", student[i]);
                     }
    
                     int lastIx = 2, i;
                     //sort data
                     sort_list(values,lastIx);
    
                     for (j=0; j<i; j++)
                         {
                         //printf ("     %s      %c \n", student[j], lettergrade[j]);
                         printf ("    %s   %c \n", values[j].student, values[j].lettergrade);
                         }
                         fclose (myFile);
                         printf ("\n");
                         printf (" *******************************\n");
    
      //add extra lines
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("press 'ENTER' key to continue...\n");
      scanf ("%c",&ch);
      }
    
      void sort_list (struct list a[], int n)
           {
        int i,j;
        struct list temp;
               for (j = 1; j<=n; j++)
                      {
                   for (i = n; i>= j; i--)
                       if (a[i-1].student > a[i].student)
                       {
                       temp = a[i-1];
                       a[i-1] = a[i];
                       a[i] = temp;
                        }
    
                      }
           }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You apparently don't know how to use a structure. You're doing it all wrong:

    Code:
    struct list {
           char student[MAXN][MAX];
           int grade[MAXN];
           char lettergrade[MAXN];
     };
    This is right. You've defined how your structure will work, what it will contain as data.

    Code:
    struct list values[MAX] = {
                 {student[MAXN][MAX],grade[MAXN],lettergrade[MAXN]}
    What on earth is this supposed to be?

    Assuming you want an array of strucutres, just do:

    struct list values[MAX];

    There. Now you have an array of structures.

    Here's how you access the elements of one structure's elements:

    struct list myInstance;

    myInstance.student[x][y] = 10;
    myInstance.grade[z] = 4;

    Now an array of structures works similar to this:

    struct list values[MAX];

    values[x].student[y][z] = 10;

    Got it?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    Of course I don't understand how to do structures, that is why I am asking a question.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by jgonzales
    Of course I don't understand how to do structures, that is why I am asking a question.
    Well now you've got your answer.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Re: how do I sort this structure???

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX 40
    #define MAXN 10
    
    struct List {
      char name[MAX];
      double grade;
      char lettergrade[MAXN];
     };
    
    FILE * OpenFile ( char [], char []);
    int GetList (FILE *, struct List []);
    void SortList(struct List[], const int);
    void DisplayList (const struct List [], const int);
    
    
    int main (void)
    {
      struct List students[MAX] = { };
      int size;
    
      FILE * fptr = OpenFile("textfile","r");
      size = GetList(fptr, students);
      fclose(fptr);
    
      printf("Unsorted Records: \n");
      DisplayList (students, size);
      printf("\n\nSorted Records: \n");
      SortList(students,size);
      DisplayList(students,size);
      printf("\n\nYou Figure Out The Rest");
    
      return 0;
    }
    
    FILE * OpenFile(char filename[], char m [])
    {
      //open the file
      FILE *fptr = fopen(filename, m);
      //validate the file pointer
      if (fptr == NULL) exit(1);
    
      return fptr;
    }
    
    int GetList (FILE *fptr, struct List a[])
    {
      int count = 0;
      char buffer[256];
    
      //Take one line from the datfile and place it into a buffer
      while ( fgets(buffer, 256, fptr) != NULL )
      {
        //Extract data from buffer into structure array members
        sscanf(buffer, "%s%lf", a[count].name, &a[count].grade );
        count++;
      }
    
      //return number of records read
      return count;
    }
    
    void SortList(struct List a[], const int size)
    {
      int i, ii;
      struct List temp;
    
      for ( i = 0; i < size; i++)
      {
        for(ii = i+ 1; ii < size; ii++)
        {
          if( a[i].grade < a[ii].grade)
          {
            //switch structure indexes
            temp = a[i];
            a[i] = a[ii];
            a[ii] = temp;
          }
        }
      }
    }
    
    void DisplayList (const struct List a[], const int size)
    {
      int i;
      for( i = 0; i < size; i++)
        printf("%s %lf\n", a[i].name, a[i].grade);
    }

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Example datafile:

    name1 55.3
    name2 88.3
    name3 21.0

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    thanks for the code I really appreciate it, but I tried your code with my datafile and this was the output I received......

    .................................................. ....
    Unsorted Records:
    Jones 0.000000
    55 0.000000
    Brown 0.000000
    81 0.000000
    Smith 0.000000
    89 0.000000
    Alazar 0.000000
    93 0.000000
    Forster 0.000000
    63 0.000000
    Joplin 0.000000
    67 0.000000
    Grey 0.000000
    33 0.000000
    @ 0.000000
    0.000000
    0.000000


    Sorted Records:
    Jones 0.000000
    55 0.000000
    Brown 0.000000
    81 0.000000
    Smith 0.000000
    89 0.000000
    Alazar 0.000000
    93 0.000000
    Forster 0.000000
    63 0.000000
    Joplin 0.000000
    67 0.000000
    Grey 0.000000
    33 0.000000
    @ 0.000000
    0.000000
    0.000000


    You Figure Out The Rest

    .................................................. .............................


    Jose

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Show me what your datafile looks like before being processed. Just give me 4 or 5 lines of it.

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The way the code works is that it looks for a string than a double.

    Name grade
    Troll 44.5
    Star 34.2
    Wars 55.33

    If your datafile records are not set up this way than it will obviously not work. You'll have to make some small adjustments.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    I really appreciate your help.

    Honestly I am working on an assignment for a class I took this summer, this is the only part of the last assignment I couldn't figure out in time. The professor said he would give me full credit for the assignment because he knows I spent alot of time on it, but I want to make the program work the way he asks and send it to him anyway. The class is already over but I really want to figure out this program.


    this is my data file.....

    Jones
    55
    Brown
    81
    Smith
    89
    Alazar
    93
    Forster
    63
    Joplin
    67
    Grey
    33
    @

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay, I can see why there was a problem. There are inconsistant data in the sense that they are not logically organized into records. At least they could have been organized like this:

    Jones 55
    Brown 81
    ...

    I'll adapt the code. Give me half an hour or so.

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    #define MAX 40
    #define MAXN 10
    
    struct List {
      char name[MAX];
      int grade;
      char lettergrade[MAXN];
     };
    
    FILE * OpenFile ( char [], char []);
    int GetList (FILE *, struct List []);
    void SortList(struct List[], const int);
    void DisplayList (const struct List [], const int);
    
    
    int main (void)
    {
      struct List students[MAX] = { };
      int size;
    
      FILE * fptr = OpenFile("textfile","r");
      size = GetList(fptr, students);
      fclose(fptr);
    
      printf("Unsorted Records: \n");
      DisplayList (students, size);
      printf("\n\nSorted Records: \n");
      SortList(students,size);
      DisplayList(students,size);
      printf("\n\nYou Figure Out The Rest");
    
      return 0;
    }
    
    FILE * OpenFile(char filename[], char m [])
    {
      //open the file
      FILE *fptr = fopen(filename, m);
      //validate the file pointer
      if (fptr == NULL) exit(1);
    
      return fptr;
    }
    
    int GetList (FILE *fptr, struct List a[])
    {
      int count = 0;
      char buffer[256];
    
      //Take one line from the datfile and place it into a buffer
      while ( fgets(buffer, 256, fptr) != NULL )
      {
        if (!isdigit(buffer[0]))
        {
        //Extract data from buffer into structure array members
          sscanf(buffer, "%s", a[count].name);
          continue;
        }else
          sscanf(buffer, "%d", &a[count].grade );
        count++;
      }
    
      //return number of records read
      return count;
    }
    
    void SortList(struct List a[], const int size)
    {
      int i, ii;
      struct List temp;
    
      for ( i = 0; i < size; i++)
      {
        for(ii = i+ 1; ii < size; ii++)
        {
          if( a[i].grade < a[ii].grade)
          {
            //switch structure indexes
            temp = a[i];
            a[i] = a[ii];
            a[ii] = temp;
          }
        }
      }
    }
    
    void DisplayList (const struct List a[], const int size)
    {
      int i;
      for( i = 0; i < size; i++)
        printf("%s %d\n", a[i].name, a[i].grade);
    }
    Try that. Only a couple changes, however that datafile is problematic because it is not organized into records. I would not have used a structure at all had I known beforehand the organization of the datafile.
    Last edited by Troll_King; 08-21-2002 at 09:22 AM.

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Krickey!!! Ignor the top part. That isn't supposed to be there. Okay I fixed it.
    Last edited by Troll_King; 08-21-2002 at 09:22 AM.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    Thank you that sorted them by grade.

    Now I need to figure out how to incorporate your example into my code...I wrote my program different from you and had to also change the grades into a letter grade...I am a begginner at C programming and I can tell you are an advanced C programmer.


    Jose

    below is my code...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 10
    #define MAXN 10
    
    struct list {
           char name[MAXN][MAX];
           int grade[MAXN];
           char lettergrade[MAXN];
     };
    
    void display_grades(void);
    void sort_list (struct list a[], int n);
    
    int main (void)
      {
         display_grades();
         return 0;
      }
    
    
      void display_grades(void)
      {
         //open text file, read and display grades
         char ch;
         //char lettergrade[MAXN];
         FILE *myFile;
         //char student[MAXN][MAX];
         //int grade[MAXN];
         int i;
         int j;
    
         struct list students[MAX] = { };
    
         //open file and check for file error
         if (( myFile = fopen("c:\\temp\\grades.txt","r")) == NULL)
            {
             printf ("Error Opening with File.");
             scanf ("%c",&ch);
             return;
             }
    
         //display grades
        printf ("\n");
        printf ("\n");
        printf ("\n");
        printf (" ******************************\n");
        printf ("     NAME     GRADE                  \n");
        printf ("  -----------------------  \n");
        i=0;
    
         fscanf (myFile, "%s", students[0]);
               //...until last ending character in file
               while (students[i][0] != '@' && i < MAXN -1)
                     {
                     fscanf (myFile, "%i", &grade[i]);
                         //convert number grades to letter grades
                         if (grade[i] >= 90 && grade[i] <=100)
                             lettergrade[i] = 'A';
                         else if (grade[i] >= 80)
                             lettergrade[i] = 'B';
                         else if (grade[i] >= 70)
                            lettergrade[i] = 'C';
                         else if (grade[i] >= 60)
                            lettergrade[i] = 'D';
                         else
                            lettergrade[i] = 'F';
                         //end conversion
                     i++;
                            fscanf (myFile, "%s", students[i]);
                     }
    
                     int lastIx = 2, i;
                     //sort data
                     sort_list(values,lastIx);
    
                     for (j=0; j<i; j++)
                         {
                         //printf ("     %s      %c \n", student[j], lettergrade[j]);
                         printf ("    %s   %c \n", values[j].students, values[j].lettergrade);
                         }
                         fclose (myFile);
                         printf ("\n");
                         printf (" *******************************\n");
    
      //add extra lines
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("\n");
      printf ("press 'ENTER' key to continue...\n");
      scanf ("%c",&ch);
      }
    
      void sort_list (struct list a[], int n)
           {
        int i,j;
        struct list temp;
               for (j = 1; j<=n; j++)
                      {
                   for (i = n; i>= j; i--)
                       if (a[i-1].student > a[i].student)
                       {
                       temp = a[i-1];
                       a[i-1] = a[i];
                       a[i] = temp;
                        }
    
                      }
           }

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In my source when you display the grades, display the students name than before you display their grade, since you want to display a grade letter instead, than throw in your if/else condtions in there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:35 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 01:24 PM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21