Help please, i am new to C++ and am working on Function and Parameter Declarations right now. Below is the task and then what i have so far...thank you for any help you can offer.
4a. Write a function called mult() that accepts two double-precision numbers as parameters, multiplies these two numbers, and displays the result.
4b. Include the function written in Exercise 4a in a working program. Make sure your function is called from main(). Test the function by passing various data into it.
#include <iostream>
using namespace std;
int mult (int a, int b)
{
int r;
r=a*b;
return (r);
}
int main ()
{
int z;
z = mult (6,2);
cout << "The result is " << z;
system("pause");
return 0;
}
I am confused on the part that says "that accepts two double-precision numbers as parameters"
It say the function should "displays the result" and they don't mention a return value so maybe they mean you should print r inside mult instead of returning it.
It is not correct. View my response above and view Breadman's response. He states that you are returning an int value(not correct) and taking 2 int parameters (not correct). Double precision means the usage of the keyword double.
i apoligize for not understanding very well, this is all a bit overhelming to me since it is all 100% new to me. After watching a tutorial i have re did my program..this is what i came up with...let me know what you think, thank you guys!
#include <iostream>
using namespace std;
void mult(int);
int num1, num2;
int main()
{
cout << "Enter a number:" <<endl;
cin >> num1;
cout << "Enter a second number:" <<endl;
cin >> num2;
mult (num1*num2);
system ("pause");
return 0;
}
void mult (int x)
{
using namespace std;
cout <<"The result is " << x <<endl;
}
if i read the directions correctly the second time around, i believe they want the user to enter two numbers and not for me to choose them beforehand.?
mult now takes one argument. It should take two. So instead of multiplying the numbers before passing them in you multiply them inside mult (like you did earlier). When we say you should use a double we mean that you should use double instead of int.
Can someone write the code correctly, so i can visually see what i am doing wrong..i am getting confused as to where i should be plugging stuff in correctly and changing things when reading the replies. ugh:/
Sorry sledger87 I feel that we have given you more than enough help for you to figure this out on your own.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int mult (int a, int b)
{
double r;
r=a*b;
return (r);
}
int main ()
{
double z;
z = mult (6,2);
cout << "The result is " << z;
system("pause");
return 0;
}
Your original code was correct. All you have to do is change the arguments to double and make sure you are printing the answer in the method like you have done in your 2nd version.
#include <iostream>
using namespace std;
double mult (double num1, double num2)
{
cout << "Enter a number: ";
cin >> num1;
cout << "Enter a second number: ";
cin >> num2;
double r;
r=num1*num2;
return (r);
}
int main ()
{
double z;
z = mult(0,0);
cout << "The result is " << z <<endl;
system("pause");
return 0;
}
The program runs and works. But I dont quite understand
z = mult(0,0);
what is the purpose of this?, i noticed it did not matter which numbers i inputed in place of the "0,0" the program still ran correctly based on what two numbers the user inputs at the start of the program. thanks..again.