How do I modify thing program??

This is a discussion on How do I modify thing program?? within the C Programming forums, part of the General Programming Boards category; I've managed to write this following program which accepts values for 5 students and works on it. But, for the ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    How do I modify this program??

    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; 
    }
    Last edited by gouthamgmv; 10-30-2010 at 01:08 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    If you want an arbitrary number of students, you will have to use dynamic memory allocation.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You will need to work with dynamic memory if the number of students is unknown at compile time. This basically means replacing all fixed size arrays with dynamically allocated arrays once you know how many students there are.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    You could simply ask the user how many students they need and use this number as the size of your array.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    If you want an arbitrary number of students, you will have to use dynamic memory allocation.
    You will need to work with dynamic memory if the number of students is unknown at compile time. This basically means replacing all fixed size arrays with dynamically allocated arrays once you know how many students there are.
    Can someone please explain this concept in detail?? I'm just a beginner and am not sure, thanks!

    You could simply ask the user how many students they need and use this number as the size of your array.
    I can't do that as I've already defined a function that uses that array above main(). Like I said, I'm a beginner! If I'm wrong, please correct me, thanks!

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    malloc

    i cant write code for you at moment but you are being advised to use the malloc function, you will find lots of examples google. you declare a type pointer and later size it according to requirements then if you pass this new array to a function you use an argument for the type pointer and an int for the size then just reference as usual in the function with square brackets
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A bit of reality check:

    1) Your struct is quite small. Any decent PC will easily handle a static array of a thousand student structs.

    Dynamic sized arrays are fine, but for a beginner, I'd seriously consider using a large static array, instead. I did a program like this last year, and just sized the array statically, for a large number of club members. The club has never exceeded that number of members, and wasting a bit of RAM, just doesn't matter, when the PC has GB of it.

    All the functions are passed the actual number of club members, so there is no problem with the empty elements of the array.

    Realistically, how many students do you expect will ever be entered into this program?

    2) Yes, you can change parameters to a straight pointer style, if you want, in your function declaration. Not a problem.

    This shows a way to allocate a 2D array, in another function:

    Reading CSV file

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    instead of your define num students this will be a local int variable which can obviously be assigned a value by the program or by user input, this value you then pass to malloc function along with type pointer variable like char pointer and hey presto you dun got a dynamic memory allocation to use
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    i agree with adak about the static array, but only because with the concept of a club or school say there is a max amount of members, but then i dont think its best practice to always reference the entire population whenever you need to get information about it, indexing counts aside,
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should your st array directly, not with an extra sortarr.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by rogster001 View Post
    instead of your define num students this will be a local int variable which can obviously be assigned a value by the program or by user input, this value you then pass to malloc function along with type pointer variable like char pointer and hey presto you dun got a dynamic memory allocation to use
    Sorry, but I think I'm still doing it wrongly! I tried that and I'm getting loads of errors! :-/ Can you please provide an example?? Thanks!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remove your #define NUM_STUDENTS -- your num_students will now need to be local, and the user will tell you what that number will be, see?

    Also, remove whole line where you declare the array of students, since it will be done inside a local function, if you want to use dynamic memory allocation, for it.

    Here's a simple example, I strong suggest you copy and run:
    Reading CSV file

    It doesn't show anything about a CSV file - just allocating a dynamic 2D array.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    I seriously don't get this thing, I'm sorry!! How about the static array thing?? Is that easier?? What would I need to do?? Thanks !

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Did the static array thing by myself!! Can't believe I missed such an easy idea! Thanks for the help guys

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gouthamgmv View Post
    Sorry, but I think I'm still doing it wrongly! I tried that and I'm getting loads of errors! :-/ Can you please provide an example?? Thanks!
    Try ...
    Code:
    #define NUM_STUDENTS 5000
    and recompile.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 12:55 AM
  2. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 11:12 AM
  3. Program Crashes when i modify one of the parameters
    By pinkcheese in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 07:46 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21