By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

Figuring out 'second highest' student average

P: 7
hi umm
i need help in writing a program in c(or c++)
here are the details:


i want it to recieve "x" numbers of student id's and the average of the student in their last term
and i want it to tell me who was second best
(who has the second highest average )**
just using "for" and "if" and "while" and "do...while" and "else if"
can somebody please help me as it is urgent.
i would very much appreciate it.
here is what i have so far
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. int main()
  4. {
  5.  int n, i, stdno1, stdno;
  6.  float ave, max;
  7.  clrscr();
  8.  printf("\n Enter number of students : ");
  9.  scanf("%d", &n);
  10.  printf("\n Enter student number1 and average1 : ");
  11.  scanf("%d%f", &stdno1, &max);
  12.  for(i = 1 ; i <= n ; i++) {
  13.     printf("\n Enter next student number and average : ");
  14.     scanf("%d%f", &stdno, &ave);
  15.     if(ave >= max)
  16.         printf(" %d %f ", stdno, ave);
  17.     else
  18.         printf(" %d %f ", stdno1, max);
  19.     }
  20.  getch();
  21.  return 0;
  22. }
but in my code,it only works for 2 averages.how can i find the second highest average out of "n" averages?
Nov 17 '07 #1
Share this Question
Share on Google+
21 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Well, your code only reads in 2 averages. How are you reading in the data? Is it input manually, or by file? If you can have them input the number, you can initialize an array with that many elements, and then use that as the max condition in a for loop to read them in. If it's in a file, you can cycle through the file (depending on how it is delimited - if it's each line, you can get a line count and use that, or else you can scan through and read how many delimiters there are and use that).

So you should start by deciding how you are going to figure out how many averages you're going to be reading in. Then you'll have to modify your code a little bit, I'd recommend a temp variable with the "current top 2 averages" (as you may have one that is either the new top, and your top is now your second, or the new second).
Nov 18 '07 #2

P: 7
Well, your code only reads in 2 averages. How are you reading in the data? Is it input manually, or by file? If you can have them input the number, you can initialize an array with that many elements, and then use that as the max condition in a for loop to read them in. If it's in a file, you can cycle through the file (depending on how it is delimited - if it's each line, you can get a line count and use that, or else you can scan through and read how many delimiters there are and use that).

So you should start by deciding how you are going to figure out how many averages you're going to be reading in. Then you'll have to modify your code a little bit, I'd recommend a temp variable with the "current top 2 averages" (as you may have one that is either the new top, and your top is now your second, or the new second).
well thats the problem.i dont want to use arrays.first of all im not allowed to.and the question itself says using only for and while and similar simple stuff.
and as u can see the user themself has to enter the number of students and then the student number and average one by one!
and i want the program to find out who has the second highest average.thats it.
Nov 18 '07 #3

P: 7
look i got up to here.but it seems to have some logical problems.can u please help me?im stuck
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. int main()
  4. {
  5.  int n, i,stdno1, stdno2, finalstdno, emptystdno;
  6.  float ave1, ave2, finalave, emptyave;
  7.  clrscr();
  8.  printf("\n Enter number of students : ");
  9.  scanf("%d", &n);
  10.  printf("\n Enter student number1 and average1 : ");
  11.  scanf("%d%f", &stdno1, &ave1);
  12.  printf("\n Enter student number2 and average2 : ");
  13.  scanf("%d%f", &emptystdno, &emptyave);
  14.  if(emptyave >= ave1) {
  15.     stdno2 = emptystdno;
  16.     ave2 = emptyave;
  17.     for(i = 2 ; i < n ; i++) {
  18.         printf("\n Enter next data : ");
  19.         scanf("%d%f", &emptystdno, &emptyave);
  20.         if(emptyave <= ave2 && emptyave > ave1) {
  21.             finalstdno = emptystdno;
  22.             finalave = emptyave;
  23.             }/* end of if */
  24.         else if (emptyave <= ave1) {
  25.             finalstdno = stdno1;
  26.             finalave = ave1;
  27.             }/* end of else */
  28.         else if (emptyave >= ave2) {
  29.             finalstdno = stdno2;
  30.             finalave = ave2;
  31.             }/* end of else */
  32.         }/* end of for */
  33.         printf("\n result : %d %5.2f ", finalstdno, finalave);
  34.        }/* end of if */
  35.  else {
  36.     stdno2 = emptystdno;
  37.     ave2 = emptyave;
  38.     for(i = 2 ; i < n ; i++) {
  39.         printf("\n Enter next data : ");
  40.         scanf("%d%f", &emptystdno, &emptyave);
  41.         if(emptyave < ave1 && emptyave > ave2) {
  42.             finalstdno = emptystdno;
  43.             finalave = emptyave;
  44.             }/* end of if */
  45.         else if (emptyave < ave2) {
  46.             finalstdno = stdno1;
  47.             finalave = ave1;
  48.             }/* end of else */
  49.         else if (emptyave > ave1) {
  50.             finalstdno = stdno2;
  51.             finalave = ave2;
  52.             }/* end of else */
  53.           }/* end of for */
  54.           printf("\n result : %d %5.2f", finalstdno, finalave);
  55.         } /* end of else */
  56.  getch();
  57.  return 0;
  58.  }
  59.  
Nov 18 '07 #4

AHMEDYO
100+
P: 112
HI....

i was seen your first post but i didnt reply because i cant understand exactly what you want and i was wait for another post trying to understand but i didnt get it yet..your first example said when average >= max then you will print this student but else you will print First student average, i understand that u want one of two things

1.you wanna to print student average if average >=max but else you wanna to print last max average you was meet in your loop instead of first student

2.you wanna to compare the current average in your loop with all last average you was meet in your loop, and print it if it the max one or print what i dont know??

finally in your second example i see that you repeat "enter student1" and "enter student2" before loop this have no meaning to exit "enter student 2" from your loop this will bring you to "enter student3" and then you will not have loop

i think you will help use very much if you give real example by your hand. try to write example if user input what the result you need????


Thanx
Nov 18 '07 #5

P: 7
i know
im so confused
i just want to find out how i can enter "x' numbers of students and i want the program to tell me the second highest one.its so simple but i cant get it done.
please help me.can anyone provide me a way of doing it not using arrays?
Nov 18 '07 #6

AHMEDYO
100+
P: 112
Hey.. Rest your mind man

i wanna help but you didnt help me

for example if User input is

Enter Student 1: ID=10 , Average=3
Student Numbers=5
Max=8

Enter Student 2: ID=5 , Average=7
Enter Student 3: ID=1 , Average=5
Enter Student 4: ID=3 , Average=4
Enter Student 5: ID=7 , Average=8
Enter Student 6: ID=6 , Average=2

simple what the result you want?????

waiting your reply
Nov 18 '07 #7

P: 7
Hey.. Rest your mind man

i wanna help but you didnt help me

for example if User input is

Enter Student 1: ID=10 , Average=3
Student Numbers=5
Max=8

Enter Student 2: ID=5 , Average=7
Enter Student 3: ID=1 , Average=5
Enter Student 4: ID=3 , Average=4
Enter Student 5: ID=7 , Average=8
Enter Student 6: ID=6 , Average=2

simple what the result you want?????

waiting your reply
as u can see the highest average is 8,i want the program to tell me the second highest average is 7.thats it
Nov 18 '07 #8

AHMEDYO
100+
P: 112
hi man..

i think you need somehing like that

Spoonfeeding code removed, please read the Posting Guidelines

GOOD LUCK
Nov 18 '07 #9

P: 1
HI some changes in your code,hopes to help u
you can try it

Spoonfeeding code removed, please read the Posting Guidelines
Nov 18 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
@Everyone in this thread-

The OP is a beginner in C/C++, and therefore struggling with the basics. This means that it would be easy for people to just add/edit code and give it back, however that does not help the OP. I have removed all the code examples, please talk to the OP, don't just throw code out. Explain why you are doing the things you are doing, the logic behind it. Then the OP will begin to learn not only the syntax rules, but also pick up things like good programming practices.

Thanks
Nov 18 '07 #11

AHMEDYO
100+
P: 112
@Everyone in this thread-

The OP is a beginner in C/C++, and therefore struggling with the basics. This means that it would be easy for people to just add/edit code and give it back, however that does not help the OP. I have removed all the code examples, please talk to the OP, don't just throw code out. Explain why you are doing the things you are doing, the logic behind it. Then the OP will begin to learn not only the syntax rules, but also pick up things like good programming practices.

Thanks
Hi...

i just edit his code and port it again because he read it and then learn how to do the required task , then he can make it by his way, if we cant post code here in script site!!!! , where i can post code, may be i cant understand you, but i see that was no reason to delete code.

Many Thanx
Nov 18 '07 #12

AHMEDYO
100+
P: 112
and finally i see that i was learn more and more by reading code from others, just he will read and understand how he can attain requirements, and im not give him full application just 2 lines to understand

and please take a deep look we was talking about task, not just code editing
Nov 18 '07 #13

AHMEDYO
100+
P: 112
HI...

The concept Behind code is
the code is simple start loop with 2 average variables = 0's, and when loop start it check about if the entered average greater that the first average and the average 1 less that average 2, because the concept is to replace the less average and hold the greater one, and dont overwite the greater, then if the conditional 1 is not meet, then loop check if the entered average greater than average2, if itgreater than we hold it in ave2 variable and you can think in loop as sort operation we cant print result within loop, after loop we print the result.


[ snip ]

Many Many Thanx..
Nov 18 '07 #14

Expert 10K+
P: 11,448
[ snip ]

Many Many Thanx..
Please don't spoonfeed code to the OP, not just this one in particular but to no
OP at all. It raises the chance that an OP might cheat with it and turn it in as if
it were their own work. We don't want to encourage cheating for at least two
important reasons:

1) teachers or lecturers might want to blaim this forum for encouraging cheating,
which is not what we want.

2) nobody wants a coworker that just graduated by cheating; it gives you double
the work without getting the benefits for it.

Also please read our posting guidelines (press the 'Help' tag near the top right
corner of this page).

kind regards,

Jos
Nov 18 '07 #15

AHMEDYO
100+
P: 112
HI...

Thanx for your help..but simple question why [code] Tag is in your forums, because i was read guid lines and i think i understand guid lines and home work and so on... and i think i am not out of rules

Thanx once again
Nov 18 '07 #16

Expert 10K+
P: 11,448
HI...

Thanx for your help..but simple question why [code] Tag is in your forums, because i was read guid lines and i think i understand guid lines and home work and so on... and i think i am not out of rules

Thanx once again
Well, as a matter of fact you did break the rules by posting a complete program
to the OP (I snipped it).

Those tags are supplied if you want or need to supply a snippet of code along
your explanation. Don't supply source code that can be (almost) copied and
pasted, compiled, run and turned in as an answer to the OPs question.

You're just lucky that I'm a bit easy going on this because you could be banned
from this forum, either temporarily or permanently if you spoonfeed source code.

The Articles section may contain full source code but they must be accompanied
with full explanatory text and must deal with general problems, not just about
finding the two largest numbers in a sequence.

Just don't do it ok?

kind regards,

Jos
Nov 18 '07 #17

AHMEDYO
100+
P: 112
nice that i am lucky :D, look man my words like to be resist to your words, but its not you can think of it as different opinions between us ,no more, and beleive me your ban will not make me sad or anythink like that, but i will be sad if i am rude with anyone and im not like to be that

i promise that. i will try to understand and i will try to be better

Sorry if i was rude.and Thanx alot for your patient
Nov 18 '07 #18

Expert 10K+
P: 11,448
nice that i am lucky :D, look man my words like to be resist to your words, but its not you can think of it as different opinions between us ,no more, and beleive me your ban will not make me sad or anythink like that, but i will be sad if i am rude with anyone and im not like to be that

i promise that. i will try to understand and i will try to be better

Sorry if i was rude.and Thanx alot for your patient
You weren't rude; you just didn't follow those posting guidelines, that's all. If you
don't violate them again there's no harm done. Replying to a post like this can
be difficult, i.e. you want to help the OP on track but you don't want to spoonfeed
anything, just make the OP start to think in the right direction. Making people
think for themselves is one of the primary goals of these forums. We want to help
them to solve their problems themselves.

kind regards,

Jos
Nov 18 '07 #19

AHMEDYO
100+
P: 112
Thanx very much, i confess that you learn me more from your words, and sure i will do that and i will help as i can without spoonfeed

Thanx once again for your patient.
Nov 18 '07 #20

Expert 10K+
P: 11,448
Thanx very much, i confess that you learn me more from your words, and sure i will do that and i will help as i can without spoonfeed

Thanx once again for your patient.
I'll send you a Personal Message (PM) see the top of the page after a minute
or so.

kind regards,

Jos
Nov 18 '07 #21

P: 7
hey
thank you for your help
i sat down and thought alot on how to write the program,and BANG,i figured it out and wrote it in two different codes that both work!!
check these two out:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #define N 10
  5.  
  6. void main(void)
  7. {int n,i,j,k,sid[N];
  8. float temp1,temp2,avg[N];
  9. clrscr();
  10. printf("\nThe number of students is:");
  11. scanf("%d",&n);
  12. for(i=0;i<n;i++)
  13. {printf("\nEnter StudentID for %dth student:",i+1);
  14. scanf("%d",&sid[i]);
  15. printf("\nEnter Average for %dth student:",i+1);
  16. scanf("%f",&avg[i]);
  17. }
  18. temp1=0;
  19. temp2=0;
  20. for(i=0;i<n;i++)
  21. if(avg[i]>=temp1)
  22. {temp1=avg[i];
  23. k=i;
  24. }
  25. for(i=0;i<n;i++)
  26. if(avg[i]==temp1)avg[i]=0;
  27. for(i=0;i<n;i++)
  28. if(avg[i]>=temp2)
  29. {temp2=avg[i];
  30. j=i;
  31. }
  32. avg[k]=temp1;
  33. printf("\nThe 2nd avg[%5.2f]is for student with SID=%d",temp2,sid[j]);
  34. getch();
  35. }
  36.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. int main()
  5. {
  6. int i, c, stdno, n , finalstdno = 0;
  7. float ave, max1 = 0, max2 = 0;
  8. clrscr();
  9. printf("\n This is a program for calculating the second highest average in a class.");
  10. printf("\n Programmed by Farzad Soltani.");
  11. printf("\n Enter number of students : ");
  12. scanf("%d", &n);
  13. for(i = 0 ; i < n ; i++) {
  14.     if(n == 1) {
  15.         printf("\n Please restart the program and enter more than one student!!");
  16.         break;
  17.         }/* End of if */
  18.     printf("\n Enter student number and the average of the student : ");
  19.     scanf("%d%f", &stdno , &ave);
  20.     if(ave >= max1) {
  21.         c = finalstdno;
  22.         finalstdno = stdno;
  23.         max2 = max1;
  24.         max1 = ave;
  25.         }/* End of if */
  26.     else if(ave > max2 && ave < max1) {
  27.         c = finalstdno;
  28.         finalstdno = stdno;
  29.         max2 = ave;
  30.         }/* End of else if */
  31.     }/* End of for */
  32. printf("\n The second highest average is from student number : %d", c);
  33. printf("\n With the average : %5.2f ", max2);
  34. getch();
  35. return 0;
  36. }/* End of program */
  37.  
Nov 18 '07 #22

Post your reply

Sign in to post your reply or Sign up for a free account.