Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
I am doing simple recursion, I have written the following program:
Code:
 // csc2111lab5part2.cpp : Defines the entry point for the console application. 
#include <string>
#include <iostream>
using namespace std;
int counter = 0;
bool palindrome(string pal) 
{
	int length;
	length = pal.length();
	if ( pal[length-counter] == pal[counter])
	{
		counter++;
		palindrome();
	}
	
	if ( counter == length)
	{
		return true;
	}
	
	return false;
	
	
	
}


	

int main()
{
	string pal;
	cout << "input what you think is a palindrome\n";
	cin >> pal;
	
	cout << palindrome(pal);
	return 0;
}


when defining my palindrome function it says too few arguments, also in my first if statement whe I call the function again it gives me a very nondescript error that just says error at this point of the file. Any help would be much appreciated.
 

deputy_doofy

macrumors 65816
Sep 11, 2002
1,466
410
I am doing simple recursion, I have written the following program:
Code:
 // csc2111lab5part2.cpp : Defines the entry point for the console application. 
#include <string>
#include <iostream>
using namespace std;
int counter = 0;
bool palindrome([b]string pal[/b]) 
{
	int length;
	length = pal.length();
	if ( pal[length-counter] == pal[counter])
	{
		counter++;
		palindrome(); [b]// WHERE'S YOUR PARAMETER?[/B]
	}
	
	if ( counter == length)
	{
		return true;
	}
	
	return false;
	
	
	
}


	

int main()
{
	string pal;
	cout << "input what you think is a palindrome\n";
	cin >> pal;
	
	cout << palindrome(pal);
	return 0;
}


when defining my palindrome function it says too few arguments, also in my first if statement whe I call the function again it gives me a very nondescript error that just says error at this point of the file. Any help would be much appreciated.

I didn't comb the code for logic, but I highlighted the error.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Is this for a tennessee tech course or a wayne state course? While it was noted in your code, you should still say explicitly that this is homework, so people don't solve the problem for you, or write you code. deputy_doofy already pointed out the problem, so you just need to decide, logically, what you will determine with each call to the function. Also, since you're just learning, I'll implore you to find a better solution to a global variable, even if it's a static local variable.

I'm not sure if the algorithm you are using was mandated by your professor, but you may want to rethink it. Why do you need to keep track of the number of times your function has been called at all? I'm not going to give it away, but I don't feel that that's at all necessary. This isn't a loop, think about it differently.

-Lee

EDIT: Also,:
Code:
int main(int argc, char *argv[]) {
is probably a better way to go for your main signature.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Since we're talking about recursion, and this was homework and I couldn't post the C++ version and ruin it for the OP, I thought the Haskell version might be interesting.

Code:
isPalindrome :: String -> Bool
isPalindrome [] = True
isPalindrome (x:[]) = True
isPalindrome (x:xs) = if x==last xs 
                        then isPalindrome (init xs)
                        else False

This should be properly tail recursive (the original version used && in the recursive case, and that messed it up), so it should run in constant space. That's nicer than the C/C++ version, and the speed should be about the same as C/C++ with some sort of switch construct.

Edit: On a related note, be sure to take CSC 5800 after CSC 2200. It was the only course I found that mentioned functional programming at all, and I think it really helps you think about problems differently. This course is not required by either the BA or BS, but it seems like it probably should be. Perhaps another course covers functional programming, and it's simply omitted from the catalog description. If so, take at least one course that covers it.

Edit 2: Not that anyone is likely to be reading this, but the example above was modified, it had a case that was redundant.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.