Storing values from a file into array and sorting them

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2630 Views - Last Post: 11 August 2012 - 10:45 AM Rate Topic: -----

#1 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Storing values from a file into array and sorting them

Posted 09 August 2012 - 01:53 PM

I've a file which contains names and grades of students, and I'd like to write a program which can sort their grades (like midterm 1,midterm 2) according to user choice. I wrote as far as the choice part and opening the file, yet I don't know how to make program read only certain part of the file (like only Midterm 1 grades for example) and sort them only,then need to define a structure named “student”(so I can store name,surname and scores of each student under the same variable name)At the end, all data in the file needs to be stored in an array of new data type named “student”. Here's what I've wrote so far;

int main() {
    int choice,studentnumber,midterm1,midterm2,midterm3;
    char name[30];
    char surname[30];
    FILE *cfPtr;

    if ((cfPtr = fopen("grades.txt", "r")) == NULL)
        printf("Dosya açılamadı.\n");
    else {
        printf("%s%s%s%s\n", "StudentNumber", "Name", "Surname", "Midterm1", "Midterm2",    "Midterm3");
        fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);

        while (!feof(cfPtr)) {
            printf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);
            fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);

        } // while
    printf("What would you like to do? \n"
    "1- Sort according to midterm 1\n"    
    "2- Sort according to midterm 2\n"    
    "3- Sort according to midterm 3\n"    
    "4- Exit\n"
    scanf("%d",&choice);

    while (choice != 4);{
        fprintf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);

        switch(choice) {
            fclose(cfPtr);
        } // switch

        system("PAUSE");    
        return 0;
    } // scope which starts after while


After those, I have absolutely no idea what to do. Any ideas to give me? Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Storing values from a file into array and sorting them

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Storing values from a file into array and sorting them

Posted 09 August 2012 - 02:46 PM

You could create the structure to hold the student records/data first. Then create an array of student records. Afterwards load the data to the array and then print the data to test it is working ok.
Was This Post Helpful? 0
  • +
  • -

#3 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Re: Storing values from a file into array and sorting them

Posted 10 August 2012 - 08:44 AM

My problem is, I really don't know how to read the records into an array. I started with the first part;
 int choice,studentnumber,midterm1,midterm2,midterm3;
    char name[30];
    char surname[30];
    FILE *cfPtr;



which was actually meant to be struct yet failed. Any guidence with that? Sorry I'm a starter, and a really bad one :D. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Storing values from a file into array and sorting them

Posted 10 August 2012 - 02:56 PM

You can use an index variable to increment through the array.


  const int STUDENTSMAX = 100;

  // create array of students
  Student students[STUDENTSMAX];

  int i = 0;
  while (!feof(cfPtr)) 
  {
    // read data from file
    fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name, 
           &students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);

    // print
    printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name, 
            students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);

    // increment array index
    i++;
  } // while



Was This Post Helpful? 0
  • +
  • -

#5 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 12:07 AM

I changed the general code a bit and managed to write structure;

#include <stdio.h>
#include <stdlib.h>

    typedef struct {
    int studentnumber;
    char name[30];
    char surname[30];
    int midterm1,midterm2,midterm3;
    } Student[30];
    
int main()
{
    int choice,studentnumber,midterm1,midterm2,midterm3;
    FILE *cfPtr;
    
    struct student *name;
    name = malloc( 10 * sizeof(Student));
    
    if ((cfPtr = fopen("grades.txt", "r")) == NULL)
    printf("File cannot be opened.\n");
    else {
         printf("%s%s%s%s\n", "StudentNumber", "Name", "Surname", "Midterm1", "Midterm2", "Midterm3");
         fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);
         
         while (!feof(cfPtr)) {
               printf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);
               fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);
    
}
    printf("What would you like to do? \n"
    "1- Sort according to midterm 1\n"
    "2- Sort according to midterm 2\n"
    "3- Sort according to midterm 3\n"
    "4- Exit\n"
    scanf("%d",&choice);
    
    while (choice != 4);{
    fprintf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);
    
   
               
               
    fclose(cfPtr);
}

  system("PAUSE");	
  return 0;
}



Where can I insert the array part now?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 12:30 AM

Look closer at post #4. See his students array? See how he is reading into each structure?

BTW, you have bug where you are creating a dynamic array at line 17, but unfortunately, you using the variable as a parameter in your scanf() to read in the first name.
Was This Post Helpful? 0
  • +
  • -

#7 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 12:59 AM

Okay so I'm currently trying this one:

#include <stdio.h>
#include <stdlib.h>

    typedef struct {
    int number;
    char name[30];
    char surname[30];
    int midterm1,midterm2,midterm3;
    } Student;
    
    int comp(const int * a, const int * B)/> 
{
  if (*a==*B)/>
    return 0;
  else
    if (*a < *B)/>
        return -1;
     else
      return 1;
}
       
int main()
{
    int choice,studentnumber,midterm1,midterm2,midterm3;
    char surname;
    FILE *cfPtr;
    
    struct student *name;
    name = malloc( 10 * sizeof(Student));
    
    if ((cfPtr = fopen("grades.txt", "r")) == NULL)
    printf("File cannot be opened.\n");
    else {
         
         
   	const int STUDENTSMAX = 100;

    Student students[STUDENTSMAX];
    int i = 0;

    while (!feof(cfPtr))

    {
    fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
    printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
  i++;
}
    
    printf("What would you like to do? \n"
    "1- Sort according to midterm 1\n"
    "2- Sort according to midterm 2\n"
    "3- Sort according to midterm 3\n"
    "4- Exit\n");
    scanf("%d",&choice);
    
    while (choice != 4);{
          
          
      switch (choice) {
           
           case 1:
                qsort(students,10,sizeof(int),comp);
                for (i=0; i<9; i++)      
    printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1);
}
    
   
               
               
    fclose(cfPtr);
}

  system("PAUSE");	
  return 0;
}


my qsort gives an error though...
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 02:40 AM

What's the error you are getting?
Was This Post Helpful? 0
  • +
  • -

#9 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 02:45 AM

It says "63 C:\Users\UseR\Desktop\main.c [Warning] passing arg 4 of `qsort' from incompatible pointer type". Also gives those errors too: "75 C:\Users\UseR\Desktop\main.c syntax error at end of input " " C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1 "
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,673
  • Joined: 25-December 09

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 07:13 AM

Your comp function should be defined more like:
int compare (const void * a, const void * B)/>

Note the void* not int* which means you will need to cast these void pointers into the proper type inside the function. See this link: qsort.

Edit: Also you need to find an indentation style you like and use it consistently. You seem to have some mismatched braces which will be easily seen if you use proper indentation.

The code you provided has the following errors/warnings.

Quote

main.c||In function ‘main’:|
main.c|45|warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char (*)[30]’|
main.c|45|warning: format ‘%s’ expects type ‘char *’, but argument 5 has type ‘char (*)[30]’|
main.c|63|error: passing argument 4 of ‘qsort’ from incompatible pointer type|
/usr/include/stdlib.h|761|note: expected ‘__compar_fn_t’ but argument is of type ‘int (*)(const int *, const int *)’|
main.c|65|warning: too few arguments for format|
main.c|60|warning: switch missing default case|
main.c|76|error: expected declaration or statement at end of input|
main.c|26|warning: unused variable ‘surname’|
main.c|25|warning: unused variable ‘midterm3’|
main.c|25|warning: unused variable ‘midterm2’|
main.c|25|warning: unused variable ‘midterm1’|
main.c|25|warning: unused variable ‘studentnumber’|
||=== Build finished: 3 errors, 12 warnings ===|

The first two warnings are because you are using the ampersand with C-strings in fscanf(), since the name of the array is the address you don't need the ampersand when dealing with arrays (C-strings).


Jim

This post has been edited by jimblumberg: 11 August 2012 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#11 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 07:18 AM

Okay fixed that I think, here's the latest one:

#include <stdio.h>
 #include <stdlib.h>

 typedef struct {
 int number;
 char name[30];
 char surname[30];
 int midterm1,midterm2,midterm3;
 } Student;

 int comp(const Student * a, const Student * B)/>
 {
 if (a->midterm1==B->midterm1)
 return 0;
 else
 if (a->midterm1 < B->midterm1)
        return -1;
 else
  return 1;
 }


 int main()
 {
 int choice,studentnumber,midterm1,midterm2,midterm3;
 char surname;
 FILE *cfPtr;

 struct student *name;
 name = malloc( 10 * sizeof(Student));

 if ((cfPtr = fopen("grades.txt", "r")) == NULL)
 printf("File cannot be opened.\n");
 else {


const int STUDENTSMAX = 100;

 Student students[STUDENTSMAX];
 int i = 0;

 while (!feof(cfPtr))

 {
 fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
 printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
 i++;
 }

 printf("What would you like to do? \n"
 "1- Sort according to midterm 1\n"
 "2- Sort according to midterm 2\n"
 "3- Sort according to midterm 3\n"
 "4- Exit\n");
 scanf("%d",&choice);

while (choice != 4) {

  switch (choice) {

  case '1':
  qsort(students, 10, sizeof(Student), comp);
  printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1);
                                      break;
  case '2':
  qsort(students, 10, sizeof(Student), comp);
  printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm2);
                                      break;
  
  case '3':
  qsort(students, 10, sizeof(Student), comp);
  printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm2);
                                      break;
  case '4':
  break;                          
  

   }
   }        
   }        
  fclose(cfPtr);


  system("PAUSE");  
  return 0;
  }



It still returns with the 66 C:\Users\UseR\Desktop\main.c [Warning] passing arg 4 of `qsort' from incompatible pointer type error though :/
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 07:59 AM

Compare your line 11 to what Jim wrote on the previous post.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,673
  • Joined: 25-December 09

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 08:10 AM

And don't forget about these warnings!

Quote

main.c||In function ‘main’:|
main.c|45|warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘char (*)[30]’|
main.c|45|warning: format ‘%s’ expects type ‘char *’, but argument 5 has type ‘char (*)[30]’|


These need to be fixed!

Also your indentation still needs serious work.

You should also check your compiler settings and insure you are using the maximum warning level. And never ignore warnings.

Jim

This post has been edited by jimblumberg: 11 August 2012 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#14 dawsonrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 12

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 09:42 AM

Okay, I ran over most parts and fixed those thanks to helps, and here's the final code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 struct Student {
 int number;
 char name[30];
 char surname[30];
 int midterm1,midterm2,midterm3;
 } Student;

int comp(const void * aa, const void * bb)
{
        struct Student a = *(struct Student*)aa;
        struct Student b = *(struct Student*)bb;
        if (a.midterm1==b.midterm1)
                return 0;
        else if (a.midterm1 < b.midterm1)
                return -1;
        else
                return 1;
}


 int main(void)
 {
 int choice,studentnumber,midterm1,midterm2,midterm3,i,n;
 char surname;
 FILE *cfPtr;
 

 struct student *name;
 name = malloc( 10 * sizeof(Student));

 if ((cfPtr = fopen("grades.txt", "r")) == NULL)
 return 1;

const int STUDENTSMAX = 100;
struct Student students[STUDENTSMAX];
char buff[1024];
while(1)
{
            memset(buff, 0, sizeof(buff));
            fgets(buff, sizeof(buff) -1, cfPtr);
            if (feof(cfPtr)) {
                    break;
            }
            sscanf(buff, "%d %s %s %d %d %d", &students[i].number, students[i].name, students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
            printf("%4d %15s %15s %10d %10d %10d\n", students[i].number, students[i].name, students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
            i++;
    }

 while (!feof(cfPtr))

 {
 fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
 printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
 i++;
 }

 printf("What would you like to do? \n"
 "1- Sort according to midterm 1\n"
 "2- Sort according to midterm 2\n"
 "3- Sort according to midterm 3\n"
 "4- Exit\n");
 scanf("%d",&choice);

 scanf("%d",&choice);
    switch (choice) {
            case 1:
            qsort(students, i, sizeof(struct Student), comp);
            for (n = 0;  n < i; n++) {
                    printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm1);
            }
            break;
    
    
            case 2:
            qsort(students, i, sizeof(struct Student), comp);
            for (n = 0;  n < i; n++) {
                    printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm2);
            }
            break;   
            
            case 3:
            qsort(students, i, sizeof(struct Student), comp);
            for (n = 0;  n < i; n++) {
                    printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm3);
            }
            break;                          
  

 }
   }                  
  fclose(cfPtr);


  system("PAUSE");  
  return 0;
  }




Which returns me with those errors;
95 C:\Users\UseR\Desktop\main.c [Warning] parameter names (without types) in function declaration
95 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
98 C:\Users\UseR\Desktop\main.c syntax error before string constant
98 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1

Except the syntax error, I'm not sure how to fix the rest or what's wrong :/ Any ideas?
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,673
  • Joined: 25-December 09

Re: Storing values from a file into array and sorting them

Posted 11 August 2012 - 09:46 AM

This is where proper indentation would really help you see what is wrong!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct Student {
   int number;
   char name[30];
   char surname[30];
   int midterm1,midterm2,midterm3;
} Student;
 
int comp(const void * aa, const void * bb)
{
   struct Student a = *(struct Student*)aa;
   struct Student b = *(struct Student*)bb;
   if (a.midterm1==b.midterm1)
      return 0;
   else if (a.midterm1 < b.midterm1)
      return -1;
   else
      return 1;
}
 
 
int main(void)
{
   int choice,studentnumber,midterm1,midterm2,midterm3,i,n;
   char surname;
   FILE *cfPtr;
 
 
   struct student *name;
   name = malloc( 10 * sizeof(Student));
 
   if ((cfPtr = fopen("grades.txt", "r")) == NULL)
      return 1;
 
   const int STUDENTSMAX = 100;
   struct Student students[STUDENTSMAX];
   char buff[1024];
   while(1)
   {
      memset(buff, 0, sizeof(buff));
      fgets(buff, sizeof(buff) -1, cfPtr);
      if (feof(cfPtr)) {
         break;
      }
      sscanf(buff, "%d %s %s %d %d %d", &students[i].number, students[i].name, students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
      printf("%4d %15s %15s %10d %10d %10d\n", students[i].number, students[i].name, students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
      i++;
   }
 
   while (!feof(cfPtr))
 
   {
      fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
      printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
      i++;
   }
 
   printf("What would you like to do? \n"
          "1- Sort according to midterm 1\n"
          "2- Sort according to midterm 2\n"
          "3- Sort according to midterm 3\n"
          "4- Exit\n");
   scanf("%d",&choice);
 
   scanf("%d",&choice);
   switch (choice) {
      case 1:
         qsort(students, i, sizeof(struct Student), comp);
         for (n = 0;  n < i; n++) {
            printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm1);
         }
         break;
 
 
      case 2:
         qsort(students, i, sizeof(struct Student), comp);
         for (n = 0;  n < i; n++) {
            printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm2);
         }
         break;
 
      case 3:
         qsort(students, i, sizeof(struct Student), comp);
         for (n = 0;  n < i; n++) {
            printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm3);
         }
         break;
 
 
   }
}
fclose(cfPtr);
 
 
system("PAUSE");
return 0;
}



Look at the indentation level of the last three lines. This indicates missing braces!!!

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2