# Basic Function To Return Average of Three Numbers

Page 1 of 1

## 11 Replies - 6515 Views - Last Post: 18 March 2009 - 12:35 AMRate 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=93491&amp;s=e2e00e4fa3067757cfbeaebf5b6b5ff8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 26
• Joined: 10-February 09

# Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 12:58 PM

I'm trying to make a program that will ask for 3 different numbers, and give a decimal average, however, it needs to be executed in a function, then returned.

Here's my attempt: (I'm using Visual C++ 2009 Express)

```#include <iostream>

using namespace std;

double MultThree(double,double,double);
double a,b,c,d,e;

int Main()
{

cout<< "Please enter the three scores:" <<endl;
cin>>A;
cin>>B;
cin>>C;
e = MultThree();

cout<<e<<endl;
system("PAUSE");
return 0;
}

double MultThree(a, b, c)
{
return (A * B * C / 3);
}

```

I don't know if it's a syntax or logic error, or if the express version just doesn't come with the libraries.

Is This A Good Question/Topic? 0

## Replies To: Basic Function To Return Average of Three Numbers

### #2 carolinaswamp

Reputation: 1
• Posts: 7
• Joined: 30-July 08

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 01:06 PM

The declaration of the MultThree needs to define the type for variables a, b, c;

When you call MultThree, you need to pass it a, b, c.

In the code for MultThree, you define that the variables a, b, c are passed in but you use A, B, C. These are different. The variables you declare as parameters should be the variables you use in your function.

### #3 Notorion

• D.I.C Regular

Reputation: 35
• Posts: 378
• Joined: 17-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 01:07 PM

```double MultThree(double,double,double);
//double a,b,c,d,e;  //move these into the main function

int Main()
{
double a,b,c,d,e;  //moved inside main function
//make the variables lower case.
cout<< "Please enter the three scores:" <<endl;
cin>>A;
cin>>B;
cin>>C;
e = MultThree();

cout<<e<<endl;
system("PAUSE");
return 0;
}

double MultThree(a, b, c)
{
return (A * B * C / 3);  //make variables lower case
}

```

For you it was more of a syntax error.
C and C++ are case sensitive

Reputation: 0
• Posts: 26
• Joined: 10-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 01:48 PM

Alright. I've made some changes to it, but now its not telling me what the error is, it just isn't running.

```
double MultThree(double,double,double);

int Main()
{
double a,b,c,d;

cout<< "Please enter the three scores:" <<endl;
cin>>a;
cin>>b;
cin>>c;
d = MultThree(a,b,c);

cout<<d<<endl;
system("PAUSE");
return 0;
}

double MultThree(double a,double b,double c)
{
return (a * b * c / 3);
}

```

### #5 carolinaswamp

Reputation: 1
• Posts: 7
• Joined: 30-July 08

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 02:09 PM

Not sure what you mean.

When you compile the program, are there errors? There should be.

int Main() should have a lower case m.

What are the #includes that you have at the top of your file? Are you using namespace std?

Also, if you are trying to calculate the average of those three numbers, you need to follow the correct order of operations.

(a * b * c) / 3 - this will multiply all three numbers before dividing by three.

Reputation: 0
• Posts: 26
• Joined: 10-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 04:02 PM

Well, I'm not quite sure what's going on. Apparently when i run the average of 23, 24, and 25, the answer is 4600!?

Here's what I have:
```#include <iostream>

using namespace std;

double MultThree(double,double,double);

int main()
{
double a,b,c,d;

cout<< "Please enter the three scores:" <<endl;
cin>>a;
cin>>b;
cin>>c;
d = MultThree(a,b,c);

cout<<d<<endl;
system("PAUSE");
return 0;
}

double MultThree(double a,double b,double c)
{
return ((a * b * c)/3);
}

```

### #7 Notorion

• D.I.C Regular

Reputation: 35
• Posts: 378
• Joined: 17-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 04:11 PM

Yes, this is a logic error
When averaging, you want to ADD, all the numbers up, then divide by the total amount of numbers.

You were multiplying the numbers, not adding.
```double MultThree(double a,double b,double c)
{
//	return ((a * b * c)/3);
return ((a + b + c)/3);

}

```

Reputation: 0
• Posts: 26
• Joined: 10-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 04:35 PM

Roflmao....

----FACEPALM----

Nuff said.

### #9 Hyper

• Banned

Reputation: 108
• Posts: 2,129
• Joined: 15-October 08

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 04:36 PM

Madamayo, on 17 Mar, 2009 - 06:35 PM, said:

Roflmao....

----FACEPALM----

Nuff said.

Reputation: 0
• Posts: 26
• Joined: 10-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 05:05 PM

Yes, thank you very much.

### #11 Newfie Ken

Reputation: 2
• Posts: 30
• Joined: 22-February 09

## Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 06:34 PM

Here's another way to do what you were trying to accomplish, and for a laugh I added a loop so you can make 3 number averages as many times as your heart so desires without having to close the program .

```#include "stdafx.h"
#include <iostream>

using namespace std;

double multthree(), a, b, c, d;
int choice;

int main()
{
system ("TITLE Average Calculator (3 Numbers)");
system ("COLOR 0c");
restart:
system ("CLS");
cout << "Please enter the three scores: " << endl << endl;
cout << "Score 1: ";
cin >> a;
cout << endl << "Score 2: ";
cin >> b;
cout << endl << "Score 3: ";
cin >> c;
d = multthree();
cout << endl << endl << "Average: " << d << endl << endl;

cout << endl << "What would you like to do now?" << endl << endl;
cout << "1. New" << endl;
cout << "2. Esc" << endl << endl;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 1:
goto restart;
case 2:
{
}
}

return 0;
}
double multthree()
{
return (a + b + c) / 3;
}
```

I moved some stuff around, and to me, it looks a lot cleaner making it easier to read. It's up to you what you do though .

### #12 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

## Re: Basic Function To Return Average of Three Numbers

Posted 18 March 2009 - 12:35 AM

... and using standard C++

```// this program averages a user determined number of numbers
// and demos using a dynamic array ...
// and some input validation ...

#include <iostream>
using namespace std;

double getAverage( double ary[], int numElements )
{
double sum = 0;
for( int i = 0; i<numElements; ++i )
sum += ary[i];

return sum/numElements;
}

bool more()
{
cin.clear();
cin.sync();
cout << "\nMore ... (y/n) ? " << flush;
}

int main()
{
do
{
cout << "How many numbers do you want to average ? " << flush;
int num=0;
cin >> num;
// validate num ...
if( num == 0 ) { cin.clear(); cin.sync(); continue; }

// get some memory to hold your array of doubles ...
double* dAry = new double [num];

double sum = 0;
cout << "Please enter " << num << " scores ...\n";
for( int i=0; i<num; )
{
cout << "dAry[" << i << "] = " << flush;
cin >> dAry[i];
if( !cin.good() )
{
cin.clear();
cin.sync();
cout << "Entry error ... redo." << endl;
continue; // from the top of the for loop right now ...
}
sum += dAry[i++]; // increment i here ... (since this value was ok)
}

cout << "The average done during input was   : " << sum/num << endl
<< "The average done from the array was : " << getAverage( dAry, num )
<< endl;

// free up the memory that was reserved ...
delete [] dAry;

}while( more() );
}
```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }