### Thread: What's wrong with my functions?

Page 2 of 2 First 12

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

• dwise1_aol agrees : He should probably go try the Psychic Programmers forum. Maybe [i]they[/i] will be able to read his mind.
2. 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 );
}
}```
3. 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