Using pointer notation and array notation

Description: We have to use pointers (no structures). Ask user how many test scores they will be entering. Error trap for + input. In main, dynamically allocate an array to hold number of test scores. In main, fill an array with the scores that user inputs. Error trap for range of 0 to 100. Create a function to calculate average test score. In main display the average score. Then pass the array to a function that calculates how many people got an A on the test. In main, display the number of A grades. Finally, release the dynamically allocated memory at the end.
So far I have:

#include <iostream>
using namespace std;


int numTests;
double calcAverage(double* scores, int numTests);
char grade;
int *testsPtr;

double calcAverage(double* score, int numScores);
int howManyA(double* score, int numScores);

int main() {

cout << "How many test scores will there be?" << endl;
cin >> numTests;

while (numTests > 0) {
//Create a dynamic array
double* scores = new double[numTests];

for (int count = 0; count < numTests; count++) {
cout << "Enter the test score: " << (count + 1) << endl;
cin >> scores[count];
}
}

cout << "The average test score is: " << calcAverage(scores, numTests) << endl;
cout << "The number of A grades is: " << howManyA(scores, numTests) << endl;


}

double calcAverage(double* scores, int numTests) {
int total = 0;
double average;

for (int count = 0; count <= numTests; count++) {
total += numTests[count];
}
average = total / numTests;
return average;
}

int howManyA(double* score, int numTests)
{
//calculate how mmany As
int numA;

if (scores > 90 && scores <= 100)
numA++;

return numA;
}

I dont think Im using the pointer notation right. Also Im getting errors in these lines:
cout << "The average test score is: " << calcAverage(scores, numTests) << endl;
total += numTests[count];
total += numTests[count];
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
#include <iostream>
#include <cstdlib>
using namespace std;

double calcAverage(double *scores, int N)
{
	int i;
	double total = 0;
	for(i = 0; i < N; i++)
	{
		total += scores[i];
	}
	return total / (double)N;
}

int howManyA(double *scores, int N)
{
	int i;
	int count = 0;
	for(i = 0; i < N; i++)
	{
		if(scores[i] >= 90 && scores[i] <= 100) count++;
	}
	return count;
}

int main()
{
	int i;
	int N;
	double *scores;
	while(true)
	{
		cout << "How many test scores will there be? : "; 
		if(cin >> N && N >= 1 && N <= 200) break;

		if(!cin)
		{
			cin.clear();
			cin.ignore(1000, '\n');
		}

		cout << "Invalid number or number out-of-range. Please try again.\n\n";
	}

	scores = new double[N];

	for(i = 0; i < N; i++)
	{
		cout << "- Enter the test score " << i + 1 << " : "; 
		if(!(cin >> scores[i]) || scores[i] < 0 || scores[i] > 100)
		{
			if(!cin)
			{
				cin.clear();
				cin.ignore(1000, '\n');
			}
			cout << "Invalid score. Please try again.\n\n"; i--; continue;
		}
	}

	cout << endl;

	double average = calcAverage(scores, N);
	int scoreAcount = howManyA(scores, N);

	cout << "+ The average test score is : " << average << endl;
	cout << "+ The number of A grades is : " << scoreAcount << endl;

	cout << endl;

	delete [] scores;
	system("pause");
	return 0;
}


How many test scores will there be? : 10
- Enter the test score 1 : 66
- Enter the test score 2 : 99
- Enter the test score 3 : 64
- Enter the test score 4 : 37
- Enter the test score 5 : 86
- Enter the test score 6 : 88
- Enter the test score 7 : 90
- Enter the test score 8 : 100
- Enter the test score 9 : 70
- Enter the test score 10 : 90

+ The average test score is : 79
+ The number of A grades is : 4
Thank you!
But this is what I have now:

//Lab 5 Dynamic Memory Allocation
//

#include <iostream>

using namespace std;

int numTests;
double calcAverage(double* score, int numScores);
int howManyA(double* score, int numScores);

int main() {

int i;

cout << "How many test scores will there be?" << endl;
cin >> numTests;
//Create a dynamic array
double *scores;

while (numTests > 0) {
for (int count = 0; count < numTests; count++) {
cout << "Enter the test score: " << (count + 1) << endl;
cin >> scores[count];
}
}

scores = new double[numTests];

for (i = 0; i < numTests; i++) {
cout << "Enter the test score " << (i + 1) << " : ";
}
cout << endl;

double average = calcAverage(scores, numTests);
int scoreAcount = howManyA(scores, numTests);

cout << "The average test score is: " << average << endl;
cout << "The number of A grades is: " << scoreAcount << endl;

}

double calcAverage(double *scores, int numTests) {
int count;
double total = 0;
double average;

for (int count = 0; count <= numTests; count++) {
total += scores[count];
}
average = total / numTests;
return average;
}

int howManyA(double *scores, int numTests)
{
//calculate how mmany As
int i;
int numA = 0;

for (i = 0; i < numTests; i++) {
if (scores[i] >= 90 && scores[i] <= 100)
numA++;
}
return numA;
}

I am getting 2 errors where it says
count: unreferenced local variable
uninitialized local variable 'scores' used

sorry i dont know how to include the code lines in these comments
Last edited on
It is just a header so that we can use system("pause"); at the end of the program.
It simply displays "Press any key to continue. . ."
Please wrap your code in [code]Your code...[/code] so that your code becomes easier to read and you will get more helpful replies in the future.
Last edited on
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
#include <iostream>

using namespace std; 

int numTests;
double calcAverage(double* score, int numScores);
int howManyA(double* score, int numScores);

int main() {

int i;

cout << "How many test scores will there be?" << endl;
cin >> numTests;
//Create a dynamic array
double *scores;

while (numTests > 0) {
for (int count = 0; count < numTests; count++) {
cout << "Enter the test score: " << (count + 1) << endl;
cin >> scores[count];
}
}

scores = new double[numTests];

for (i = 0; i < numTests; i++) {
cout << "Enter the test score " << (i + 1) << " : ";
}
cout << endl; 

double average = calcAverage(scores, numTests); 
int scoreAcount = howManyA(scores, numTests);

cout << "The average test score is: " << average << endl;
cout << "The number of A grades is: " << scoreAcount << endl;

}

double calcAverage(double *scores, int numTests) {
int count;
double total = 0; 
double average;

for (int count = 0; count <= numTests; count++) {
total += scores[count];
}
average = total / numTests; 
return average; 
}

int howManyA(double *scores, int numTests)
{
//calculate how mmany As 
int i;
int numA = 0;

for (i = 0; i < numTests; i++) {
if (scores[i] >= 90 && scores[i] <= 100)
numA++;
}
return numA;
}

I am getting 2 errors where it says
count: unreferenced local variable line 45
uninitialized local variable 'scores' used line 25
Last edited on
The line numbers in your error messages don't match up with the code you've posted. Are you sure you're giving us the actual code you're compiling?

At line 21 in the code you've posted, you're trying to access the count-th element of scores, but you haven't yet initialised scores to point to an array. You don't actually initialise scores until line 25 of the code you've posted.

At line 41, you declare a variable count, but you never actually use it - although you do later define another variable called count that shadows it.

EDIT: it would really help both you and us read your code if you used a sensible indentations style.
Last edited on
Okay so I fixed those lines:
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
#include <iostream>

using namespace std; 

int numTests;
double calcAverage(double* score, int numScores);
int howManyA(double* score, int numScores);

int main() {

	int i;

	cout << "How many test scores will there be?" << endl;
	cin >> numTests;
	//Create a dynamic array
	double *scores;
	scores = new double[numTests];

	while (numTests > 0) {
			for (int count = 0; count < numTests; count++) {
			cout << "Enter the test score: " << (count + 1) << endl;
			cin >> scores[count];
		}
	}

	for (i = 0; i < numTests; i++) {
		cout << "Enter the test score " << (i + 1) << " : ";
	}
	cout << endl; 

	double average = calcAverage(scores, numTests); 
	int scoreAcount = howManyA(scores, numTests);

	cout << "The average test score is: " << average << endl;
	cout << "The number of A grades is: " << scoreAcount << endl;

}

double calcAverage(double *scores, int numTests) {
	//int count;
	double total = 0; 
	double average;

	for (int count = 0; count <= numTests; count++) {
		total += scores[count];
	}
	average = total / numTests; 
	return average; 
 }

int howManyA(double *scores, int numTests)
{
	//calculate how mmany As 
	int i;
	int numA = 0;
	
	for (i = 0; i < numTests; i++) {
		if (scores[i] >= 90 && scores[i] <= 100)
			numA++;
	}
	return numA;
}


Output:

How many test scores will there be?
7
Enter the test score: 1
90
Enter the test score: 2
80
Enter the test score: 3
70
Enter the test score: 4
96
Enter the test score: 5
60
Enter the test score: 6
87
Enter the test score: 7
67
Enter the test score: 1
90

So it keeps looping instead of displaying the average and number of grade As
You forgot to delete the dynamic array at the end of the program.
So it keeps looping instead of displaying the average and number of grade As


Look at your loop:

19
20
21
22
23
24
        while (numTests > 0) {
			for (int count = 0; count < numTests; count++) {
			cout << "Enter the test score: " << (count + 1) << endl;
			cin >> scores[count];
		}
	}


When do you think that loop will end?

EDIT: Also, it's good practise to initialise your variables right away, rather than declaring them uninitialised, and then assigning a value to them later. So:

16
17
18
	//Create a dynamic array
	double *scores;
	scores = new double[numTests];


Would be better as:

16
17
	//Create a dynamic array
	double *scores = new double[numTests];


Also, insert obligatory grumble about tab characters here...
Last edited on
Thank you everyone who helped. Here is my final, working code.

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
#include <iostream>
#include <iomanip>

using namespace std;

int numTests;
double calcAverage(double* score, int numScores);
int howManyA(double* score, int numScores);

int main() {
	cout << setprecision(1) << fixed << setw(1) << endl;

	cout << "How many test scores will there be?" << endl;
	cin >> numTests;
	while (numTests <= 0) {
		cout << "Please re-enter a positive number: \n";
		cin >> numTests;
	}

	//Create a dynamic array
	double *scores;
	scores = new double[numTests];

	for (int count = 0; count < numTests; count++) {
		cout << "Enter the test score " << (count + 1) << ": " << endl;
		cin >> scores[count];
		while (scores[count] <= 0 || scores[count] > 100) {
			cout << "Please a score between 0 and 100: \n";
			cin >> scores[count];
		}
	}

	double average = calcAverage(scores, numTests);
	int scoreAcount = howManyA(scores, numTests);

	cout << "The average test score is: " << average << endl;
	cout << "The number of A grades is: " << scoreAcount << endl;

	delete[] scores; 
}

//***********************************************************************
//This function takes in the array and calculates the average test score*
//***********************************************************************

double calcAverage(double *scores, int numTests) {
	
	
	
	double total = 0;
	double average;

	for (int count = 0; count < numTests; count++) {
		total += scores[count];
	//	total += *(scores + count);
		
	}

	average = total / numTests;
	return average;
}

//***************************************************************
//This function calculates how many people go tan A on the test *
//***************************************************************

int howManyA(double *scores, int numTests)
{
	//calculate how mmany As 
	int i;
	int numA = 0;

	for (i = 0; i < numTests; i++) {
		if (scores[i] >= 90 && scores[i] <= 100)
			numA++;
	}
	return numA;
}
Topic archived. No new replies allowed.