User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,804 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,337 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 642 | Replies: 9
Reply
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

Question Please help me "I WANT IT FOR TOMORROW"

  #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 9:11 am. Reason: Added [CODE=CPP][/CODE] Tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

  #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  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

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

  #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  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

  #4  
Jan 9th, 2008
Google for finding maximum in array C++
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

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

  #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  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

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

  #6  
Jan 9th, 2008
Where is the mistakes in my solution , there is no output ,I don't know why.
Reply With Quote  
Join Date: May 2006
Posts: 2,719
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

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

  #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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jan 2008
Posts: 6
Reputation: smile! is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
smile! smile! is offline Offline
Newbie Poster

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

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

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

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

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

  #10  
Jan 10th, 2008
what is invisal?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC