3 Replies - 398 Views - Last Post: 05 October 2012 - 04:45 PM Rate Topic: -----

#1 alexb9054  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-October 12

Help on finding average of a for loop

Posted 05 October 2012 - 03:40 PM

I need some help with finding the average of this for loop. The assignment requires that it be in individual functions.




#include <iostream>
#include <cmath>
#include <string>
#include <math.h>

using namespace std;

//Functions
int studentScore();
void average();
//Variables
int scoreInput[3];
int countScore;
double sAverage;

int main ()
{	
	studentScore();
	system("pause");
	return 0;

}

//Part 5
void average()
{
	sAverage = studentScore() / 4;
	cout << sAverage << endl;
	return average();
}

//Part 6
int studentScore()
{

	//Looping countScore and countInput 15x
	for (countScore = 1; countScore <= 3; countScore++)
	{
		cout << "Please Enter a Score: ";
		cin >>scoreInput[countScore];
	}

	for (countScore = 1; countScore <= 3; countScore++)
		{
			cin >>scoreInput[countScore];
			break;
		}
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help on finding average of a for loop

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Help on finding average of a for loop

Posted 05 October 2012 - 03:45 PM

What problems are you encountering?

As an aside, I can already see that you have a buffer overrun. Remember that C/C++ arrays are zero based, but you are accessing your arrays with indices 1 to 3.
Was This Post Helpful? 0
  • +
  • -

#3 alexb9054  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-October 12

Re: Help on finding average of a for loop

Posted 05 October 2012 - 03:49 PM

The problem I'm having isn't an error, but it's not averaging the numbers that the user inputs.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Help on finding average of a for loop

Posted 05 October 2012 - 04:45 PM

Well, ignoring for now that you have a buffer overrun in studentScore(), your function studentScore() is return 0, rather than the sum of the scores. So when you try to compute the average in average(), 0 divided by 4, results in an average of zero.

Assuming that you fix things such that you are computing the sum of the scores, you are only inputting 3 scores (twice) in studentScore(). Then later when you try to compute the average, you are dividing by 4.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1