Ask Experts Questions for FREE Help!
 

Free Answers in 3 Easy Steps

Register Now
3 Steps
 


Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.
  View Answers    Answer this question    Ask a question  
 

gouthamgmv
Oct 29, 2010, 06:18 AM
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!

#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
Jan 12, 2011, 11:40 AM
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.