last day (15 days later) » 

12:04 AM
-1
Q: reverse array using recursion in c++

NathanThe assignment is relatively simple reverse the array in main using the ReverseStringRecursive function. However the limitation is I can only use a single int and a char nothing else that declares a variable (this includes the banning of for loops and so on). Additionally no extra libraries may b...

 
are you allowed to use a while loop or not? your description implies no, but your code uses one.
 
@MFisherKDX while loops are allowed since they don't declare a variable. a for loop is not allowed because an additional variable would be needed (i.e. for(int i = 10; i < 10 i++) this is not allowed)
 
are you supposed to simply print the reversed string or are you supposed to reverse it?
 
@MFisherKDX this function just reverses the string then main prints the string
 
I don't see any printing going on here. Your problem is probably elsewhere. Please consider creating a Minimal, Complete, and Verifiable example
 
12:04 AM
@alterigel the function is not suppose to print it that is handled in main sorry I should have included that.
 
@Nathan: for(; reverseMe[position] != '\0'; ++position); is equivalent to your while loop
 
Are you certain about the expected output? How do you know that main doesn't just print the string, then call your code to reverse it, then print it again?
 
@alterigel I'm almost 100% certain it's not a problem in main however I have updated the post to include it if you would like to check. perhaps i'm overlooking something. although my teacher did say that we shouldn't touch the main function.
 
Any class for which an exercise requires <conio.h> is bad and should not be taken. Regardless, without a Minimal, Complete, and Verifiable example it is difficult to diagnose the problem.
 
@GovindParmar we use <conio.h> for _getch(); that way the window doesn't close before we see the output. what's wrong with that?
 
12:04 AM
@Nathan probably because the class doesn't teach you how to use a compiler and relies on libraries to make up for that lack. Also, in recursion, you usually don't use a while or a for loop because the function itself is supposed to be recursive.
 
To start with, you are overwriting your terminating '\0' and therefore corrupting your string. When your while loop exists, reverseMe[position] is at '\0' and then you overwrite it with holder
 
@Sailanarmo the while loop is being used after the recursion happens and the stack collapses the while loop is used to put the letters back into the array backwards.
 
I tried your code after making some minor edits. Mainly, I removed STRING_SIZE since you didn't specify what it is. The code segfaulted after printing (excuse my rude unicode) abcdefghijklmnopqrstuvwxyzzyxwvu� @tsrqp0�G�.onmlkjihgfX.RL�‌​edcba, so either you're exceeding the bounds of an array and causing Undefined Behavior, or STRING_SIZE is some magic value that you haven't told us. Again, please create a Minimal, Complete, and Verifiable example for us to be able to help you.
 
@alterigel sorry I missed the #define when I copied it's included now. basically it's just a constant int that sets the string size.
 
ReverseStringRecursive() is define to return an int, but the return value is discarded both in main() and during the recursion.
 
12:04 AM
What is the signature of the recursive function that you are allowed to use?
 
@Bob__ it's suppose to be a non-tail recursion I believe. i'm not sure what you mean by the signature i'm just learning recursion so i'm not sure about some of the lingo.
 
I mean, is the prototype you have posted mandatory or can you choose what are the parameters and the return types of that function?
 
@Bob__ I'm locked to what is given to me with the function and what is returned unfortunately. So I can't change the function from an int to a char it has to remain an int.
 
I think @bob is getting at whether you're allowed a second (optional) parameter to the function. It's easy to use the prototype as currently written to recurse through the the string, storing each character on the stack and returning the number of characters. If you were able to pass the original string as the second parameter, then it would be trivial to use the stored character and the value returned from the recursive call to reverse your string.
 
@paddy ah I see what your saying, that as well is not allowed the teacher is very strict as to what is allowed on this assignment and he is only allowing us what he has included in main and this single function, in which we can only use a single char, int, and the array included in main.
 
12:04 AM
... so, you're not allowed to create a second function that does the actual recursion, which is purely invoked from your teacher's function?
 
correct I understand how dumb that sounds but he was very clear on what we can and can't do.
 
You said that loops are not allowed, yet you're using a while loop with the bogus argument that it's not a loop because you're not declaring another variable. You know it's still a loop, right?
 
@paddy I said that loops that declare variables are not allowed such as a for loop. I used the for loop example because the most common for loop looks like for(int i = 0; i < x; i++). loops are allowed but they can't declare extra variables
 
It might be time to ask for your money back on this course.
 
i think you are misunderstanding the requirements. either that or the professor hasn't thought this out ...
 
12:04 AM
@MFisherKDX here is the link to the actual assignment directions that he gave us maybe this will help? onedrive.live.com/…
 

last day (15 days later) »