Probably because ofProper and simple or not, the instructions specifically say to return a 1 or a 0, not a true or a false.
That would be why.
Which I immediately followed with...
Booleans are simple, yes, but to someone who hasn't encountered them before, it's best not to introduce new concepts, however simple you may think they are, when what you're teaching doesn't require the use of them.As far as memory and performance is concerned they are exactly the same thing.
If you were teaching someone how to make an apple pie, and the instructions said to use butter but all you had was margarine, would you make a big deal about the fact that butter and margarine are not the same thing, but achieve the same goal, or would you just pretend it's butter and not bring it up?
My day job has me coding in C++ every day. I could tell this guy how to do his homework without creating any functions aside from main(), but I'm trying to make this as easy for him to understand as possible.
No offense, but from everything I've seen you post on this thread so far, I don't think you're taking the right class.
At an attempt to actually help, it sounds like you're having trouble understanding what recursion actually is, or how to write recursive functions, or fuck, how to write functions in general.
Here's a function in silly C:
return-type function-name (input-type input_name, ...)
{
do stuff
the input you pass on to this function is used here, and you reference it based on the input name. That name can only be referenced here, because that's the scope of it. You seem to be having trouble understanding the concept of scope.
return something that's of type return-type
}
If you have any understanding of programing, that really shouldn't be hard to grasp, and you should be able to tell why your code is shit.
Spoiler: show
Now for understanding recursion, let's take a simple and classical example: The Fibonacci number sequence. (http://en.wikipedia.org/wiki/Fibonacci_number)
When you're writing recursion, you're trying to break a problem down to simple base cases. So the first thing you want to do is identify the base case. For the Fibonacci number, the base case is F(0) = 0 and F(1) = 1. (Let's assume F(n) means the nth Fibonacci number.) This is just given based on the sequence. You also know that to find F(n), all you do is add the two previous values together: F(n-1) and F(n-2). (0 1 1 2 3 5 8 13 etc, you're always adding the two previous numbers)
So say, you want to find F(5).
Spoiler: show
So now that you understand the logic, can you guess what the code for it would be?
Spoiler: show
Note: Recursive solution for Fibonacci numbers is a terrible solution. I only used it because it's an easy example to explain recursion basics, and the only one I could really come up with off the top of my head.
Recursion can be a little bit difficult to wrap your head around at first, but it's not bad. If you can't understand that, and you can't understand why your functions make no sense, you should really consider dropping that class, and possibly changing your major if this is a class for your major.
(Hope I didn't fuck anything up in these explanations :< oh god why am I doing this I just wasted half an hour that should have been spent studying for a test fuck fuck fuck)
But we do have butter. All he has to do is change a few letters, and his margarine will magically turn into butter.If you were teaching someone how to make an apple pie, and the instructions said to use butter but all you had was margarine, would you make a big deal about the fact that butter and margarine are not the same thing, but achieve the same goal, or would you just pretend it's butter and not bring it up?
Or hey, why don't we just use a double for it? It makes no difference here, right? Let's just declare everything as a double, so we don't have to think about what is actually appropriate.
Fine, you have both margarine and butter. The recipe asks for butter. You'd rather use margarine, but your student has never seen margarine before. Do you go through the trouble of explaining why you're not following the recipe exactly, or do you just grab the stupid butter?
Sure, use a double if you want. You'd run the risk of rounding errors messing up your compares, but I don't think it would apply in this case.
Clearly you've never had to teach in a setting like this before. It's pretty commonplace to use only the tools the student knows about and don't bring up anything new, unless your entire purpose is to teach about [new thing]. I am not trying to be a teacher here, merely a tutor. I don't want to teach new concepts, merely solidify those that are already known.
I guess you would teach differently; that's just not my style.
Ok, after having a small talk with my teacher, I got the program to actually run.
Here's the code
He literally wanted me to check in main to see whether or not isPrime came out to return 1 or 0, and then make it print out the result. Now, I just have to get the math working, and it will be good for submission. Just as a note, we are covering booleans next week.Code://Anthony Hernandez, 12-25-2012 #include <iostream> using namespace std; int recrusiveFunc (int testNum) { //Decrease the number by 1 until 1 if (testNum != 1) { recrusiveFunc(testNum -1); } } int isPrime (int testNum) { //Test whether the number is prime if (testNum % recrusiveFunc(testNum) == 0) { return 1; } else { return 0; } } int main () { //Get the number from the user int testNum; cout << "Please enter a number: "; cin >> testNum; isPrime (testNum); if (isPrime(testNum)==1) { cout << "The number " << testNum << " is prime."; } else { cout << "The number " << testNum << " is not prime."; } return 0; }
That code is all kinds of wrong
This doesn't even return a value. :sCode:int recrusiveFunc (int testNum) { //Decrease the number by 1 until 1 if (testNum != 1) { recrusiveFunc(testNum -1); } }
Also Tidane gave you the whole answer up there, all you had to do was replace bool for int, true for 1 and false for 0.
What Tajin said.
Also, you don't need to be calling isPrime twice in the main() function, and the contents of isPrime() are just wrong. I could elaborate, but it's hard to tell where assistance ends and doing your work for you begins. Personally, I'd rather just answer questions.
Only unsolicited hint I'll give right now is to say that recursiveFunc() needs two input variables, not one.
Some ideas:
I don't know C++ but it looks like you're checking if the input integer mod (division with remainder) any integer smaller than it equals 0. If yes, isPrime returns false, otherwise true, right?
First off, the number 1 is not prime according the fundamental theorem of arithmetic. The way you're going about it will return false on 1. I know you're not taking abstract algebra, but someone who has will wince and it might end up being worth marks.
Secondly, no integer is divisible by integers greater than half of that integer and less than itself, obviously. It would be good form not to check for divisibility when you know the answer a priori, especially if the number is large. Start from 2 and work upwards instead.
I think my post is pretty useful.![]()
That works as is, but does not meet the requirements for your assignment. I'll leave that part up to you.Code:#include <iostream> bool isPrime(int input) { if (input == 1) { return false; } int devisor; for (devisor=input/2;devisor>1;devisor--) { if (input % devisor == 0) { return false; } } return true; } int main () { int input; std::cout << "Please enter a number: "; std::cin >> input; if (isPrime(input)) { std::cout << input << " is a prime."; } else { std::cout << input << " is not a prime."; } return 0; }
Look through it, figure out what it does, and rewrite it.
For loops are great and all, but his assignment is specifically about recursion...
Nevermind, you're probably just showing him how it's done and he can figure out how to do it with recursion, and now I feel like a dumbass because I don't know how to delete a post.
Plus the code's been already posted >_>
ITT: Assholes who have taken one or two programming classes telling some kid in his first class he's shit, classy.
Give the kid a break, he asked why he was getting a compilation error not your thoughts on whether to define a bool (which is pointless). Students in intro classes are discovering concepts for the first time, stop trying to shove your interpretations down his throat and let him figure shit out himself. It's funny because I can look at someones post in this thread and immediately identify what kind of kid they were in my classes with programming. Some are chill and some are overzealous loudmouths who think their way is the right way (most of this thread), hardly ever a middle ground because the people who think they know it all are usually so damn vocal.
Tidane is a chill bro, for the record.