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 -
#include <stdio.h>
-
#include <conio.h>
-
int main()
-
{
-
int n, i, stdno1, stdno;
-
float ave, max;
-
clrscr();
-
printf("\n Enter number of students : ");
-
scanf("%d", &n);
-
printf("\n Enter student number1 and average1 : ");
-
scanf("%d%f", &stdno1, &max);
-
for(i = 1 ; i <= n ; i++) {
-
printf("\n Enter next student number and average : ");
-
scanf("%d%f", &stdno, &ave);
-
if(ave >= max)
-
printf(" %d %f ", stdno, ave);
-
else
-
printf(" %d %f ", stdno1, max);
-
}
-
getch();
-
return 0;
-
}
but in my code,it only works for 2 averages.how can i find the second highest average out of "n" averages?
21 2594
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, 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.
look i got up to here.but it seems to have some logical problems.can u please help me?im stuck -
#include <stdio.h>
-
#include <conio.h>
-
int main()
-
{
-
int n, i,stdno1, stdno2, finalstdno, emptystdno;
-
float ave1, ave2, finalave, emptyave;
-
clrscr();
-
printf("\n Enter number of students : ");
-
scanf("%d", &n);
-
printf("\n Enter student number1 and average1 : ");
-
scanf("%d%f", &stdno1, &ave1);
-
printf("\n Enter student number2 and average2 : ");
-
scanf("%d%f", &emptystdno, &emptyave);
-
if(emptyave >= ave1) {
-
stdno2 = emptystdno;
-
ave2 = emptyave;
-
for(i = 2 ; i < n ; i++) {
-
printf("\n Enter next data : ");
-
scanf("%d%f", &emptystdno, &emptyave);
-
if(emptyave <= ave2 && emptyave > ave1) {
-
finalstdno = emptystdno;
-
finalave = emptyave;
-
}/* end of if */
-
else if (emptyave <= ave1) {
-
finalstdno = stdno1;
-
finalave = ave1;
-
}/* end of else */
-
else if (emptyave >= ave2) {
-
finalstdno = stdno2;
-
finalave = ave2;
-
}/* end of else */
-
}/* end of for */
-
printf("\n result : %d %5.2f ", finalstdno, finalave);
-
}/* end of if */
-
else {
-
stdno2 = emptystdno;
-
ave2 = emptyave;
-
for(i = 2 ; i < n ; i++) {
-
printf("\n Enter next data : ");
-
scanf("%d%f", &emptystdno, &emptyave);
-
if(emptyave < ave1 && emptyave > ave2) {
-
finalstdno = emptystdno;
-
finalave = emptyave;
-
}/* end of if */
-
else if (emptyave < ave2) {
-
finalstdno = stdno1;
-
finalave = ave1;
-
}/* end of else */
-
else if (emptyave > ave1) {
-
finalstdno = stdno2;
-
finalave = ave2;
-
}/* end of else */
-
}/* end of for */
-
printf("\n result : %d %5.2f", finalstdno, finalave);
-
} /* end of else */
-
getch();
-
return 0;
-
}
-
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
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?
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
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
hi man..
i think you need somehing like that
Spoonfeeding code removed, please read the Posting Guidelines
GOOD LUCK
HI some changes in your code,hopes to help u
you can try it
Spoonfeeding code removed, please read the Posting Guidelines
@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
@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
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
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..
[ 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
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
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
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
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
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.
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
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: -
-
#include <stdio.h>
-
#include <conio.h>
-
#define N 10
-
-
void main(void)
-
{int n,i,j,k,sid[N];
-
float temp1,temp2,avg[N];
-
clrscr();
-
printf("\nThe number of students is:");
-
scanf("%d",&n);
-
for(i=0;i<n;i++)
-
{printf("\nEnter StudentID for %dth student:",i+1);
-
scanf("%d",&sid[i]);
-
printf("\nEnter Average for %dth student:",i+1);
-
scanf("%f",&avg[i]);
-
}
-
temp1=0;
-
temp2=0;
-
for(i=0;i<n;i++)
-
if(avg[i]>=temp1)
-
{temp1=avg[i];
-
k=i;
-
}
-
for(i=0;i<n;i++)
-
if(avg[i]==temp1)avg[i]=0;
-
for(i=0;i<n;i++)
-
if(avg[i]>=temp2)
-
{temp2=avg[i];
-
j=i;
-
}
-
avg[k]=temp1;
-
printf("\nThe 2nd avg[%5.2f]is for student with SID=%d",temp2,sid[j]);
-
getch();
-
}
-
-
-
#include <stdio.h>
-
#include <conio.h>
-
int main()
-
{
-
int i, c, stdno, n , finalstdno = 0;
-
float ave, max1 = 0, max2 = 0;
-
clrscr();
-
printf("\n This is a program for calculating the second highest average in a class.");
-
printf("\n Programmed by Farzad Soltani.");
-
printf("\n Enter number of students : ");
-
scanf("%d", &n);
-
for(i = 0 ; i < n ; i++) {
-
if(n == 1) {
-
printf("\n Please restart the program and enter more than one student!!");
-
break;
-
}/* End of if */
-
printf("\n Enter student number and the average of the student : ");
-
scanf("%d%f", &stdno , &ave);
-
if(ave >= max1) {
-
c = finalstdno;
-
finalstdno = stdno;
-
max2 = max1;
-
max1 = ave;
-
}/* End of if */
-
else if(ave > max2 && ave < max1) {
-
c = finalstdno;
-
finalstdno = stdno;
-
max2 = ave;
-
}/* End of else if */
-
}/* End of for */
-
printf("\n The second highest average is from student number : %d", c);
-
printf("\n With the average : %5.2f ", max2);
-
getch();
-
return 0;
-
}/* End of program */
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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)....
|
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...
|
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.
...
|
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...
|
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;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |