recursion c++

Discussion in 'Mac Programming' started by chris200x9, Oct 7, 2008.

  1. chris200x9 macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    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.
     
  2. deputy_doofy macrumors 65816

    deputy_doofy

    Joined:
    Sep 11, 2002
    #2
    I didn't comb the code for logic, but I highlighted the error.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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.
     
  4. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #4
    thanx for your help! One question though how the heck did you guess my college???? (wayne state) :confused:
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    CSC2111 isn't a popular course number, it seems. =)

    -Lee
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     

Share This Page