Thread: need help with sorting students GPA

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    need help with sorting students GPA

    I am basically done with this program, but now my only problem sorting the students GPA from lowest to highest.

    this is the .dat file (its the ways its suppose to look like when printed out):

    ANNA A ADAMS 11 A STREET ALLENTOWN PA 11111 11 5.50
    BOB B BRADBURY 22 B ROAD BOSTON MA 22222 22 2.22
    CARLA C COTTRELL 33 C AVENUE CHICAGO IL 33333 33 3.33
    DENNIS D DODD 44 D SQUARE DETROIT MI 44444 44 4.44
    ERICA E EVANS 55 E STREET EUGENE OR 55555 55 9.99
    FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 66666 66 6.66
    GLENDA G GROGAN 77 G AVENUE GREAT FALLS MT 77777 77 7.77
    HARRY H HALL 88 H WAY HONOLULU HI 88888 88 8.88
    IDA I IFIELD 99 I STREET INDIANAPOLI IN 99999 99 1.11

    The function i need help with is at the very bottom.
    (void sortIntArray(StudFile sRec[], int num))
    i have some of it started but im not sure how to finish it correctly.
    i then need to print it out

    i also need to sort the students by last name.
    i will also need some help with this too.

    compiler: visual studio
    os: microsoft

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    //Defines go here:
    #define FIRST 7
    #define MIDDLE 1
    #define LAST 9
    #define STREET 16
    #define CITY 12
    #define STATE 2
    #define ZIP 5
    #define AGE 3
    #define GPA 5
    #define MAX 81
    #define CLASS 9
    
    //student's address info (added +1 to make room for null char)
    typedef struct
    {
        char streetAddress[STREET + 1];
        char cityName[CITY + 1];
        char stateAbb[STATE + 1];
        char zipCode[ZIP + 1];
    } Address;
    
    //student's personal info (added +1 to make room for null char)
    typedef struct
    {
        char firstName[FIRST + 1];
        char middleInitial[MIDDLE + 1];
        char lastName[LAST + 1];
        Address address;
        int studentAge;
        double studentGPA;
    } StudFile;
    
    //Protypes go here:
    void readFile(StudFile sRec[]);
    void strsub(char s1[],char s2[], int start, int length);
    void printClass(StudFile sRec[]);
    void highestGPA(StudFile sRec[]);
    double averageGPA(StudFile sRec[]);
    void aboveGPA(StudFile sRec[], double average);
    void youngBelowGPA(StudFile sRec[], double average);
    void sortIntArray(StudFile sRec[], int num);
    //void printSort(StudFile sRec[], int num);
    
    void main(void)
    {
        double average_gpa = 0;
    
        StudFile student[CLASS];
        readFile(student);
        printClass(student);
    	highestGPA(student);
        average_gpa = averageGPA(student);
        aboveGPA(student,average_gpa);
    	youngBelowGPA(student, average_gpa);
    	sortIntArray(student, 9);
    	//printSort(student, 9);
    }
    
    void readFile(StudFile s[])
    {
        char line[MAX];
        char tempAGE[AGE];
        char tempGPA[GPA];
        int i = 0;
    
    	FILE *fp;
        errno_t err;
        err = fopen_s(&fp, "Students.dat", "r");
        if(err){
            printf("File cannot be found!\n");
            exit(1);
        }
        while(!feof(fp)){
            fgets(line, MAX, fp);
            strsub(line, s[i].firstName, 0, FIRST);
            strsub(line, s[i].middleInitial, 8, MIDDLE);
            strsub(line, s[i].lastName, 10, LAST);
            strsub(line, s[i].address.streetAddress, 20, STREET);
            strsub(line, s[i].address.cityName, 37, CITY);
            strsub(line, s[i].address.stateAbb, 49, STATE);
            strsub(line, s[i].address.zipCode, 52, ZIP);
            strsub(line, tempAGE, 58, AGE - 1);
            strsub(line, tempGPA, 61, GPA - 1);
            s[i].studentAge = atoi(tempAGE);
            s[i].studentGPA = atof(tempGPA);
            i++;
        }
        fclose(fp);
    }
    
    void strsub(char s1[],char s2[], int start, int length)
    {
        int i;
    
        for(i = start; i < (start + length); ++i){
            s2[i - start] = s1[i];
        }
        s2[i - start] = '\0';
    }
    
    //Prints the class .dat file
    void printClass(StudFile sRec[])
    {
        int i;
    
        printf("Students in class:\n\n");
        for(i=0;i<CLASS;++i){
    		printf("%s %s %s ", sRec[i].firstName, sRec[i].middleInitial, sRec[i].lastName);
    		printf("%s %s ", sRec[i].address.streetAddress, sRec[i].address.cityName);
    		printf("%s %s ", sRec[i].address.stateAbb, sRec[i].address.zipCode);
    		printf("%i, %.2f\n", sRec[i].studentAge, sRec[i].studentGPA);
        }
        printf("\n\n");
    }
    
    void highestGPA(StudFile sRec[])
    {
        int i, x = 0;
    
        for(i = 0; i < CLASS; ++i){
            if(sRec[i].studentGPA > sRec[x].studentGPA)
                x = i;
        }
    	printf("Student with Highest GPA: \n");
    	printf("%s %s %s GPA is: %.2f.\n\n", sRec[x].firstName, sRec[x].middleInitial,
            sRec[x].lastName, sRec[x].studentGPA);
    }
    
    double averageGPA(StudFile sRec[])
    {
        int i;
        double sum = 0;
    
        for(i = 0; i < CLASS; ++i){
            sum+= sRec[i].studentGPA;
        }
        printf("Average GPA: %.2f\n\n", sum / i);
        return (sum / i);
    }
    
    
    void aboveGPA(StudFile sRec[],double average)
    {
        int i;
    
        printf("Students with higher than average GPA:\n");
        for(i = 0; i < CLASS; i++){
            if(sRec[i].studentGPA >= average){
    			printf("%s %s %s GPA is: %.2f.\n", sRec[i].firstName, sRec[i].middleInitial, 
    				sRec[i].lastName, sRec[i].studentGPA);
            }
        }
    	printf("\n");
    }
    
    void youngBelowGPA(StudFile sRec[], double average)
    {
    	int i, x = 0;
    
    	printf("Youngest student with GPA below average: \n");
    	for(i = 0; i < CLASS; ++i){
    		 if((sRec[i].studentGPA < average) && (sRec[i].studentAge <= sRec[x].studentAge))
    			 x = i;
    	}
    		printf("%s %s %s GPA is: %.2f.\n\n", sRec[x].firstName, sRec[x].middleInitial,
            sRec[x].lastName, sRec[x].studentGPA);
    }
    
    void sortIntArray(StudFile sRec[], int num)
    {
    	int i, j; // indexes into unsorted and sorted partitions
    	int temp; // temporarily holds an element from the array
    	
    	for (i = 1; i < num; ++i) {
    		temp = sRec[i].studentGPA;
    		j = i - 1;
    		while (j >= 0 && temp < sRec[j].studentGPA) {
    			sRec[j + 1].studentGPA = sRec[j].studentGPA;
    			j = j - 1;
    		}
    		sRec[j + 1].studentGPA = temp;
    	}
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you are allowed to use qsort(), use qsort(). Then you just need to write comparison depending on which key (GPA,name) you want to sort.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    no im not allowed to use that. i just need to finish void sortIntArray(StudFile sRec[], int num) and then print it out.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	for (i = 1; i < num; ++i) {
    		temp = sRec[i].studentGPA;
    		j = i - 1;
    		while (j >= 0 && temp < sRec[j].studentGPA) {
    			sRec[j + 1].studentGPA = sRec[j].studentGPA;
    			j = j - 1;
    		}
    		sRec[j + 1].studentGPA = temp;
    	}
    Look very closely at what you are doing here... You are sorting the GPAs by moving them from one student record to another... you are not sorting student records in GPA order...

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    sorry for my ignorance, but i don't see it.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You need to exchange the entire studFile record... not just the GPA field.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I answered this on the other forum you posted this up to, already.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(!feof(fp)){
        fgets(line, MAX, fp);
        ... processing of data ...
    }
    Flawed... don't use feof to control your loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Please
    By Ekrish in forum C Programming
    Replies: 13
    Last Post: 12-11-2009, 03:59 PM
  2. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  3. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  4. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  5. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM