Search code examples
carrayssortingmultidimensional-arrayalphabetical

Sorting multiple arrays either alphabetically or in descending order


This program is supposed to accept a student's last name (LN), first name (FN), middle initial (MI), address (ADD), first, second, third, and fourth quarter grades (FIR, SEC, THR, FTH), compute for the average of the four inputted grades, and display all the info either alphabetically (last names) or descending order (averages).

It is supposed to look like this:

.
.    Last name:                 First name:                 Middle Intial:     Address:
.
.                   First Quarter grades:
.                   Second Quarter grades:
.                   Third Quarter grades:
.                   Fourth Quarter grades:
.                   Average:
.
.    Which way do you want to arrange the names? Enter A to alphabetize or B to arrange in descending order by grade.

After that, it should clear the screen and only display the names.

.
.    List:
.
.    Apple, Johnny K. from Kansas 89 91 89 91 90.0
.    Graham, Crackers L. from N.Y. 79 81 79 81 80.0
.

It is actually supposed to accept 90 students but I set 3 as a test number.

After inputting all the data, it will end saying "segmentation fault".

Can somebody please tell me what I'm doing wrong? Should I use puts instead of printf in the last part? If so, how do I do that?

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

int main()
{
int a=0, SW=1, x, y, i, j, FIR[100], SEC[100], THR[100], FTH[100], AVE[100], sum=0, temp2[100];
char LN[15][90], FN[15][90], MI[5][90], ADD[50][90], OPT, temp1[50];
while(SW)
{
    clrscr();
    sum=0;

    gotoxy(10,2);
    printf("Family name: ");
    gotoxy(35,2);
    printf("First name: ");
    gotoxy(60,2);
    printf("Middle initial: ");
    gotoxy(80,2);
    printf("Address: ");
    gotoxy(25,4);
    printf("1st Quarter Grade: ");
    gotoxy(25,5);
    printf("2nd Quarter Grade: ");
    gotoxy(25,6);
    printf("3rd Quarter Grade: ");
    gotoxy(25,7);
    printf("4th Quarter Grade: ");
    gotoxy(25,8);
    printf("Average: ");
    gotoxy(23,2);
    fgets(LN[a],15,stdin);
    gotoxy(47,2);
    fgets(FN[a],15,stdin);
    gotoxy(76,2);
    fgets(MI[a],15,stdin);
    gotoxy(89,2);
    fgets(ADD[a],15,stdin);
    gotoxy(44,4); 
    scanf("%d",&FIR[a]);
    sum+=FIR[a];
    gotoxy(44,5);
    scanf("%d",&SEC[a]);
    sum+=SEC[a];
    gotoxy(44,6);
    scanf("%d",&THR[a]);
    sum+=THR[a];
    gotoxy(44,7);
    scanf("%d",&FTH[a]);
    sum+=FTH[a];
    AVE[a]=(float)sum/4;
    gotoxy(34,8);
    printf("%d",AVE[a]);
    a++;
    if(a==3)
        SW=0;
    getch();    
}        
gotoxy(10,10);
printf("Which way do you want to arrange the names? Enter A to alphabetize or B to arrange in descending order by grade. ");
gotoxy(10,11);
scanf("%c",&OPT);
if(OPT=='A' || OPT=='a')
{
    for(x=0;x<=2;x++){
        for(y=x+1;y<=2;y++){
            if(strcmp(LN[x],LN[y])>0){
                strcpy(temp1, LN[x]);
                strcpy(LN[x], LN[y]);
                strcpy(LN[y], temp1);
                strcpy(temp1, FN[x]);
                strcpy(FN[x], FN[y]);
                strcpy(FN[y], temp1);
                strcpy(temp1, MI[x]);
                strcpy(MI[x], MI[y]);
                strcpy(MI[y], temp1);
                strcpy(temp1, ADD[x]);
                strcpy(ADD[x], ADD[y]);
                strcpy(ADD[y], temp1);
                strcpy(temp2, FIR[x]);
                strcpy(FIR[x], FIR[y]);
                strcpy(FIR[y], temp2);
                strcpy(temp2, SEC[x]);
                strcpy(SEC[x], SEC[y]);
                strcpy(SEC[y], temp2);
                strcpy(temp2, THR[x]);
                strcpy(THR[x], THR[y]);
                strcpy(THR[y], temp2);
                strcpy(temp2, FTH[x]);
                strcpy(FTH[x], FTH[y]);
                strcpy(FTH[y], temp2);
                strcpy(temp2, AVE[x]);
                strcpy(AVE[x], AVE[y]);
                strcpy(AVE[y], temp2);
            }
        }
    }
}    
else
{
    for(x=0;x<=2;x++){
        for(y=x+1;x<=2;x++){
            if(AVE[x]<AVE[y]){
                strcpy(temp1, LN[x]);
                strcpy(LN[x], LN[y]);
                strcpy(LN[y], temp1);
                strcpy(temp1, FN[x]);
                strcpy(FN[x], FN[y]);
                strcpy(FN[y], temp1);
                strcpy(temp1, MI[x]);
                strcpy(MI[x], MI[y]);
                strcpy(MI[y], temp1);
                strcpy(temp1, ADD[x]);
                strcpy(ADD[x], ADD[y]);
                strcpy(ADD[y], temp1);
                strcpy(temp2, FIR[x]);
                strcpy(FIR[x], FIR[y]);
                strcpy(FIR[y], temp2);
                strcpy(temp2, SEC[x]);
                strcpy(SEC[x], SEC[y]);
                strcpy(SEC[y], temp2);
                strcpy(temp2, THR[x]);
                strcpy(THR[x], THR[y]);
                strcpy(THR[y], temp2);
                strcpy(temp2, FTH[x]);
                strcpy(FTH[x], FTH[y]);
                strcpy(FTH[y], temp2);
                strcpy(temp2, AVE[x]);
                strcpy(AVE[x], AVE[y]);
                strcpy(AVE[y], temp2);
            }
        }
    }
}
gotoxy(10,15);
printf("List: ");
for(j=17;j<=19;j++){
    for(x=0;x<=2;x++){
    i=10;
    gotoxy(i,j);
    printf("%s,%s %s",LN[x],FN[x],MI[x]);
    i+=40;
    gotoxy(i,j);
    printf("%f",FIR[x]);
    i+=5;
    gotoxy(i,j);
    printf("%f",SEC[x]);
    i+=5;
    gotoxy(i,j);
    printf("%f",THR[x]);
    i+=5;
    gotoxy(i,j);
    printf("%f",FTH[x]);
    i+=5;
    gotoxy(i,j);
    printf("%f",AVE[x]);
    i+=5;
    gotoxy(i,j);
    printf("from %s",ADD[x]);
    }
}

}

Edit: I have no idea what I'm doing. I'm just trying to pass 8th grade Computer Science.


Solution

  • This program is supposed to accept a student's last name (LN), first name (FN), middle initial (MI), address (ADD), first, second, third, and fourth quarter grades (FIR, SEC, THR, FTH), compute for the average of the four inputted grades, and display all the info either alphabetically (last names) or descending order (averages).

    The design you currently use is unmaintainable and disorganized. It sounds like you need to use a stucture to represent a student:

    #define MAX_LNAME    80
    #define MAX_FNAME    32
    #define MAX_ADDRESS  128
    
    typedef struct {
        char lname[MAX_LNAME];
        char fname[MAX_FNAME];
        char address[MAX_ADDRESS];
        double fir, sec, thr, fth;
    } student;
    

    And then a few functions that will manipulate or create student structures

    student mkstudent(char *lname, char *fname, char *address, 
                      double fir, double sec, double thr, double fth)
    {
        student ret;
    
        strcpy(ret.lname, lname);
        strcpy(ret.fname, fname);
        strcpy(ret.address, address);
        ret.fir = fir; ret.sec = sec; ret.thr = thr; ret.fth = fth;
    
        return ret;
    }
    double getAve(const student *s)
    {
        return (s->fir + s->sec + s->thr + s->fth) / 4.;
    }
    void print(const student *s)
    {
        printf("%s, %s from %s: %lf %lf %lf %lf %lf\n", s->lname, s->fname, 
                   s->address, s->fir, s->sec, s->thr, s->fth, getAve(s));
    }
    

    Your program asks to sort the students, so you will need to use qsort (from <stdlib.h>)

    int sortAlpha(const void *a, const void *b)
    {
        return strcmp( ((const student *) a)->lname, 
                       ((const student *) b)->lname );
    }
    
    int sortAves(const void *pa, const void *pb)
    {
        const student *a = (const student *) pa;
        const student *b = (const student *) pb;
    
        double aAve = getAve(a), bAve = getAve(b);
    
        if (aAve == bAve) return 0;
        if (aAve > bAve) return -1;
        if (aAve < bAve) return 1;
    }
    
    #define SORT_LNAME 0    /* sort by last name */
    #define SORT_AVES  1    /* sort by average, in descending order */
    
    /* howSort will be one of the two macros defined above */
    void sort(students *roster, size_t n, int howSort)
    {
        if (howSort == SORT_LNAME)
            qsort(roster, n, sizeof *roster, sortAlpha);
        else (howSort == SORT_AVES)
            qsort(roster, n, sizeof *roster, sortAves);
    }
    

    Now that you have this interface written, you can write your main function by calling various routines that are implemented above

    int main()
    {
        /* array of student structures, possibly read them in from a file, 
           possibly print them, then ask user how to sort, etc. */
    }