function Prototypes First timer

Wondering if I have this setup correctly? Supposed to create a prototype and define it. Also not supposed to use cmath or POW function. I feel like I am close but not getting correct answers. Don't want an answer just a point in the right direction please and thanks

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
  #include <iostream>
#include <iomanip>
using namespace std;

// prototype of calcPower function goes here
void calcPower(int, int);

int main()
{
	int base, exp;

	// test with values entered by the user
	cout << "Enter the base: ";
	cin >> base;
	cout << "Enter the exponent: ";
	cin >> exp;
	calcPower(base, exp);




	// display powers of 2 up to 2^10
	cout << endl << "Powers of 2 from 2^0...2^10: " << endl;
	int i;
	i = 0;
	while (i <= 10)
	{
		calcPower(2, i);
		i++;
	}

	return 0;
}

// definition of calcPower function goes here

void calcPower(int base, int exp)
{
	for (int i = 0; i <= exp; i++)
		base = base * exp;
	cout << "The answer is " << base << endl;
}


Enter the base: 2
Enter the exponent: 2
The answer is 16

Powers of 2 from 2^0...2^10:
The answer is 0
The answer is 2
The answer is 16
The answer is 162
The answer is 2048
The answer is 31250
The answer is 559872
The answer is 11529602
The answer is 268435456
The answer is -1616365790
The answer is -1863462912
In your function calcPower:
- You need a separate variable for the answer, not misuse base. That separate variable should be initialised to 1, since base^0 is 1 for any non-zero base.
- You are multiplying the answer by exp, but you should be multiplying the answer by base each loop.
- You are doing too many loops. Start from i=1.
- I think it would be more natural for your function to return the answer, not print it out.
- exp is not a good name for a variable, since it is that of a standard library routine and very important mathematical function.

Last edited on
I will give that a try.
I believe as far as returning the answer that is the second part of the task I believe professor is showing us just like you said it is better to return the answer instead of print it out

Thanks again lastchance will let you know the outcome!!
Thanks lastchance for push in right direction. Next going to work on returning answer

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 <iomanip>
using namespace std;

// prototype of calcPower function goes here
void calcPower(int, int);


int main()
{
	int base, exponent;

	// test with values entered by the user
	cout << "Enter the base: ";
	cin >> base;
	cout << "Enter the exponent: ";
	cin >> exponent;
	calcPower(base, exponent);




	// display powers of 2 up to 2^10
	cout << endl << "Powers of 2 from 2^0...2^10: " << endl;
	int i;
	i = 0;
	while (i <= 10)
	{
		calcPower(2, i);
		i++;
	}

	return 0;
}

// definition of calcPower function goes here

void calcPower(int base, int exponent)

{
	int expResult = 1;

	for (int i = 1; i <= exponent; i++)
		expResult = base * expResult;

	cout << "The answer is " << expResult << endl;
}



Enter the base: 2
Enter the exponent: 6
The answer is 64

Powers of 2 from 2^0...2^10:
The answer is 1
The answer is 2
The answer is 4
The answer is 8
The answer is 16
The answer is 32
The answer is 64
The answer is 128
The answer is 256
The answer is 512
The answer is 1024
Last edited on
expResult = base * expResult;
can be written
expResult *= base;
and it usually is, because its shorter/easier to read. all the basic operators have this, eg ^=, +=, %=, etc.
Last edited on
For info, doing 'powers of 2' can be done simpler. Multiplying by 2 in decimal is the same as bit shifting left by 1 in binary. So:

1
2
3
4
5
	// display powers of 2 up to 2^10
	cout << endl << "Powers of 2 from 2^0...2^10: " << endl;

	for (int p = 1, i = 0; i <= 10; ++i, p <<= 1)
		cout << p << '\n';

by the way, you are seeing overflow when it goes negative. This is not 'wrong' -- you asked the machine to store/create a value bigger than it can hold in the space you gave it, but the logic can be correct up to that point. (it was not, but you were doing some sort of exponential type growth and hit a limit that did a similar thing).

you can squeeze a little more out of it by using unsigned, and a little more by using 64 bit integer types if your compiler supports it. Even so, you cant store a 2 to the 73rd power in a 64 bit integer; it only stores up to 2 to the 64 -1 in unsigned. Just like a 8 bit stores 255 but 256 (2 to the 8th) won't fit. 2 to the 10 fits into even a short (16 bit) so its fine for your example, but I wanted to explain why it went negative in case you test something once it is correct and see that happen. If you do it on unsigned, you get a random number instead of a negative one.
Last edited on
Thanks jonnin after you said that I remember using that way before and changed it.

I will have to look into that seeplus appreciate the tip.
Topic archived. No new replies allowed.