Ask Experts Questions for FREE Help !
Ask
    gouthamgmv's Avatar
    gouthamgmv Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 29, 2010, 06:18 AM
    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; 
    }
    rpray2007's Avatar
    rpray2007 Posts: 319, Reputation: 23
    Full Member
     
    #2

    Jan 12, 2011, 12:40 PM
    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.
    Scleros's Avatar
    Scleros Posts: 2,166, Reputation: 262
    Hardware Expert
     
    #3

    Nov 30, 2013, 10:14 AM
    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How do I program a woods timer model number TN300 [ 1 Answers ]

How do I program a woods timer model number TN300


View more questions Search