My assignment is to write a c++ program which does statistical analysis of a group of exam scores. Up to 100 scores may be entered. All scores should be validated within the range of 0-100. A (-1) will be entered to signify the end of data. Scores may be decimal, i.e 85.6. After all the data has been entered, the program will produce the following statistics:
1) the number of scores
2)The maximum score
3)The minimum score
4) The mean score
5) The median score
6) The number of A's B's C's D's and F's using 90, 80,70, 60 cut offs....


I have done the first 4 but for some reason, now, my code will output a duplicate number in the sort and i don't know why. I have spent the last couple of hours trying to work around it or sort it out but I haven't come up with a solution. Any advice/help would be great.

#include <iostream>
using namespace std;

int average_score ( int *src, int size )
{
   int sum = 0;

   for ( int i = 0; i < size; i++ )
       sum += src[i];

   return sum / size;
}
void sort_descending ( int *dst, int *src, int size )
{
   for ( int i = 0; i < size; i++ )
       dst[i] = src[i];
   for ( int i = size - 1; i > 0; i-- ) 
   {
       for ( int j = 0; j < i; j++ ) 
	   {
           if ( dst[j] < dst[j + 1] ) 
		   {
               int save = dst[j];
               dst[j] = dst[j + 1];
               dst[j + 1] = save;
           }
       }
   }
}
void sort_ascending ( int *dst, int *src, int size )
{
   for ( int i = 0; i < size; i++ )
       dst[i] = src[i];
   for ( int i = size - 1; i > 0; i-- ) {
       for ( int j = 0; j < i; j++ ) {
           if ( dst[j] > dst[j + 1] ) {
               int save = dst[j];
               dst[j] = dst[j + 1];
               dst[j + 1] = save;
           }
       }
   }
}


int main()
{
   int scores[101];
   int descend[101];
   int ascend[101];
   int size = 0, count=0, pos=0, num_tests;


   while (size < 101 && count == 0)
   {
	   cout<<"Score? ";
	   cin>>scores[pos];
	   while (scores[pos] > 100 || scores[pos] < -1)
	   {
		   cout<<"Please use an integer between 0 and 100"<<endl;
		   cout<<"Score? ";
		   cin>> scores[pos];
	   }
	   if (scores[pos]== -1)
	   {
		   count=1;
		   scores[pos]= scores[pos-1];
		   num_tests= pos;

	   }
	   size++;
	   pos++;
   }

	
   
   
   sort_descending ( descend, scores, size );
   sort_ascending ( ascend, scores, size );
   cout<<"Scores:\n";
   for ( int i = 0; i < size; i++ )
       cout<< descend[i]<<"\t";
   cout<<"\nThere were "<<num_tests<<" scores entered"<<endl;
   cout<<"The maximum score was "<< descend[0] <<'\n';
   cout<<"The minimum score was "<< ascend[0] <<'\n';
   cout<<"The mean score was "<< average_score ( scores, size ) <<endl;
}

I think some small changes are all you need.

if (scores[pos]== -1)
	   {
		   count=1;  //you could make this a "break;" statement to get you out of the loop (move it after the next statement, though, then take out your second condition in the while loop)
		   //scores[pos]= scores[pos-1];  here was the problem
                   //you would then write the previous good score over the
                    // -1 in the array?
		   num_tests= size;
		   //also num_tests should be size, I think so you're not
                   //getting the extra score in your calculation
	   }
	   else
                 size++;

thanks for the suggestion. I had already figured it out. I meant to mark this as solved..Sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.