Need help with a homework problem on void functions.

Okay so for starters the problem asks me to consider the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

void func1();
void func2(/*formal parameters*/);

int main()
{
	int num1, num2;
	double num3;

		int choice;

	cout << fixed << showpoint << setprecision(2);

	do
	{
		func1();
		cin >> choice;
		cout << endl;

		if (choice == 1)
		{
			func2(num1, num2, num3);
			cout << num1 << ", " << num2 << ", " << num3 << endl;
		}
	}
	while (choice != 99);

	return 0;

}

void func1()
{
	cout << "To run the program, enter 1." << endl;
	cout << "To exit the program enter 99." << endl;
	cout << "Enter 1 or 99: ";
}

void func2(/*formal parameters*/)
{
       //Write the body of func2.
}


So I am given the above snippet of code to type first. So then this is what the problem tells me to do next for the void function func2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
The function func2 has three parameters of type int, int, and double, say a, b, and c respectively. Write the function func2 so that its action is as follows:

a. Prompt the user to to input two integers and store the numbers in a and b, respectively.

b. If both of the numbers are nonzero:

 i. If a >= b, the value assigned to c is a to the power b, that is a ^ b.
 ii. If a < b, the value assigned to c is b to the power a, that is b ^ a.

c. If a is nonzero and b is zero, the value assigned to c is the square root of the absolute value of a.

d. If b is nonzero and a is zero, the value assigned to c is the square root of the absolute value of b.

e. Otherwise, the value assigned to c is 0.

The values of a, b, and c are passed back to the calling environment.

After completing the definition of the func2 and writing its function prototype, test run your program. 


And so following along as well as I could I typed my code accordingly, function prototype and all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

void func1();
void func2(int a, int  b, double c);

int main()
{
	int num1, num2;
	double num3;

		int choice;

	cout << fixed << showpoint << setprecision(2);

	do
	{
		func1();
		cin >> choice;
		cout << endl;

		if (choice == 1)
		{
			func2(num1, num2, num3);
			cout << num1 << ", " << num2 << ", " << num3 << endl;
		}
	}
	while (choice != 99);

	return 0;

}

void func1()
{
	cout << "To run the program, enter 1." << endl;
	cout << "To exit the program enter 99." << endl;
	cout << "Enter 1 or 99: ";
}

void func2(int a, int  b, double c)
{
	cout << "Enter two integers." << endl;
	cin >> a >> b;

	if ( a != 0 && b != 0)
	{
		if (a >= b)
			c = pow(a, b);
		
		if (a < b)
			c = pow(b, a);
	}
	else if ( a != 0 && b == 0)
		c = sqrt(abs(a));
	else if ( b != 0 && a == 0)
		c = sqrt(abs(b));
	else
		c = 0;

}


However I tried compiling my program and I am getting syntax errors regarding my cmath functions in func2 saying "ambiguous call to overloaded function." Just what do I need to do to fix all this while creating my program exactly as the problem instructs me to do?
What the compiler means is that it can't determine which version of the pow() function you're trying to call.

If you look up the definition of pow() you'll find it has five overloaded versions, non of which take two int parameters, which is what you're passing it.

Likewise, abs() and sqrt() will accept double, float or long double parameters, but not int.

Hope that helps,
Jim
closed account (D80DSL3A)
Those cmath functions have overloads which take float, double or long double arguments - but not integer arguments. The compiler doesn't know which way to cast the integers - as float, double or long double.
Cast a and b yourself, as in:
c = pow((float)a, (float)b); or introduce float variables to substitute for a and b in the functions:
1
2
3
float fa = a, fb = b;
// ...
c = pow(fa,fb);

Once you get this to compile you will discover that line 29 outputs garbage values.
Review pass by value vs. pass by reference.
1
2
3
4
5
6
7
8
9
// pow function does not get to integers
pow(double, double)
pow (double, int)
pow(float, int)

// sqrt function does not get an integere
sqrt(double)
sqrt(float)


With that said the best way I can think is , after you get the input you convert them to double (int a and int b)

do the math and if you have to you can convert them back to integers

This way is exactly the way it was instructed...

Good luck
You can't have the inputs to those functions be ints because it doesn't compile. I would leave a and b as ints and cast to double (because c is a double) where necessary.

I would cast the inputs to the abs function as doubles and I would use this overload of the pow function:
pow(double, int).

EDIT: This was directed at a post by Terminus Est which has been deleted.
Last edited on
Ok as per what fun2code said, this is what I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void func2(int a, int  b, double c)
{
	cout << "Enter two integers." << endl;
	cin >> a >> b;


	if ( a != 0 && b != 0)
	{
		if (a >= b)
			c = pow((double)a, (double)b);
		
		if (a<b)
			c = pow((double)b, (double)a);
	}
	else if ( a != 0 && b == 0)
		c = sqrt(abs((double)a));
	else if ( b != 0 && a == 0)
		c = sqrt(abs((double)b));
	else
		c = 0;

}


It now compiles successfully and without syntax errors, however I am also getting this here while compiling.
1
2
3
1>h:\cosc1436fa11\homework\chap7_ex7\main.cpp(31): warning C4700: uninitialized local variable 'num3' used
1>h:\cosc1436fa11\homework\chap7_ex7\main.cpp(31): warning C4700: uninitialized local variable 'num2' used
1>h:\cosc1436fa11\homework\chap7_ex7\main.cpp(31): warning C4700: uninitialized local variable 'num1' used


Just what do I do now to eliminate this? Because I typed the whole int main() the way the problem in my textbook showed me to. And that's where I have my num1, num2, and num3 variables.
Last edited on
closed account (D80DSL3A)
Initialize the local variables (provide initial values for them)
1
2
int num1=0, num2=0;
double num3=0.0;

Glad you saw that casting as double is a better choice. I wasn't thinking about the type of c.
Initialize the local variables (provide initial values for them)

That's one problem that needs sorting, yes, but by lucky chance, leaving out the initialisation in the original code meant that the compiler error also highlighted that func2() wasn't assigning the values - as fun2code suggested in his original answer.

You need to have func2() implement as pass-by-reference, rather than pass-by-value, which means

void func2(int& a, int& b, double& c);
Ok I initialized those two variables, and now the compilation was 100% successful however I am not getting the output I want. Look at what the console shows here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
To run the program, enter 1.
To exit the program enter 99.
Enter 1 or 99: 1

Enter two integers.
7 8
0, 0, 0.00
To run the program, enter 1.
To exit the program enter 99.
Enter 1 or 99: 1

Enter two integers.
0 9
0, 0, 0.00
To run the program, enter 1.
To exit the program enter 99.
Enter 1 or 99: 1

Enter two integers.
8 0
0, 0, 0.00
To run the program, enter 1.
To exit the program enter 99.
Enter 1 or 99:


So no matter what numbers I enter, I am always getting 0, 0 , 0.00 as output. Something else is wrong here. I do not think I am supposed to have that output.
Oh ok I'll try that Jim
Oh ok Jim finally! You are right I was supposed to have the ampersands! Thanks, now the program works perfectly!
Topic archived. No new replies allowed.