need some help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 16
Reputation: bis student is an unknown quantity at this point 
Solved Threads: 0
bis student's Avatar
bis student bis student is offline Offline
Newbie Poster

need some help

 
0
  #1
Jan 10th, 2008
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 .
  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. }
there are always hope
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 48
Reputation: amt_muk is an unknown quantity at this point 
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: need some help

 
0
  #2
Jan 10th, 2008
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];
				
			}
	}
....
....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC