3 Replies - 339 Views - Last Post: 05 November 2012 - 03:07 PM Rate Topic: -----

#1 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 Coding Mash  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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.

Was This Post Helpful? 0
  • +
  • -

#4 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1