test scores

So if i have to write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.

Input Validation: Do not accept negative numbers for test scores.

Did i do this right??? Although I think I missed something at the bottom, but the program still works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	#include <iostream>
	#include <iomanip>
	 
	using namespace std;
	 
	void showArr( double* arr, int size )
	{
	   for( int i = 0; i < size; ++i )
	      cout << setw(8) << arr[i];
}
	 
// Prototypes
void arrSelectSort( double*, int );
	 
int main()
{
   cout << "How many test scores (1..10) do you wish "
        << "to enter? " << flush;
   int numTests;
   cin >> numTests;
   while( !cin.good() || numTests < 1 || numTests > 10 )
   {
      cout << "Enter an integer in range 1..10 : "
           << flush;
      cin.clear(); // clear cin stream error flags
      cin.sync(); // 'super' cin stream
      cin >> numTests;
   }
   cin.sync();
   double* testScores = new double[numTests];
	 
   cout << "Enter the test scores below.\n";
   double  sumTestScores = 0.0;
   for( int super = 0; super < numTests; ++super )
   {
      cout << "Test Score " << (super + 1)
           << ": " << flush;
      cin >> testScores[super];
	 
// Only numbers in range 0.0..100.0 valid
      while( !cin.good() ||
             testScores[super] < 0.0 ||
             testScores[super] > 100.0 )
      {
         cout << "Valid score range is 0..100"
              << "  Please enter again: ";
         cin.clear();
         cin.sync();
         cin >> testScores[super];
      }
      cin.sync();
      sumTestScores += testScores[super];
   }
	 
// Display the results
   cout << "\nYou entered testScores: \n";
   cout << fixed << showpoint << setprecision(2);
   showArr( testScores, numTests );
	    
   cout << "\nThe average score is "
        << sumTestScores / numTests << endl;
	 
//Display the Test Scores in ascending order
  cout << "The test scores, sorted in ascending "
       << "order, are: \n";
	         
   arrSelectSort( testScores, numTests );
// now display ...
   showArr( testScores, numTests );
	 
// Free dynamically allocated memory ...
   delete [] testScores;
   testScores = 0; // make testScores point to null
	 
// to keep window open until 'Enter' pressed
   cin.get();
}
// This function performs an ascending order
// selection sort
void arrSelectSort( double* arr, int size )
{
}
any help?
what kind of help do you want?
just for addition, i have a nightmare with OR logic, this code (probably) should be:

 
while( !cin.good() || numTests < 1 || numTests > 10 )


should be;

 
while( cin.good() && (numTests >= 1 && numTests <= 10))


otherwise, the loops will continue even if:

1. it's a bad input (but the rest of the condition(s) is true)
2. the numTests is 0 or negative
3. numTests greater that 10

CMIIW
@chipp: Nope, the loop is supposed to end when everything is fine. Your loop goes as long as anything is fine
sorry, my bad... didn't read the whole code...
I just wanted to know if I did it correctly and If I was missing anything at the bottom under void arrSelectSort
Looks good to me, although you could remove all of the cin.sync() calls.

Setting testScores to 0 at the end isn't necessary.
Thanks, the only thing i'm alittle confused about is how to sort the numbers. I know I need a for loops. But can I create my own variables or do I have to use the ones from before?
In your sort function, the only variables that will be defined for you when you enter it are arr and size. If you need more variables to sort the array, of course you should declare/define/use them.
I tried this and it didn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void arrSelectSort( double* arr, int size )
{
	int num1,num2,num3;

	for(num1=0;num1<(size-1);num1++)
	{
		num2=num1;
		num3=array[num1];
		for(int index=num1+1;index<size;index++)
		{
			if(array[index<num3)
			{
					num3=array[index];
					num2=index;
			}
		}
		array[num2]=array[num1];
		array[num1]=num3;
	}
}

Topic archived. No new replies allowed.