English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Like I said in my subject line, I'm sorry to ask what would look like such a stupid question since I wrote the program myself, but this recursive stuff has me somewhat baffled at how complex it could possibly be, and the book examples are trash since I am not good in math.

Book def of ...

Recursive definition - A definition in which something is defined in terms of a smaller version of itslef.

Recursive algorithm - An algorithm given to a problem by reducing the problem to smaller versions of itself.

Recursive function - A FUNCTION THAT CALLS ITSLEF.

The def of a recursive function is what I am not understanding, why is this not a function to reduce a problem to a smaller version of itself then calling it (saving all the parameters) again.

Regardless is the function I've written in the following program considered a recursive function??? Again I'm sorry to ask, but this is the second time I've written this program, because my first one wasn't considered "recursive"

2006-11-05 17:35:24 · 4 answers · asked by D 4 in Computers & Internet Programming & Design

#include
#include


using namespace std;

bool palindrome(char word[],int listsize, int first);

int main()
{
//Declare local variables
char breakapart[100] = {0};
int listsize = 0;
int first = 0;
char *word;




//Get input from user
cout << "Hello user " << endl;
cout << "Please enter a word for me to check to see if the word is a palindrome or not " << endl;
cin >> breakapart;

//"Find out" the size the word entered
for (int i = 0;i < 100;i++)
{
cout << i << endl;
if (breakapart[i] != 0)
listsize++;
}

//Error check...delete before final product
//Used to find listsize value...had runtime error
//on NOV 4
cout << "Value of listsize " << listsize - 1 << endl;
//End ERROR CHECK***

//Assign memory array addresses to pointers
word = new char[listsize];

//Copy only "valid" data into first array
for (int i = 0; i < listsize; i++)
word[i] = breakapart[i];

//Call function palindrome
palindrome(word,listsize,first);

//Pause sytem after program execution

2006-11-05 17:35:39 · update #1

return 0;
}
bool palindrome(char word[],int listsize, int first)
{
//Error check...delete before final product
cout << "value of first" << first << " " << word[first] << endl;
cout << "Value of last " << listsize << " " << word[listsize - 1] << endl;
//End error check***

//If statement will return true ONLY if word entered is a palindrome
//Else if statment will return ONLY if the word entered is NOT a palindrome
//Else Statement will increment/decrement the counters and recall the function
//until a true or false condition is returned
if (first == listsize -1)
{
cout << "the word entered IS a palindrome " << endl;
return true;
}
else if (word[first] != word[listsize-1])
{
cout << "the word entered is not a palindrome " << endl;
return false;
}
else
{
first++;
listsize--;
palindrome(word,listsize,first);
}
}

2006-11-05 17:36:04 · update #2

Oh by the way, if you haven't figured out yet. This program requires a recursive function, and it is a program to check weather a word is a palindrome or not.

2006-11-05 17:39:41 · update #3

4 answers

As you wrote, a recursive function is a function that calls itself.

So, your palindrome function is recursive in that the palindrome function calls itself - it calls the palidrome function -- within the function.

This is the line that makes it recursive:

else
{
first++;
listsize--;
palindrome(word, listsize, first); <= THAT'S IT
}
}


I add the lines around it so you can tell what i was talking about - but the line that calls the palindrome function is what makes it recursive, since it's called within the palindrome function.

Hope that helps you understand it better.

2006-11-05 19:43:31 · answer #1 · answered by ★ Estelle ★ 6 · 0 0

Yes, it is recursive program, and I think it will work quite good.

2006-11-05 18:27:46 · answer #2 · answered by manoj Ransing 3 · 0 0

it is recursive because it calls itself

2006-11-05 18:44:07 · answer #3 · answered by Anonymous · 0 0

ya it is.

2006-11-05 17:46:44 · answer #4 · answered by Rajesh G 2 · 0 0

fedest.com, questions and answers