943,787 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 615
  • C++ RSS
Jan 10th, 2008
0

need some help

Expand Post »
I have these question and I need some help from you :
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

and this is my answer , and I dont know what is the problem with it , becuase the out put was not what I need the number on the screen like this -234567877.87 .
C++ Syntax (Toggle Plain Text)
  1.  
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7. int main()
  8. { double scores[50];
  9. double avg ;
  10. double sum=scores[0],max = scores[0];
  11. string names[50];
  12. int count=0;
  13. ifstream data;
  14. data.open("in.txt");
  15.  
  16. while (!data.eof()&&count<=50)
  17.  
  18. {
  19. data>> names[count] >> scores[count] ;
  20. count++;
  21. }
  22.  
  23. for (int m=1; m<count; m++)
  24. {
  25.  
  26. sum+= scores[m];
  27.  
  28.  
  29. if (scores[m]>max)
  30. {
  31. max= scores[m];
  32.  
  33. }
  34. }
  35.  
  36. avg= sum/count;
  37.  
  38. cout<<" class average is : "<<avg<<endl;
  39. cout<<"Students whose test scores are below the class average are :"<<endl;
  40.  
  41. for (int n=0; n<count; n++)
  42. {
  43. if (scores[n]<avg)
  44. cout<<names[n]<<endl;
  45. }
  46.  
  47. cout<<"Highest test score is : "<<max<<endl;
  48. cout<<"Students having the highest score are :"<<endl;
  49.  
  50. for (int i=0; i<count; i++)
  51. {
  52. if (scores[i]==max)
  53. cout<<names[i]<<endl;
  54. }
  55.  
  56. data . close ();
  57. return 0;
  58. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bis student is offline Offline
16 posts
since Dec 2007
Jan 10th, 2008
0

Re: need some help

try this

.....
.....
int main()
{     double scores[50];
      double avg ;    
      
      string names[50];  
	  int count=0;
	  ifstream data;
	  data.open("in.txt");

	while (!data.eof()&&count<=50)

	{
		data>> names[count] >> scores[count] ;
					count++;
	}

        double sum=scores[0],max = scores[0];

	for (int m=1; m<count; m++)
	{

		sum+= scores[m];

			
			if (scores[m]>max)
			{
				max= scores[m];
				
			}
	}
....
....
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HeLP I need it today
Next Thread in C++ Forum Timeline: seeking a Project idea on c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC