Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   How do I modify this program to work for 'n' number of students? (https://www.askmehelpdesk.com/showthread.php?t=521172)

  • Oct 29, 2010, 06:18 AM
    gouthamgmv
    How do I modify this program to work for 'n' number of students?
    I've managed to write this following program which accepts values for 5 students and works on it. But, for the life of me, I can't figure out how to modify this for 'n' number of students! I've tried almost everything I know but I can't get it to work without errors! Does anyone have any ideas? Thanks!

    Code:

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

    #define NUM_STUDENTS 5

    #define IND_REGNO 0
    #define IND_FAIL 1
    #define IND_RANK 2
    #define IND_TOTAL 3
    #define IND_MARKS 4

    int st[NUM_STUDENTS][9];
    int sortarr[NUM_STUDENTS];

    int sortfunc(const void* p1, const void* p2) {
        int id1=*(int*)p1, id2=*(int*)p2;

        return st[id2][IND_TOTAL]-st[id1][IND_TOTAL];
    }

    int main() {
        printf("\n Enter the register number and marks in each subject");
        int i, j, passers=0, passrate=0, topper=0;

        for(i=0; i<NUM_STUDENTS; i++) {
            scanf("%i %i %i %i %i %i", &st[i][IND_REGNO], &st[i][IND_MARKS+0], &st[i][IND_MARKS+1],
                  &st[i][IND_MARKS+2], &st[i][IND_MARKS+3], &st[i][IND_MARKS+4]);

            for(j=0; j<5; j++) {
                st[i][IND_TOTAL]+=st[i][IND_MARKS+j];
                if(st[i][IND_MARKS+j] < 50) {
                    st[i][IND_FAIL]=1;
                }
            }

            if(st[i][IND_FAIL] == 0) {
                passers++;
            }
        }

        for(i=0; i<NUM_STUDENTS; i++) {
            sortarr[i]=i;
        }

        qsort(sortarr, NUM_STUDENTS, sizeof(int), sortfunc);

        for(i=0; i<NUM_STUDENTS; i++) {
            st[sortarr[i]][IND_RANK]=i+1;
        }
        passrate=(passers*100) / NUM_STUDENTS;
        topper=st[sortarr[0]][IND_REGNO];
    printf("\nNo \tM1 \tM2 \tM3 \tM4 \tM5 \tTOTAL \tP/F \tRANK \n \n \n");
        for(i=0; i<NUM_STUDENTS; i++) {
            printf("\n%i \t%i \t%i \t%i \t%i \t%i \t%i \t%s \t%i\n", st[i][IND_REGNO], st[i][IND_MARKS+0], st[i][IND_MARKS+1],
                  st[i][IND_MARKS+2], st[i][IND_MARKS+3], st[i][IND_MARKS+4], st[i][IND_TOTAL],
                  (st[i][IND_FAIL]==0) ? "PASS" : "FAIL", st[i][IND_RANK]);
        }

        printf("\nPass Percentage - %i%%\nTopper - %i\n", passrate, topper);
        getch();
        return 0;
    }

  • Jan 12, 2011, 12:40 PM
    rpray2007
    You can't use a standard array structure - you have to use a dynamic malloc procedure to do this. It is a lot more complicated but it will help you understand C much better.
  • Nov 30, 2013, 10:14 AM
    Scleros
    Old thread, but "can't" caught my eye. A standard array of predetermined MAX_SIZE could be used if the upper limit of 'n' is bounded and known. Students are finite.

  • All times are GMT -7. The time now is 03:02 AM.