bool isPalindromeIterative(const char *s1){
int len=strlen(s1)-1;
if(len>0)
if(s1[0]==s1[len]){
len-=2;
isPalindromeIterative(s1+1);
}
else
return false;
return true;
}
I am writing about Palindrome.
And when I run it, it appears warning like this:
":79:13: warning: add explicit braces to avoid dangling else
[-Wdangling-else]"
Please help me ! Thanks!
There is formally nothing wrong with the code, unless you wanted the else to match the outer if. A common mistake.
If you add braces everywhere, it will be clear what you intended:
if(len>0)
{
if(s1[0]==s1[len])
{
len-=2;
isPalindromeIterative(s1+1);
}
else
{
return false;
}
}
When you write,
if(len>0)
if(s1[0]==s1[len]){
// This has no effect on the recursive call.
len-=2;
// You missed the return in your post.
return isPalindromeIterative(s1+1);
}
else
return false;
it's most likely that you meant to associate the else with the second if.
if(len>0)
{
if(s1[0]==s1[len])
{
return isPalindromeIterative(s1+1);
}
else
return false;
}
However, the compiler does not use the indent to figure that out. It's possible that, from the compiler writer's point of view, you meant to associate the else with the first if:
if(len>0)
{
if(s1[0]==s1[len])
{
return isPalindromeIterative(s1+1);
}
}
else
{
return false;
}
Since this is common mistake made by developers, the compiler warns you and hopes that you will update the code so that it's less likely to turn into a runtime error.
I want to point out that the recursion logic you are using to detect whether a string is palindrome is wrong.
Let's say your string is "abxba".
In the first iteration, you compare 'a' with 'a'.
In the next iteration, you compare 'b' with 'a', which is incorrect. You end up with the wrong answer.
You have to change your strategy a bit. Use:
bool isPalindromeIterative(const char *s1, int start, int end)
{
if ( start >= end )
{
return true;
}
if ( s1[start] == s1[end] )
{
return isPalindromeIterative(s1, start+1, end-1)
}
return false;
}
The start of the iterative call has to be made as:
isPalindromeIterative(s1, 0, strlen(s1)-1);
It is a style warning against reading the if clauses wrong.
if(len>0) {
if(s1[0]==s1[len]){
len-=2;
isPalindromeIterative(s1+1);
}
} else {
return false;
}
is better to read and less error-prone.
We have similar coding guidelines in our company; for an if, which has bracket clauses, all else branches and all other ifs within the if of the highest order have to have brackets.
Else your example could too easily be misread as e.g.
if(len>0)
if(s1[0]==s1[len]){
len-=2;
isPalindromeIterative(s1+1);
}
else
return false;
Your original post headline mentioned iterative, and 'Iterative' is still part of your function name (even though it is recursive).
You marked this post as c++, but did not use classes.
The other answers addressed your specific questions about your error messages.
For your consideration, and because you have already selected a recursive answer, here is a possible C++ iterative solution and tail-recursive solution using std::string&.
#include <iostream>
#include <string>
class T589_t
{
public:
int exec(int argc, char* argv[])
{
if (argc < 2)
{
std::cerr << "\n please provide one or more string(s) to test"
<< std::endl;
return -1;
}
for (int i = 0; i < argc; ++i)
{
std::string s = argv[i];
{
std::string yesno = (isPalindromeIterative(s) ? " is" : " is not");
std::cout << "\n '" << s << "'" << yesno << " a palindrome (iterative)" << std::flush;
}
std::cout << std::endl;
{
std::string yesno = (isPalindromeRecursive(s) ? " is" : " is not");
std::cout << " '" << s << "'" << yesno << " a palindrome (recursive)" << std::endl;
}
} // for 0..argc
return 0;
}
private: // methods
bool isPalindromeIterative(const std::string& s1)
{ // ^^^^^^^^^
bool retVal = false; // guess s1 is not palindrome
int left = 0; // index of left most char
int right = static_cast<int>(s1.size()) - 1; // index of right most char
do { // iterative loop
if (s1[left] != s1[right]) break; // when not equal, is not palindrome
left += 1; right -= 1; // iterate!
if (left >= right) // all chars tested?
{
retVal = true; // confirm palindrome
break; // exit
}
} while (true);
return retVal;
}
// Notes './dumy589' // odd length 9
// ^-------^ [0] vs [8]
// ^-----^ [1] vs [7]
// ^---^ [2] vs [6]
// ^-^ [3] vs [5]
// ^ [4] == [4] // left >= right, break
// Notes 'abccba' // even length 6
// ^----^ [0] vs [5]
// ^--^ [1] vs [4]
// ^^ [2] vs [3]
// [3] vs [2] // left >= right, break
// Notes 'abcba' // odd length 5
// ^---^ [0] vs [4]
// ^-^ [1] vs [3]
// ^ [2] vs [2] // left >= right, break
// and bonus: tail recursion based on above iterative
// vvvvvvvvv
bool isPalindromeRecursive(const std::string& s1)
{
// index of left most char
int left = 0;
// index of right most char
int right = static_cast<int>(s1.size()) - 1;
return (isPalindromeRecursive(s1, left, right));
}
bool isPalindromeRecursive(const std::string& s1, int left, int right)
{
if (s1[left] != s1[right]) return false;
left += 1; right -= 1;
if ( left >= right ) return true;
return (isPalindromeRecursive(s1, left, right));
}
}; // class T589_t
int main(int argc, char* argv[])
{
T589_t t589;
return t589.exec(argc, argv);
}
On Linux, argv[0] is the executable name.
environment:
Lubuntu 17.10,
g++ (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
With invocation:
./dumy589 aba abccba tx s
This code reports:
'./dumy589' is not a palindrome (iterative)
'./dumy589' is not a palindrome (recursive)
'aba' is a palindrome (iterative)
'aba' is a palindrome (recursive)
'abccba' is a palindrome (iterative)
'abccba' is a palindrome (recursive)
'tx' is not a palindrome (iterative)
'tx' is not a palindrome (recursive)
's' is a palindrome (iterative)
's' is a palindrome (recursive)
Related
I'm doing something very similar to what this user did:
Error: deque iterator not dereferenceable
I've been looking for an age but I just cannot see where I'm going wrong. The solution to the other poster was finding a place where he tried to pop or top from a deque with zero elements. I can't find where I'm doing that in my code.
EDIT: I suspect the issue is within SYAlg or OSProcess, if that helps.
// TestCalculator.cpp : main project file.
#include <string>
#include <iostream>
#include <locale>
#include <ctype.h>
#include <vector>
#include <deque>
using namespace System;
using namespace std;
//using std::string;
bool lastCharDigit = true;
string RawString; //Contains the raw equation the user types in.
deque<string> TokenEquation(1); //Contains the equation in tokenised infix form.
deque<string> RPNEquation; //Contains the equation in tokenised RPN form.
deque<string> OperatorStack; //Used as part of the Shunting Yard Algorithm
deque<string> SolverStack; //Used to solve the RPN Equation.
locale loc; //Used to verify digits.
//START FUNCTION DECLARATION
int main();
void tokeniser(string RawEquation);
void SYAlg();
void OSProcess(string newOperator);
void Solver();
//END FUNCTION DECLARATION
int main()
{
cout << "Please enter a valid infix notation equation, without parenthesis.\n";
cin >> RawString;
tokeniser(RawString);
cout << "\n";
system("pause");
return 0;
}
void tokeniser(string RawEquation)
{
int testCharPos = -1; // Initialise the index of the raw string
int tokenVectorPos = 0; // Initialise the token array position
int tokenVectorPrintPos = 0; // Initialise the print position
for (int eLength = RawEquation.length(); eLength != 0; eLength--) // For each character in the Raw string...
{
testCharPos++; // Increment the char we're testing
char testChar = RawEquation.at(testCharPos); // Establish the current test char
if (isdigit(testChar, loc)) //If the testchar is a digit
{
if (lastCharDigit) //If the last character was a digit
{
TokenEquation[tokenVectorPos] += testChar; //Append the tested char to the current token array pos
}
if (!lastCharDigit) //If the last character was not a digit
{
TokenEquation.push_back(string(1, testChar)); //Establish a new element with the testchar in it.
tokenVectorPos++;
}
lastCharDigit = true;
}
if (!isdigit(testChar, loc))//If the testchar is not a digit
{
TokenEquation.push_back(string(1, testChar)); //Establish a new element with the testchar in it.
tokenVectorPos++;
lastCharDigit = false;
}
}
cout << "The tokens of that equation are:\n\n"; //Outputs the tokens for testing purposes.
for (int tokenLength = TokenEquation.size(); tokenLength != 0; tokenLength--)
{
cout << " " << TokenEquation[tokenVectorPrintPos];
cout << "\n";
tokenVectorPrintPos++;
}
SYAlg(); //Call the SYAlg.
}
void SYAlg() //This function uses Shunting Yard Algorithm to convert the Infix tokens to RPN.
{
cout << TokenEquation.size();
for (int testtokenLength = TokenEquation.size(); testtokenLength != 0; testtokenLength--) //For each token in the tokenised deque
{
if (isdigit(TokenEquation.front().at(0), loc)) //Check if it's a number
{
RPNEquation.push_back(TokenEquation.front()); //Add the first raw token to the RPN Equation
TokenEquation.pop_front(); //Pop the token from the deque
}
if (!isdigit(TokenEquation.front().at(0), loc)) //If it's an operator
{
OSProcess(TokenEquation.front()); //Run the SYAlg operator stack procedure. NB This will pop the front of the TokenEquation for you.
}
}
cout << "The tokens of that equation are:\n\n"; //Outputs the tokens for testing purposes.
int RPNPrintPos = 0;
for (int tokenLength = RPNEquation.size(); tokenLength != 0; tokenLength--)
{
cout << " " << RPNEquation[RPNPrintPos];
cout << "\n";
RPNPrintPos++;
}
}
void OSProcess(string newOperator) //This function processes the Operator Stack
{
bool PushedNewOperator = false;
std::string newOpSTD = newOperator; //Creates an std::string version of the argument for easier comparison.
while (PushedNewOperator == false){ //As long as the new operator is still waiting to go to the stack
if (!OperatorStack.empty()) //If there's already an operator on the stack
{
if (newOpSTD == "/" || "*")
{
std::string OSBackSTD = OperatorStack.back(); //Create an STD version of the back of the OpStack for comparison.
if (OSBackSTD == "+" || "-")
{
OperatorStack.push_back(newOperator); //Add the tested operator to the stack
TokenEquation.pop_front(); //And pop it from the token equation
PushedNewOperator = true; //Set the flag variable to true so we stop looping
}
else
{
RPNEquation.push_back(OperatorStack.back()); //Add the top of the operator stack to the equation
OperatorStack.pop_back(); //Pop this back
}
}
else
{
RPNEquation.push_back(OperatorStack.back()); //Add the top of the operator stack to the equation
OperatorStack.pop_back(); //Pop this back
}
}
if (OperatorStack.empty())
{
OperatorStack.push_back(newOperator); //Add the tested operator to the stack
TokenEquation.pop_front(); //And pop it from the token equation
PushedNewOperator = true; //Set the flag variable to true so we stop looping
}
}
//For each operator on the stack, until the following statement returns false...
//Check if the precedence of newOperator is less than or equal to the top operator.
}
void Solver() //This function solves the RPNEquation
{
//Push each token to the solver stack
//If you push an operator, solve it against the stack
//When the RPN equation is empty and the solver stack only has one token in it, you have a solution
}
One major issue is with the multitude of lines which say if (newOpSTD == "/" || "*"), or something to that effect. These need to be changed to if (newOpSTD.compare("/") == 0 || newOpSTD.compare("*") == 0).
I think these checks failing means the while loop they're in turns into while(true).
I was assigned to create an array check (to see if the array is increasing, decreasing, or neither [then exiting if neither]) and a recursive binary search for one of my assignments. I was able to do these things after some help from my peers, but I need help in finding what seems to be causing the error
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted
when running the code. I Googled this error and this error seems to be vague or I just am not understanding. It compiles without errors, but I need help in what finding what I did wrong. It is able to run without the binarySearchR function and its associating code, as the array check on its own was the previous assignment. Below is the code, and I thank you so much in advance!
#include <iosteam>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int checkArraySort (string *fileLines, int numberOfLines);
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax);
int main ()
{
int numberOfLines = 0;
string searchKey = 0;
cout << "Input search key: ";
cin >> searchKey;
ifstream fileIn;
fileIn.open("words_in.txt");
string line;
if (fileIn.eof()) /* Checks file to see if it is blank before proceeding */
{
exit (EXIT_SUCCESS);
}
else
{
while(!(fileIn.eof()))
{
fileIn >> line;
numberOfLines++;
}
fileIn.close(); /* closes fileIn, need to reopen to reset the line location */
fileIn.open("words_in.txt");
string *fileInLines;
fileInLines = new string[numberOfLines];
for (int i = 0; i < numberOfLines; i++)
{
fileIn >> line;
fileInLines[i] = line;
}
fileIn.close(); /* closes fileIn */
int resultingCheck = checkArraySort(fileInLines, numberOfLines);
if (resultingCheck == -1)
{
cout << "The array is sorted in descending order." << endl;
}
else if (resultingCheck == 1)
{
cout << "The array is sorted in ascending order." << endl;
}
else
{
cerr << "ERROR: Array not sorted!" << endl;
exit (EXIT_FAILURE);
}
int searchResult = binarySearchR (fileInLines, searchKey, 0, numberOfLines);
if (!searchResult == -1)
{
cout << "Key found at index " << searchResult << "." << endl;
}
else
{
cout << "Key not found at any index." << endl;
}
exit (EXIT_SUCCESS);
}
}
int checkArraySort (string *fileLines, int numberOfLines)
{
int result = 1; /* Ascending by default */
for (int i = 1; i < numberOfLines; i++) /* Checks if decending */
{
if (fileLines[i] < fileLines[i-1])
{
result = -1;
}
}
if (result == -1) /* Makes sure it is descending (or if it is neither) */
{
for (int i = 1; i < numberOfLines; i++)
{
if (fileLines[i] > fileLines[i-1])
{
result = 0;
}
}
}
return result;
}
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax)
{
// so, its gotta look at the center value and each times, it discards half of the remaining list.
if (iMax < iMin) /* If the minimum is greater than the maximum */
{
return -1;
}
else
{
int iMid = (iMin + iMax) / 2;
if (fileLines[iMid] > searchKey) /* If the key is in the lower subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid - 1);
}
else if (fileLines[iMid] < searchKey) /*If the key is in the upper subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid + 1);
}
else /*If anything else besides the two */
{
return iMid;
}
}
}
The easy way: add a bunch of cout s to see where you program goes and what the values are.
Pros
Easy to do
Cons
Requires a recompile each time you want to add more info
The hard way: Learn to use a debugger
Pros
Can inspect "on the fly"
Don't need to rebuild
Can use what you learn in every other C++ program
Cons
Requires a bit of research to learn how to do it.
Can someone please explain with a simple example what precisely is the difference between calling and returning a recursive function in c++?
Here is my code which seeks to find a character in a string recursively. It works fine when I just call find(); the function displays an integer value, But, when I code return find(letter,word), it gives the correct result as either a 1 or 0. Thanks
bool find(char f,string str)
{
static int len = str.length() - 1;
static int count = 1;
if (len<0)
{
return false;
}
else
{
if (str[len] == f)
{
return true;
}
else
{
len--;
return find(f, str);
}
}
}
You don't return the function you return the result of the call.
Here a basic example of recursion :
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
If we call the function with n == 2 the program will do:
1) factorial (2) => return 2 * factorial(1); // It calls factorial with n == 1, do the multiplication and then return the result.
2) factorial(1) => return 1 * factorial(0); // same here with n == 0
3) factorial(0) => return 1; // from here the program will come back from the calls with the results
4) factorial(1) => return 1 * 1; => return 1;
5) factorial(2) => return 2 * 1; => return 2;
Few other things : be aware that your code will only work once as you use static int len, and count is a useless variable.
The function signature indicates a return value, so you need to embed the function call in a return statement for a value to be returned by the function, especially when neither of the if-conditionals are true.
Consider the following code (a variant of what the OP provided):
#include <iostream>
using namespace std;
int find(char f,string str) {
static int len = str.length() - 1;
static int count = 1;
int temp = 0;
if (len < 0) {
cout << count << "\n";
return -99;
}
else
if ( str[len] == f) {
return len;
}
len--;
count++;
temp = find( f, str );
cout << temp << "\n";
return temp;
}
int main() {
char ch = 'z';
int res = find(ch,"I");
if (res < 0) {
cout << "Letter '" << ch << "' was not found";
}
return 0;
}
See demo
Note that find() has a return value of -99 when the letter is not found. That value is captured by temp in find() and the function then returns the value of temp.
Now, consider main() -- its signature also indicates a return value, although it is discarded. If you attempt to execute the code without returning some kind of an integer in main(), the execution will be flawed.
So, whether you use a function recursively or not, if the function's signature indicates a return value then you need to return a value of the expected type. Since find() may return -99 or the position of the found letter, when it executes it will evaluate as one of those two values which will be returned by means of the return statement.
I have a recursive function to find the starting index of a substring within a string. I am learning to use recursion, so the find function is not allowed. I believe I have met most of the conditions. This function is supposed to find the correct index in the string. If it is blank it returns -1.
Here is the real problem. If I enter a string "nothing" and search for "jax" it doesn't return -1. I don't understand why. Any help please? Here is the code:
The user would enter string s and t passed into below:
int index_of(string s, string t)
{
int start = 0;
int len2 = t.length();
int index = 0;
if (s == "")
{
return -1;
}
else if (s.substr(1).length() <= t.length())
{
return -1;
}
else if ( s.substr(start, len2) == t)
{
return index;
}
else
{
index ++;
return index + index_of(s.substr(1), t);
}
return -1;
}
There are several problems -- some minor ones, and some quite important ones.
You have two variables, start and index, to indicate "the current position", but one would be enough.
index can only be 0 or 1. Therefore, the way it is currently written, you could easily get rid of index and start altogether.
Important: When, during the final recursion, the end of the string is reached, you return -1 to the previous recursive call. Then, because of the way the recursive calls are done, you add 1 and return that to the previous call, and so forth. The value finally returned is the -1 plus the length of the string. That is why you get strange results.
This comparison
if (s.substr(1).length() <= t.length())
does not make much sense.
Taking all of this into account, here is an improved version:
#include <iostream>
#include <string>
int index_of(
const std::string &s,
const std::string &t,
const size_t index)
{
int len2 = t.length();
if ((s.length() - index) < t.length())
return -1;
else if (s.substr(index,len2) == t)
return index;
else
return index_of(s,t,index + 1);
return -1;
}
/** Overloading, so you can call index_of with just
two arguments */
int index_of(const std::string &s, const std::string &t)
{
return index_of(s,t,0);
}
/** Some test cases. */
int main()
{
std::cout << index_of("hello","ello") << std::endl;
std::cout << index_of("nothing","jax") << std::endl;
std::cout << index_of("hello","llo") << std::endl;
std::cout << index_of("hello","lo") << std::endl;
std::cout << index_of("hello","o") << std::endl;
std::cout << index_of("hello","hel") << std::endl;
}
The best way to learn how to debug problems like this is to work them out on paper. Your example is small enough that it shouldn't take too long. It's pretty clear that you're going to fall into your else case in the first few steps because the strings don't match. So we have:
index_of("nothing", "jax"):
index++; // index is now 1
return 1 + index_of("othing", "jax");
index_of("othing", "jax"):
index++; // index is now 1
return 1 + index_of("thing", "jax");
etc.
Does that help?
Problem with returning booleans in c++..
bool find( const TrieNode &node, const string word )
{
if (word.length() == 0)
{
if (node.isWord)
{
cout << "TRUE" << endl;
return true;
}
else
{
cout << "FALSE" << endl;
return false;
}
}
char firstletter = word.at(0);
int index = firstletter - 'a';
if (node.letters[index] == NULL)
{
return false;
}
else
{
find (*node.letters[index],word.substr(1,(word.length() - 1)));
}
}
in my main I have
cout << find(*mynode,"word") << endl;
would yield to :
FALSE
95
clearly, a cout of FALSE means that the function returns false.. However, when I print out the result of the function, I get 95 which evaluates to true.. Any reason why it could be doing this?
thanks
Your missing a final return statement, so your getting whatever is in the low byte of EAX, which is random garbage. your probably want return true; at the very end of your function.
Your should pump the warning level of your compiler as it should be telling you this (something along the lines of "not all control paths return a value").
The problem is with your final if statement:
if (node.letters[index] == NULL) {
return false;
}
else {
//if execution gets here, the return value of the function is undefined
find (*node.letters[index],word.substr(1,(word.length() - 1)));
}
...perhaps try:
if (node.letters[index] == NULL) {
return false;
}
else {
return find (*node.letters[index],word.substr(1,(word.length() - 1)));
}