Problem with writing C++ code for a function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1553 Views - Last Post: 17 November 2011 - 04:07 AM Rate Topic: -----

#1 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Problem with writing C++ code for a function

Posted 15 November 2011 - 07:04 PM

First I want to say that I'm 40+ years old and taking my first programming class. It's an online class and it's being taught by a part time instructor that has not been very helpful.

This is my first time posting here and hopefully I'm doing it correctly. If not, my apologies. I noticed the "Do Not: Copy/Paste your homework assignment", but I'm not sure how else to explain my problem. Just so you know I'm not expecting anyone to do my assignment, just guide me. It should be a simple exercise, but I've been working on it for hours and have not been able to figure it out.

First is the assignment and then my code. There are no errors, but the program does not do what it should. Instead of cleaning the code up, I thought it might be helpful if you can see the different things that I tried and commented out.

Hopefully someone will help point me in the right direction. I have 28 hours to finish and submit it.

Brian


----------
Write the C++ code for a function that receives four double numbers. The function should calculate the average of the four numbers and then return the result. Name the function calcAverage. Name the formal parameters num1, num2, num3, and num4. Also write an appropriate function prototype for the calcAverage function. In addition, write a statement that invokes the calcAverage function and assigns its return value to a double variable named quotient. Use the following numbers as the actual arguments: 45.67, 8.35, 125.78, and 99.56.


#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
double calcAverage(double, double, double, double);

int main()
{

	//declare variables
	double quotient = 0.0;
	double num1 = 0.0;
	double num2 = 0.0;
	double num3 = 0.0;
	double num4 = 0.0;

	//call function to get the character
	//quotient = calcAverage(45.67, 8.35, 125.78, 99.56 / 4);

	//display the quotient
	cout << "quotient: " << quotient << endl;

	system("pause");
	return 0;
} //end of main function

//*****function definitions*****
double calcAverage(double num1, double num2, double num3, double num4, double quotient)
{
	//double num1 = 45.67, num2 = 8.35, num3 = 125.78, num4 = 99.56, quotient = 0.0;
	//double num1, num2, num3, num4, quotient = 0.0;
	//double quotient = 0.0;
	//quotient = calcAverage(45.67, 8.35, 125.78, 99.56);
	quotient = (45.67 * 8.35 * 125.78 * 99.56)/4;
	//quotient = num1 * num2 * num3 * num4;
	return quotient;
} //end getCharacter function



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with writing C++ code for a function

#2 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 03-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 07:51 PM

You don't have to declare num1-4 in main, don't pass in
the quotient declare it in the calc funtion.
And the reason your program is doing the wrong thing lies in this line of code:
quotient = calcAverage(45.67, 8.35, 125.78, 99.56 / 4);
Why are you dividing the last parameter by 4?
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 08:08 PM

See the first example:
http://www.cplusplus...rial/functions/
It's not exactly what you're trying to do, but it's very close.
Was This Post Helpful? 1
  • +
  • -

#4 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 08:28 PM

View PostDoNotWant, on 15 November 2011 - 09:51 PM, said:

You don't have to declare num1-4 in main, don't pass in
the quotient declare it in the calc funtion.
And the reason your program is doing the wrong thing lies in this line of code:
quotient = calcAverage(45.67, 8.35, 125.78, 99.56 / 4);
Why are you dividing the last parameter by 4?


I was dividing by 4 to get the average of the 4 numbers. Maybe I'm not suppose to do it this way. Also I'm not sure what you mean by "don't pass in the quotient declare it in the calc funtion". I understand what declare is, but don't follow your instructions. If I comment out where I declare quotient in main, the program gives an error message (undeclared identifier). Also I tried removing the (= 0.0) thinking that maybe it was zeroing out the number passed back to main, but that breaks too.

I've been in this class for about 10 weeks and not only do I study the book, I have watched a lot of video instruction on youtube, but I'm sure that you cannot tell. :)

Here is the revised code with less junk.

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
double calcAverage(double, double, double, double);

int main()
{

	//declare variables
	double quotient = 0.0;

	//display the quotient
	cout << "quotient: " << quotient << endl;

	system("pause");
	return 0;
} //end of main function

//*****function definitions*****
double calcAverage(double num1, double num2, double num3, double num4, double quotient)
{
	quotient = 45.67 * 8.35 * 125.78 * 99.56;
	return quotient;
} //end getCharacter function

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 08:31 PM

Have you looked at the example in the link I posted?
Was This Post Helpful? 0
  • +
  • -

#6 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 08:36 PM

View PostCTphpnwb, on 15 November 2011 - 10:31 PM, said:

Have you looked at the example in the link I posted?


I'm studying it now and reviewing my code trying to figure it out. I'll be back soon as I work through this. I do appreciate this link.
Was This Post Helpful? 0
  • +
  • -

#7 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 08:48 PM

Does this look correct? The program returned a value so I must be on the right track.

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
double calcAverage(double, double, double, double);

int main()
{

	//declare variables
	double quotient = 0.0;
	quotient = calcAverage (45.67,8.35,125.78,99.56);

	//display the quotient
	cout << "quotient: " << quotient << endl;

	system("pause");
	return 0;
} //end of main function

//*****function definitions*****
double calcAverage(double num1, double num2, double num3, double num4)
{
	double quotient = 0.0;
	quotient = num1 * num2 * num3 * num4;
	return quotient;
} //end getCharacter function


Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 08:52 PM

It looks correct as a program, so it's not surprising that it provides an answer. Your problem is the math. To average four numbers you're taking the product of the four, but an average is the sum of the numbers divided by the count.

Oh, and I wouldn't use system("pause"). Use cin.get() instead.
Was This Post Helpful? 1
  • +
  • -

#9 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 09:02 PM

It is true that you must divide by 4 to calculate the average of the four numbers. However, when you call your function, you are passing 4 numbers to calculate an average of: 45.67, 8.35, 125.78, and 99.56

Then, your function adds these numbers together and divides by 4 (because there are 4 numbers):
average equals (a + b + c + d) / 4

Another mistake that you have is that your function prototype has 4 parameters but your function definition has 5
double calcAverage(double, double, double, double);
...
double calcAverage(double num1, double num2, double num3, double num4, double quotient)
{
    quotient = 45.67 * 8.35 * 125.78 * 99.56;
    return quotient;
}



Another mistake.... look at your algorithm to calculate the average: quotient = 45.67 * 8.35 * 125.78 * 99.56;

If you do some research on the internet, is this how you calculate the average of numbers?

Another mistake is that you are hard-coding the values to calculate.

Basically, your function calcAverage() is supposed to take 4 numbers and return the average. So if I use your function by writing the following: double average = calcAverage(10, 20, 30, 40);
average is supposed to equal 25 because (10+20+30+40)/4 = 25

However, your function will calculate the average of 45.67, 8.35, 125.78, and 99.56 no matter what numbers I pass into your function because that is how you wrote your algorithm.

When writing your algorithm you want to use the argument variables when performing the calculation. Doing this makes the calculation completely dependent on the values passed:
int addition(int num1, int num2)
{
  return num1 + num2;
  //NOT return 10 + 5;
}
...


std::cout << addition(10, 5) << std::endl;





EDIT:
To refer to your recent post... you have a slight problem with your math calculation.

This post has been edited by sparkart: 15 November 2011 - 09:03 PM

Was This Post Helpful? 1
  • +
  • -

#10 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 09:08 PM

View PostCTphpnwb, on 15 November 2011 - 10:52 PM, said:

Your problem is the math. To average four numbers you're taking the product of the four, but an average is the sum of the numbers divided by the count.

Oh, and I wouldn't use system("pause"). Use cin.get() instead.


My brain is tired and didn't notice the obvious. It not only works, but it provides the correct answer now.

I changed...

	quotient = calcAverage (45.67,8.35,125.78,99.56)/4;
	quotient = (num1 + num2 + num3 + num4);



I really appreciate the time and help you gave Paul. This was the 2nd of 3 exercises that is due tomorrow night. I will start on the 3rd, but will have to finish it tomorrow evening.

We may cross paths ago sometime. Take care!

Brian
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 09:16 PM

This will give the correct answer, but using the wrong method:
	quotient = calcAverage (45.67,8.35,125.78,99.56)/4;


If you're calling a function called calcAverage wouldn't you expect it to return an average? Yours is returning the sum of the four numbers and then you divide that by 4 to get the average. You should do the division inside the function.
Was This Post Helpful? 1
  • +
  • -

#12 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 09:24 PM

View PostCTphpnwb, on 15 November 2011 - 11:16 PM, said:

This will give the correct answer, but using the wrong method:
	quotient = calcAverage (45.67,8.35,125.78,99.56)/4;


If you're calling a function called calcAverage wouldn't you expect it to return an average? Yours is returning the sum of the four numbers and then you divide that by 4 to get the average. You should do the division inside the function.


Hopefully this will do the job.

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
double calcAverage(double, double, double, double);

int main()
{

	//declare variables
	double quotient = 0.0;
	quotient = calcAverage (45.67,8.35,125.78,99.56);

	//display the quotient
	cout << "quotient: " << quotient << endl;

	cin.get();
	return 0;
} //end of main function

//*****function definitions*****
double calcAverage(double num1, double num2, double num3, double num4)
{
	double quotient = 0.0;
	quotient = (num1 + num2 + num3 + num4)/4;
	return quotient;
} //end getCharacter function


Was This Post Helpful? 0
  • +
  • -

#13 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 09:27 PM

Looks good.
Was This Post Helpful? 0
  • +
  • -

#14 brianborn1968  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 15-November 11

Re: Problem with writing C++ code for a function

Posted 15 November 2011 - 09:30 PM

Thank you sparkart for providing your input. It was all good tips, but I had already fixed half of them by the time I read your reply. That doesn't discount the help you gave and I do appreciate it.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: Problem with writing C++ code for a function

Posted 16 November 2011 - 08:10 AM

Ok, it does look good, but since this might be graded I'm going to pick nits here.

You have:
	//declare variables
	double quotient = 0.0;
	quotient = calcAverage (45.67,8.35,125.78,99.56);


but you're not really just declaring variables. You're also calling a function. And there's no need to initialize quotient before calling the function since the function will return a value which can initialize it.

You could do:
	//declare variable
	double quotient;
	//calculate average
	quotient = calcAverage (45.67,8.35,125.78,99.56);


or
	//declare variable & calculate average
	double quotient = calcAverage (45.67,8.35,125.78,99.56);



I'm also not sure if your instructor wants you to pass variables to the function like so:
int main()
{
	
	//declare variables
	double quotient, N1, N2, N3, N4;
	N1 = 45.67;
	N2 = 8.35;
	N3 = 125.78;
	N4 = 99.56;
	// Calculate average.	
	quotient = calcAverage(N1,N2,N3,N4);
	
	//display the quotient
	cout << "quotient: " << quotient << endl;
	
	cin.get();
	return 0;
} //end of main function


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2