473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Figuring out 'second highest' student average

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
21 2594
sicarie
4,677 Expert Mod 4TB
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
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
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
112 100+
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
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
112 100+
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
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
112 100+
hi man..

i think you need somehing like that

Spoonfeeding code removed, please read the Posting Guidelines

GOOD LUCK
Nov 18 '07 #9
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
4,677 Expert Mod 4TB
@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
112 100+
@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
112 100+
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
112 100+
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
JosAH
11,448 Expert 8TB
[ 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
112 100+
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
JosAH
11,448 Expert 8TB
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
112 100+
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
JosAH
11,448 Expert 8TB
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
112 100+
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
JosAH
11,448 Expert 8TB
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
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

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

Similar topics

2
by: Dirtyweeker | last post by:
Hi, I have a database which records fitness test results of pupils. There are the usual name fields and then a series of fields holding results, e.g. field BP1 and field BP2; each of these...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
1
by: sean | last post by:
I'm trying to change my applications priority to something besides the basic highest, above average, etc. I'm developing for an embedded processor (Windows CE 4.2), thus I'm using evc++ in...
3
by: lostncland | last post by:
I am working on this program. The array has 10 scores. (there is more to this program.) Does the last "for" section make sense? /*Defines a global constant called N throughout the file. ...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
2
by: nirav11 | last post by:
# include <iostream> # include <fstream> # include <iomanip> # include <cstdlib> // needed for exit () using namespace std; int main() { ifstream inFile; ofstream outFile;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.