Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    What's wrong with my functions?


    Lost a ton of points in my test over a couple of recursive functions I wrote, I was hoping you'd be able to tell me what's wrong with them-

    first one receives a number and computes the sum value of its characters:
    int sum (int a){
    if (a/10<1){return a;}
    return (a%10 + sum(a/10));
    }

    Second one receives a number and computes the product of multiplying its characters:
    int product(int a){
    if (a/10<1){return a;}
    return ((a%10)*(product(a/10)));
    }

    Help would be appreciated.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    So why don't you paste the code into a compiler, and do things like
    Code:
    int main ( ) {
        printf("Sum=%d\n", sum(12345) );
    }
    and verify whatever it is you seek.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    So why don't you paste the code into a compiler, and do things like
    Code:
    int main ( ) {
        printf("Sum=%d\n", sum(12345) );
    }
    and verify whatever it is you seek.
    I did, there was indeed a problem- I'm trying to figure out what it is...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    Your functions return incorrect value when called with negative number as a parameter.
    Code:
    sum(-12345) == -12345
    product(-12345) == -12345
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by DRK82
    Your functions return incorrect value when called with negative number as a parameter.
    Code:
    sum(-12345) == -12345
    product(-12345) == -12345
    I'm not certain if a negative value is considered a legal input for the question, but either way I'm more concerned with why the algorithm fails with a positive input.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    Originally Posted by Galorian
    I'm more concerned with why the algorithm fails with a positive input.
    What failure do you refer to? This algorithm works just fine.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    hey mate,

    the sum function works fine on my end, the only thing I can think to add to the product function is to terminate if you encounter a zero, i.e.

    int product(int a){
    if (a/10<1){return a;}

    if(a%10 == 0) { return 0;}

    return ((a%10)*(product(a/10)));
    }
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by gugucachu
    hey mate,

    the sum function works fine on my end, the only thing I can think to add to the product function is to terminate if you encounter a zero, i.e.

    int product(int a){
    if (a/10<1){return a;}

    if(a%10 == 0) { return 0;}

    return ((a%10)*(product(a/10)));
    }
    It would certainly make it a bit more efficient (not that efficiency is much of an issue with an algorithm of this kind that runs on regular integers in a course that does not include efficiency :p ), but the end result would be the same as the algorithm would multiply the result by zero anyway in such a case.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    I can't seem to find anything wrong with my functions (that would matter in the parameters of the test at least), which leaves me with the inescapable conclusion that the professor arbitrarily decided to not accept my answers solely for the fact that they were based on recursion, which was stated as being optional material for the course (which still doesn't make sense since it was taught, if briefly, and they never stated it wasn't allowed for use in the test)...
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Then, may I ask, what was the problem you recieved the deductions for? I'm rattling my brain trying to figure it out.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by gugucachu
    Then, may I ask, what was the problem you recieved the deductions for? I'm rattling my brain trying to figure it out.
    I'll try and translate the questions-
    2) (a) Write a function that returns the sum of the digits that make up the number that was received as an input to the function int sum(int a)

    for example, if a=12 the function would return 3
    if a=123 the function would return 6

    (mid test it was clarified that the function receives a positive input)

    (b) Write a function that returns the multiplication product of the digits that make up the number that was received as an input to the function int product(int a)

    for example if a=12 the function would return 2
    if a=123 the function would return 6

    The professor did not specify what was wrong with my answer- he just wrote a big NO next to the function with no further elaboration...
  22. #12
  23. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    You need to post more information.
    You also need to go and hassle your professor as well - they're the ones being paid to provide you with an education.

    > I did, there was indeed a problem- I'm trying to figure out what it is...
    So
    - post the main() you used to test the functions with
    - post your input data
    - post your expected result, AND your actual result.

    Because at the moment, all we can do is follow those steps, get the apparent correct answer, and conclude the job is done.

    There seems to be some secret information about the assignment that either you're not telling us (say something stupid, like you can't use % operator), or there is something your prof isn't telling you (like it can/should be done on one line of code).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    You need to post more information.
    You also need to go and hassle your professor as well - they're the ones being paid to provide you with an education.

    > I did, there was indeed a problem- I'm trying to figure out what it is...
    So
    - post the main() you used to test the functions with
    - post your input data
    - post your expected result, AND your actual result.

    Because at the moment, all we can do is follow those steps, get the apparent correct answer, and conclude the job is done.

    There seems to be some secret information about the assignment that either you're not telling us (say something stupid, like you can't use % operator), or there is something your prof isn't telling you (like it can/should be done on one line of code).
    I translated the questions word for word including the examples given in the body of the test. There was no additional information aside from the announcement mid test that we can assume a positive input.

    The functions I put up at the top of the thread are exact copies of the answers I wrote in the test (it was a written exam- there was no access to computers besides a pocket calculator). There was no "main" in the body of the question, we were only asked to write the functions themselves, not the program that utilized them (2c required writing a function that used the two functions above, but it still wasn't a "main").

    [EDIT]

    Also, I'm strictly forbidden from contacting my professor or the course assistants if I intend to challenge the test score (there's a specific procedure in place for that), so I can't outright ask them about it...
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Katy, Texas
    Posts
    495
    Rep Power
    199
    Well, you could simplify your logic:

    Code:
     
    ...
    if (a<10) {return a;}
    ...
    Do you agree? Disagree? And remember, it's all about the reputation power...
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by TheOtherDino
    Well, you could simplify your logic:

    Code:
     
    ...
    if (a<10) {return a;}
    ...
    That's true. I considered that afterwards, but so far as I can tell the original statement isn't erroneous strictly speaking.

    I'm trying to find objective faults in the code I wrote during the exam (my ability to refute the score I got may very well depend on the integrity of those functions, and things like streamlining/efficiency/etc are not part of the course curriculum so they shouldn't matter).
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo