Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Katy, Texas
    Posts
    495
    Rep Power
    199
    I think this thread is dead.

    No amount of guessing on our part is going to answer your question.

    Comments on this post

    • dwise1_aol agrees : He should probably go try the Psychic Programmers forum. Maybe [i]they[/i] will be able to read his mind.
    Do you agree? Disagree? And remember, it's all about the reputation power...
  2. #17
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    So write a main() like this, and present the program (and results) as evidence.
    Code:
    int main ( ) {
        int tests[] = { 12345, 1, 112233, 11111, 1001 };
        int i;
        for ( i = 0 ; i < sizeof(tests)/sizeof(tests[0]) ; i++ ) {
            int r1 = sum(tests[i]);
            int r2 = sumByLoop(tests[i]);
            if ( r1 != r2 ) {
                printf("Test failed with %d, r1=%d, r2=%d\n", tests[i], r1, r2 );
            }
        }
    }
    sumByLoop() is another sum the digits function, but it just uses a for loop (no fancy recursion).

    The only other thing you need to do is add more test cases to the tests array. The code automatically figures out how big this array is.

    Or you could simply do this as well
    Code:
    for ( i = 0 ; i < 100000000 ; i++ ) {
            int r1 = sum(i);
            int r2 = sumByLoop(i);
            if ( r1 != r2 ) {
                printf("Test failed with %d, r1=%d, r2=%d\n", i, r1, r2 );
            }
    }
    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. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    The code is certainly inefficient. a/10 is calculated twice per digit, plus a %.
    The ocde will also not work on decimal numbers, as it deals with ints. In addition, although the number has been defined as not negative, a signed int is used as the input.

    Just a few points, but they might have had some effect on the scored result.

    HTH

    Matt
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo