# Finding Average of Dynamically Allocated Array

Page 1 of 1

## 3 Replies - 528 Views - Last Post: 05 November 2012 - 03:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298717&amp;s=efd742e93c3f3a808425f57dacc57d7e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 2
• Posts: 36
• Joined: 08-October 12

# Finding Average of Dynamically Allocated Array

Posted 04 November 2012 - 10:26 PM

Basically, this program is supposed to take test scores and find the average, highest, and lowest score. So Far I've got the program taking in the test scores, and I'm trying to get it to find the average correctly. The output I'm getting is making no sense. I've tried to tweak/ add several things, but nothing is really making the issue clear to me. Any helps is greatly appreciated, here is my code:

```// Function prototypes
//********************************************
// Function average                          *
// This function calculates and returns the  *
// average of the values stored in the array *
// passed into the scores parameter. The     *
// parameter numScores holds the number of   *
// elements in the array.                    *
//********************************************

double average(double* testScores, int numScores);
double highest(double* testScores, int numScores);
double lowest(double* testScores, int numScores);

#include <iostream>
using namespace std;

int main()
{
double *testScores,
total = 0.0;
int numScores = 0;

while (numScores <= 0)
{
cout << "How many test scores will you enter? ";
cin >> numScores;
if (numScores <= 0)
{
cout << "The number of test scores cannot be negative or 0. " << endl;
}
}

testScores = new double[numScores];

for (int count = 1; count <= numScores; count++)
{
cout << "Enter test score " << count << ": ";
cin >> testScores[count];
}

cout << "Average score: " << average(testScores, numScores);

system("pause");
return 0;
}

double average(double* testScores, int numScores)
{
double sum = 0,
average;

for (int count = 0; count < numScores; count++)
{
sum = (sum + testScores[count]);
}

average = (sum / numScores);

return average;
}
```

And here is the output:

Quote

How many test scores will you enter? 3
Enter test score 1: 90
Enter test score 2: 85
Enter test score 3: 75
Average score: -2.09248e+066Press any key to continue . . .

Is This A Good Question/Topic? 0

## Replies To: Finding Average of Dynamically Allocated Array

### #2 #define

• Duke of Err

Reputation: 1633
• Posts: 5,752
• Joined: 19-February 09

## Re: Finding Average of Dynamically Allocated Array

Posted 04 November 2012 - 10:59 PM

When count is used as an index it should start at 0 and end at numScores-1.

```38    for (int count = 1; count <= numScores; count++)
39    {
40        cout << "Enter test score " << count << ": ";
41        cin  >> testScores[count];
42    }

```

Whether that causes your problem I don't know.

### #3 Coding Mash

Reputation: 4
• Posts: 26
• Joined: 29-September 12

## Re: Finding Average of Dynamically Allocated Array

Posted 05 November 2012 - 06:39 AM

The problem is with the input loop.
You are not correctly referring to its indices.
The correct loop would be like this.
```for (int count = 0 ; count < numScores ; count++)
{
cout << "Enter test score " << count + 1 << ": " ;
cin >> testScores[count];
}
```

This post has been edited by jimblumberg: 05 November 2012 - 06:52 AM
Reason for edit:: Added missing Code Tags, Please learn to use them. You should know this by now.

Reputation: 2
• Posts: 36
• Joined: 08-October 12

## Re: Finding Average of Dynamically Allocated Array

Posted 05 November 2012 - 03:07 PM

Awesome, thank you very much Coding Mash, changing that fixed it!