Looping

Pages: 12
Thank you for the explanation, that is probably why I was having such a hard time understanding it in that it doesn't relate to time at all. Besides, won't the actual "time" vary from computer to computer and processor to processor anyway? So measuring operations IS a logical way to look at it; this whole time I thought the people who came up with Big O were crazy. One of the downsides to online college I guess is that I can't really ask my books questions.
Okay, this loop is no good; why?
 
while (num / static_cast<int>(pow(10, pwr)) >= 10)

I get the following errors:
documents\visual studio 2008\projects\6.1\6.1\main.cpp(31) : error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(575): could be 'long double pow(long double,int)'
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(527): or 'float pow(float,int)'
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(489): or 'double pow(double,int)'
typo
edit; i mean miss-post.
Last edited on
This function is copied straight from one of my books, I am supposed to create a program to implement the function, I don't see any typos but that doesn't mean they aren't there:

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
#include <iostream>
#include <cmath>

using namespace std;

bool isNumPalindrome(int);

int main()
{
	int number;
	cout<<"Enter a number to determine if it is palindrome:  ";
	cin>>number;
	do
	{
		cout<<isNumPalindrome(number)<<endl;
		cout<<"Enter another number or -1 to exit:  ";
		cin>>number;
	}while(number != -1);
	return 0;
}

bool isNumPalindrome(int num)
{
	int pwr = 0;
	if(num < 10)
		return true;
	else
	{
		while (num / static_cast<int>(pow(10, pwr)) >= 10)
			pwr++;
		while(num >= 10)
		{
			int tenTopwr = static_cast<int>(pow(10, pwr));
			if((num / tenTopwr) != (num % 10))
				return false;
			else
			{
				num = num % tenTopwr;
				num = num / 10;
				pwr = pwr - 2;
			}
		}
		return true;
	}
}
Last edited on
There are 3 forms of pow. Each form is an entirely seperate function.

One takes floats
One takes doubles
One takes long doubles.

Which is used depends on the parameters you pass.

You are passing ints. Since there isn't a form of pow that takes ints, the compiler has to cast it to one of the other types. The problem is it doesn't know which type to cast to, and thusly doesn't know which of the 3 functions to call. That's what it means by "ambiguous call".

Using pow for this is like using a sledgehammer to swat a fly, though. I swear most of the programming books I've seen are written by mediocre (at best) programmers.

A better way to approach this problem would be to put pull each digit of the number out and put it in an array, then check the array to make sure it's the same forwards and backwards.
Topic archived. No new replies allowed.
Pages: 12