11 Replies - 5461 Views - Last Post: 18 March 2009 - 12:35 AM Rate Topic: -----

#1 Madamayo  Icon User is offline

  • New D.I.C Head

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

Basic Function To Return Average of Three Numbers

Post icon  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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 1

#3 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • 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
Was This Post Helpful? 1

#4 Madamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}



Was This Post Helpful? 0
  • +
  • -

#5 carolinaswamp  Icon User is offline

  • New D.I.C Head

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

#6 Madamayo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}


Was This Post Helpful? 0
  • +
  • -

#7 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • 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);

}


Was This Post Helpful? 0
  • +
  • -

#8 Madamayo  Icon User is offline

  • New D.I.C Head

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

#9 Hyper  Icon User is offline

  • Banned

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

Re: Basic Function To Return Average of Three Numbers

Posted 17 March 2009 - 04:36 PM

View PostMadamayo, on 17 Mar, 2009 - 06:35 PM, said:

Roflmao....


----FACEPALM----

Nuff said.


Does that mean you have your answer?
Was This Post Helpful? 0
  • +
  • -

#10 Madamayo  Icon User is offline

  • New D.I.C Head

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

#11 Newfie Ken  Icon User is offline

  • New D.I.C Head

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

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • 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 reply = cin.get();
    return !( reply == 'n' || reply == 'N' );
}

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() );
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1