Page 2 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 40 of 78
  1. #21
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    Quote Originally Posted by Fairy-Killer View Post
    Side note: Raldo, you do realize that when printing a bool, it gives a 1 or 0?
    Yes, though I don't really know why you're asking...?

  2. #22
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    Probably because of
    Proper and simple or not, the instructions specifically say to return a 1 or a 0, not a true or a false.

  3. #23
    Onto plan B...
    Join Date
    Aug 2008
    Posts
    913
    BG Level
    5
    FFXI Server
    Sylph

    That would be why.

  4. #24
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    Which I immediately followed with...
    As far as memory and performance is concerned they are exactly the same thing.
    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.

    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.

  5. #25
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    8,740
    BG Level
    8
    FFXI Server
    Carbuncle

    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

    Code:
    int c (a)
    {
        if (c=1){;
        return c;
        }
        int c = c -1;
    }
    What the fuck, I don't even. C is the function name. You're treating it like a variable inside your function. What's the point of passing a in? What is this I don't even was right.

    I don't know what you're doing here, since your function names are so fucking descriptive and helpful, but I think you were actually trying to do something like...

    Code:
    int fucking_function (int a)
    {
        if (a==1){;
        return a; //basically return 1, base case for recursion, I assume? see below for more on recursion
        }
        return fucking_function(a-1); //this doesn't really make sense, as you're just calling the same function with input a-1 until the input is 1, where you return 1, so it just returns 1 all the way back. Since I don't know what you're trying to do, since your comments and function/variable names are so useless, and because I don't want to do your hw for you/read the hw description, I can't help further than that.
    }
    Code:
    double b(int a)
    {
        if (a % c !=0){
        cout << "The number " << a << "is not prime.";
        }
        else{
        cout << "The number " << a << "is prime!";
        }
    }
    Better, but what is c? You made c a function, are you trying to call it here? You're referencing it as a variable, but there is no c variable in the scope here. If you had typed c(whatever input to c), I think that would work? It should, but I haven't done C stuff in a while, so don't quote me on it.

    Also, you're not returning anything, so this function should be labeled as void function_name (int a), void means there's no return type. Alternatively, you could return an int (or boolean or whatever, it doesn't matter, 1=true 0=false, and have the if and cout statements in your main)


    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
    Is F(5) a base case? No, so by definition:
    F(5) = F(4) + F(3)
    We need to find F(4) and F(3), but we need to find F(4) first since it comes first. Is it a base case? No.
    So by definition:
    F(4) = F(3) + F(2)
    We need to find F(3) and F(2), but we need to find F(3) first since it comes first. Is it a base case? No.
    So by definition:
    F(3) = F(2) + F(1)
    We need to find F(2) and F(1), but we need to find F(2) first since it comes first. Is it a base case? No.
    So by definition:
    F(2) = F(1) + F(0)
    We need to find F(1) and F(0), but we need to find F(2) first since it comes first. Is it a base case? Yes!
    So F(1) will return 1 since it's a base case definition.
    We come back to:
    F(2) = 1 + F(0)
    Is F(0) a base case? Yes! So the function returns the value 0.
    We come back to:
    F(2) = 1 + 0 = 1
    Now the program flow comes back to where F(2) was called,
    F(3) = F(2) + F(1)
    Plug in 1 for F(2)
    F(3) = 1 + F(1)
    Which will call F(1), and since it's a base case, it will return 1, so:
    F(3) = 1 + 1 = 2
    Now the program flow goes back to where F(3) was called:
    F(4) = F(3) + F(2)
    And it will plug in 2 for F(3), but then it will call F(2), which isn't a base case, so it will have to recurse a bit more to break down to the base cases again.
    (Meaning it will basically repeat the part where it breaks down F(2) again. Then when it plugs in F(4) to the F(5) = F(4) + F(3) line, it will have to break down F(3) again.)


    So now that you understand the logic, can you guess what the code for it would be?

    Spoiler: show

    Code:
    int fib (int n){
      if n == 1
        return 1;
      if n == 0
        return 0;
      //if any of the above are true, it will return that value and exit out of the function
      //i could have put an else here, but it's not really necessary, since if it gets this far, it's because neither of the base cases are true.
      return fib(n-1) + fib(n-2);
    }
    That's pretty much how recursion always works:
    1st, check if you're in a base case. If so, return the base case.
    Otherwise, you need to further break down the problem until it reaches the base case.


    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)

  6. #26
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    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?
    But we do have butter. All he has to do is change a few letters, and his margarine will magically turn into butter.

    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.

  7. #27
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    Quote Originally Posted by hey View Post
    But we do have butter. All he has to do is change a few letters, and his margarine will magically turn into butter.

    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.

  8. #28
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Ok, after having a small talk with my teacher, I got the program to actually run.

    Here's the code

    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;
    }
    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.

  9. #29
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    That code is all kinds of wrong
    Code:
    int recrusiveFunc (int testNum)
    {
        //Decrease the number by 1 until 1
        if (testNum != 1)
        {
            recrusiveFunc(testNum -1);
        }
    }
    This doesn't even return a value. :s

    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.

  10. #30
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    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.

  11. #31
    Relic Shield
    Join Date
    Oct 2006
    Posts
    1,695
    BG Level
    6

    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.

  12. #32
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    Quote Originally Posted by Cadsuane View Post
    Secondly, no integer is divisible by integers greater than half of that integer and less than itself, obviously.
    Obviously I didn't knew that, thanks

  13. #33
    Very Sexy Nerd
    Join Date
    Oct 2005
    Posts
    8,740
    BG Level
    8
    FFXI Server
    Carbuncle

    I think my post is pretty useful.

  14. #34
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Quote Originally Posted by Julian View Post
    I think my post is pretty useful.
    I understood your post. I understand what a recursion is, it's just getting the code working is what I'm having trouble with. This is the -only- programming class I have ever taken, so I'm brand new to everything.

  15. #35
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

  16. #36
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    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;
    }
    That works as is, but does not meet the requirements for your assignment. I'll leave that part up to you.

    Look through it, figure out what it does, and rewrite it.

  17. #37
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    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.

  18. #38
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    Quote Originally Posted by Raldo View Post
    For loops are great and all, but his assignment is specifically about recursion...
    I know.

    edit: yeah. His logic was completely wrong before. Now he has it, and can focus on actually writing the program.

  19. #39
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    Plus the code's been already posted >_>

  20. #40
    i'm awesome.
    Join Date
    May 2005
    Posts
    9,218
    BG Level
    8

    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.

Page 2 of 4 FirstFirst 1 2 3 4 LastLast

Similar Threads

  1. BG Homework Help (Grammar)
    By aduidarnenye in forum General Discussion
    Replies: 8
    Last Post: 2011-01-17, 06:36
  2. BG Cooks - Grilling Help Please. :(
    By Klutz in forum General Discussion
    Replies: 82
    Last Post: 2010-02-17, 17:25
  3. BG Legal Advice: HELP ME OUT!
    By Synbios in forum General Discussion
    Replies: 7
    Last Post: 2010-02-15, 04:00
  4. BG advice: Kitty help
    By Apelila in forum General Discussion
    Replies: 5
    Last Post: 2009-11-23, 11:52
  5. Paging Dr. BG, pregnancy scare??? help!!
    By Nitsuki in forum General Discussion
    Replies: 123
    Last Post: 2009-05-29, 17:13
  6. Replies: 29
    Last Post: 2008-06-06, 11:15
  7. BG Audiophiles... lil help?
    By Doombear in forum General Discussion
    Replies: 29
    Last Post: 2008-05-22, 11:28
  8. My homework HELP ME
    By Endo in forum General Discussion
    Replies: 48
    Last Post: 2005-10-09, 10:33