Please help me "I WANT IT FOR TOMORROW"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

Please help me "I WANT IT FOR TOMORROW"

 
0
  #1
Jan 9th, 2008
I have a question in c++ , can you help me ,and tell me where is the proplem in my solution ?

The question is:

Assume that the maximum number of students in a class is 50. Write a program that reads students' names followed by their test score from a file and outputs the following:

a. class average
b. Names of all the students whose test scores are below the class average, with an appropriate message
c. Highest test score and the names of all the students having the highest score


You can use this file : in.txt
Ahmed 60.8
Mona 87.3
Ali 77.1
Mahmood 97.9
Isa 63.1
Zainab 100

MY SOLUTION :

  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int count = 0;
  10. float avrg;
  11. int maxIndex=0;
  12. float sum=0;
  13. float scores[50];
  14. string names[50];
  15.  
  16. ifstream infile;
  17. infile.open("in.txt");
  18.  
  19. while (!infile.eof() && (count <=50))
  20. {
  21. infile>>names[count]>>scores[count];
  22. count++;
  23. sum+=scores[count];
  24.  
  25. }
  26. avrg= sum/count;
  27. cout<<avrg;
  28.  
  29. if (scores[50]<avrg)
  30. cout<<names[count];
  31. cout<<endl;
  32.  
  33. maxIndex=scores[0];
  34.  
  35. if (scores[count]>maxIndex)
  36. maxIndex =count;
  37. cout <<names[count];
  38. infile.close();
  39. return 0;
  40. }
Last edited by WolfPack; Jan 9th, 2008 at 10:11 am. Reason: Added [CODE=CPP][/CODE] Tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #2
Jan 9th, 2008
Asking where the problem is not a question in C++.
You have to explain what the problem is, and then we will tell you where it is.

One problem in your code is this loop.
  1. while (!infile.eof() && (count <=50))
  2. {
  3. infile>>names[count]>>scores[count];
  4. count++;
  5. sum+=scores[count];
  6. }

For some reason that is usually in every C++ FAQ, do not use the .eof() function for end of file testing.
Change it to
  1. while ((infile>>names[count]>>scores[count])&& (count <50))
  2. {
  3. sum+=scores[count];
  4. count++;
  5. }

That should correct the problem of incorrect calculation of the average.

For 2 and 3, you should use loops to iterate through all the values in the array, instead of just using scores[50].

Something like
  1. for ( int i = 0 ; i < count; i++ )
  2. {
  3. if (scores[i ]<avrg)
  4. cout<<names[i] << endl;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #3
Jan 9th, 2008
ok ,please tell how can I get the Highest test score and the names of all the students having the highest score ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #4
Jan 9th, 2008
Google for finding maximum in array C++
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #5
Jan 9th, 2008
The most common way to find the biggest number, is creating a variable, which is called max , for storing the first element of the array. Run throught the entirely array and check whether there are any number that are bigger than our max variable. Whenever we found any number that is bigger than max , we immediately assign that value to max . By the end of the loop, max will contain the largest number.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #6
Jan 9th, 2008
Where is the mistakes in my solution , there is no output ,I don't know why.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #7
Jan 9th, 2008
Of course there's output. You have cout statements in your code. Unless you're not telling us the whole story. Explain exactly what is happening, and why it's wrong. Examples are easier for us to understand.

And about "I WANT IT FOR TOMORROW" -- we don't care. You should have posted last week if you needed that bad. See this, it's in The Rules you read when you registered.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #8
Jan 10th, 2008
Thank you inviusal ...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #9
Jan 10th, 2008
thank you very much invisal*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 23
Reputation: wamuti is an unknown quantity at this point 
Solved Threads: 1
wamuti wamuti is offline Offline
Newbie Poster

Re: Please help me "I WANT IT FOR TOMORROW"

 
0
  #10
Jan 10th, 2008
what is invisal?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC