pointers and arrays questions
Apr 28, 2018 at 3:37am UTC
I'm wondering how I can calculate the average but instead of calculating all the numbers in the array, the first one is dropped. Then the second and so fourth.
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
#include <iostream>
#include <iomanip>
using namespace std;
void sort(double * score, int );
double average(double * score, int );
int main()
{
int numTestScores;
double *testScorePtr;
double testAverage;
cout << "\n\tHow many test scores will you enter? " ;
cin >> numTestScores;
while (numTestScores < 0)
{
cout << "\n\tThe number cannot be negative.\n" ;
cout << "\tEnter another number: " ;
cin >> numTestScores;
}
testScorePtr = new double [numTestScores];
for (int count = 0; count < numTestScores; count++)
{
cout << "\n\tEnter The test score for test # " << (count + 1)
<< ": " ;
cin >> testScorePtr[count];
while (testScorePtr[count] < 0)
{
cout << "\n\tThe number cannot be negative.\n" ;
cout << "\tEnter another number: \n" ;
cin >> numTestScores;
}
}
sort(testScorePtr, numTestScores);
testAverage = average(testScorePtr, numTestScores);
cout << "\tThe average of all tests: " << testAverage;
return 0;
}
void sort(double * score, int size)
{
int startScan, minIndex;
double minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = *(score + startScan);
for (int index = startScan + 1; index < size; index++)
{
if (*(score + index) < minValue)
{
minValue = *(score + index);
minIndex = index;
}
}
*(score + minIndex) = *(score + startScan);
*(score + startScan) = minValue;
}
}
double average(double *score, int size)
{
double total = 0.00;
double totalAverage;
int index = 0;
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < size; count++)
{
total += *score;
score++;
}
totalAverage = total / size;
return totalAverage;
}
Apr 28, 2018 at 7:03am UTC
One way is to pass the second elem of your array to the average function, then the third , fourth....
Topic archived. No new replies allowed.