Write a Program That Dynamically Allocates an Array to Hold a User Defined Number of Test Scores.

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:
had similar submission, but i changed code around a bit. my problem is no errors, but it's not showing average. also if you could make sure i used pointers where ever possible that be great.

                          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
                                                      #include <iostream>                            #include <iomanip>                            using                            namespace                            std;                            void                            arrSelectSort(double                            *,                            int);                            void                            showArrPtr(double                            *,                            int);                            double                            showAverage(double,                            int);                            int                            main() {                            double                            *scores,                            //To dynamically allocate an array                            total = 0.0,                            //Accumulator                            average;                            //To hold the averge scores                            int                            numScores;                            //To hold the number of test scores                            //Get the number of test scores.                            cout <<                            "How many test scores would you like to process?"; 	cin >> numScores;                            //Dynamically allocate an array large enough to hold that many test scores                            scores =                            new                            double[numScores];                            if                            (scores == NULL)                            return                            0;                            //Get the test score for each test                            cout <<                            "Enter the test scores below.\n";                            for                            (int                            count = 0; count < numScores; count++) 	{ 		cout <<                            "Test score #"                            << (count + 1) <<                            ": "; 		cin >> scores[count];                            while                            (scores[count] <= 0) 		{ 			cout <<                            "Zero or negative numbers not accepted.\n"; 			cout <<                            "Test Score #"                            << (count + 1) <<                            ": "; 			cin >> scores[count]; 		} 	}                            //Calculate the total scores                            for                            (int                            count = 0; count < numScores; count++) 	{ 		total += scores[count]; 	}                            //sort the elements of the array pointers                            arrSelectSort(scores, numScores);  	cout <<                            "The test scores in ascending order are: \n"; 	showArrPtr(scores, numScores); 	showAverage(total, numScores);                            return                            0;  }                            void                            arrSelectSort(double                            *array,                            int                            size) {                            int                            startScan, minIndex;                            double                            minElem;                            for                            (startScan = 0; startScan < (size - 1); startScan++) 	{ 		minIndex = startScan; 		minElem = array[startScan];                            for                            (int                            index = startScan + 1; index < size; index++) 		{                            if                            (array[index]  < minElem) 			{ 				minElem = array[index]; 				minIndex = index; 			} 		} 		array[minIndex] = array[startScan]; 		array[startScan] = minElem; 	} }                            void                            showArrPtr(double                            *array,                            int                            size) {                            for                            (int                            count = 0; count< size; count++) 		cout << array[count] <<                            " "; 	cout << endl; }                            double                            showAverage(double                            total,                            int                            numScores) {                            double                            average;                            //Calculate the average                            average = total / numScores;                            return                            average;                            //Display the results.                            cout << fixed << showpoint << setprecision(2); 	cout <<                            "Average Score: "                            << average << endl; 	system("pause"); }                        

That's because lines 96 - 98 will never execute. Can you see why?

Last edited on

got it to work, but forgot to post.

                          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
                                                      #include <iostream>                            #include <iomanip>                            using                            namespace                            std;                            void                            arrSelectSort(double                            *,                            int);                            void                            showArrPtr(double                            *,                            int);                            void                            showAverage(double,                            int);                            int                            main() {                            double                            *scores,                            //To dynamically allocate an array                            total = 0.0;                            //Accumulator                            //average;                             //To hold the averge scores                            int                            numScores;                            //To hold the number of test scores                            //Get the number of test scores.                            cout <<                            "How many test scores would you like to process?"; 	cin >> numScores;                            //Dynamically allocate an array large enough to hold that many test scores                            scores =                            new                            double[numScores];                            if                            (scores == NULL)                            return                            0;                            //Get the test score for each test                            cout <<                            "Enter the test scores below.\n";                            for                            (int                            count = 0; count < numScores; count++) 	{ 		cout <<                            "Test score #"                            << (count + 1) <<                            ": "; 		cin >> *(scores + count); 		 	}                            //Calculate the total scores                            for                            (int                            count = 0; count < numScores; count++) 	{ 		total += *(scores + count); 	} 	showAverage(total, numScores);                            //sort the elements of the array pointers                            arrSelectSort(scores, numScores);  	cout <<                            "The test scores in ascending order are: \n"; 	showArrPtr(scores, numScores);                            //Free memory.                            delete[] scores; 	scores = 0;                            return                            0;  	 }                            // bubble sort                                                        void                            arrSelectSort(double                            *array,                            int                            size) {                            int                            temp;                            bool                            swap;                            do                            {  		swap =                            false;                            for                            (int                            count = 0; count < (size - 1); count++) 		{                            if                            (*(array + count) > *(array + count + 1)) 			{  				temp = *(array + count); 				*(array + count) = *(array + count + 1); 				*(array + count + 1) = temp; 				swap =                            true; 			} 		} 	}                            while                            (swap); }                            // sort function                            void                            showArrPtr(double                            *array,                            int                            size) {                            for                            (int                            count = 0; count< size; count++) 		cout << *(array + count) <<                            " "; 	cout << endl; }                            // average function                            void                            showAverage(double                            total,                            int                            numScores) {                            double                            average;                            //Calculate the average                            average = total / numScores;                            //Display the results.                            cout << fixed << showpoint << setprecision(2); 	cout <<                            "Average Score: "                            << average << endl;  }                        

you don't have to change to a bubble sort, but me and the teacher couldn't figure out after a few needed changes why my program wasn't working. changed to a bubble sort and come to realize I had an asterisk inside a one of the array + count codes

like this:

                          1
2
3
4
5
                                                      //wrong                            (*array + count)                            //right                            *(array + count)                        

Topic archived. No new replies allowed.

Write a Program That Dynamically Allocates an Array to Hold a User Defined Number of Test Scores.

Source: http://cplusplus.com/forum/general/214365/

0 Response to "Write a Program That Dynamically Allocates an Array to Hold a User Defined Number of Test Scores."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel