test scores #1

book question:
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.

teachers added notes:
Make sure that you use pointers in the program wherever possible.
The program is to ask the user for the number of test scores.

my problem:
I'm at the last stretch of my college semester, and I don't have enough time to really think this one through to finish it. if some one could help me figure out what im doing wrong that would be appreciated



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 #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];

}

// Function prototypes
void arrSelectSort(double*, int);

int main()
{

	int numTests;

	cout << "How many test scores (1..10) do you wish "
	<< "to enter? " << flush;
	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(); // 'flush' cin stream
			cin >> numTests;

		}

		cin.sync();

		double* testScores = new double[numTests];

		cout << "Enter the test scores below.\n";
		
	    double  sumTestScores = 0.0;
		
	for (int count = 0; count < numTests; ++count)

		{

				cout << "Test Score " << (count + 1)
				<< ": " << flush;
				cin >> testScores[count];

			// Only numbers in range 0.0..100.0 valid
			while (!cin.good() ||

					testScores[count] < 0.0 ||
					testScores[count] > 100.0)

				{

					cout << "Valid score range is 0..100"
					<< "  Please enter again: ";
					cin.clear();
					cin.sync();
					cin >> testScores[count];

				}

			cin.sync();
				sumTestScores += testScores[count];

		}

		// 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)
{

	double total = 0;
	int numTest;
	double average;

	for (int count = 0; count < numTest; count++)
	{

		total += numTest[count];

		average = total / numTest;

}
Why do you think you're doing something wrong? What problems are you seeing with this code?
I think there is something wrong with the end
Error:
Severity Code Description Project File Line
Error C2109 subscript requires array or pointer type

part of code it is talking about needs to be pointer according to teacher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void arrSelectSort(double* arr, int size)
{

	double total = 0;
	int numTest;
	double average;

	for (int count = 0; count < numTest; count++)
	{

		total += numTest[count];

		average = total / numTest;

	}
}
total += numTest[count]; numTest is an ordinary int so you can'y use [].
Your function should probably look like this:
1
2
3
4
5
6
7
8
9
10
11
12
void arrSelectSort(double* arr, int size)
{
	double total = 0;
	int numTest; // not needed
	double average = 0; // not needed but good practice

	for (int count = 0; count < size; count++)
	{
		total += arr[count];
		average = total / numTest;
	}
}

Somehow you need to return average otherwise it's lost. So either change the return type to double or pass a reference or pointer to the function.
BTW: The name of the function is totally misleading.
Last edited on
#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];

}

// Function prototypes
void arrSelectSort(double*, int);

int main()
{

int numTests;

cout << "How many test scores (1..10) do you wish "
<< "to enter? " << flush;
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(); // 'flush' cin stream
cin >> numTests;

}

cin.sync();

double* testScores = new double[numTests];

cout << "Enter the test scores below.\n";

double sumTestScores = 0.0;

for (int count = 0; count < numTests; ++count)

{

cout << "Test Score " << (count + 1)
<< ": " << flush;
cin >> testScores[count];

// Only numbers in range 0.0..100.0 valid
while (!cin.good() ||

testScores[count] < 0.0 ||
testScores[count] > 100.0)

{

cout << "Valid score range is 0..100"
<< " Please enter again: ";
cin.clear();
cin.sync();
cin >> testScores[count];

}

cin.sync();
sumTestScores += testScores[count];

}

// 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)
{

double total = 0;
int numTest;
double average;

for (int count = 0; count < size; count++)
{
total += arr[count];
average = total / numTest;
}
}

Topic archived. No new replies allowed.