PDA

View Full Version : I'm stuck on integer math, exercise 5-8 from Kochan book

Robot-Scott
Mar 7, 2009, 06:22 PM
I'm working out of "Programming in Objective-C 2.0" by Kochan. I'm in the chapter about loops and I'm on the last exercise. The problem is this:
Write a program that calculates the sum of the digits of an integer. For example, the sum of the digits of the number 2155 is 2 + 1 + 5 + 5, or 13. The program should accept any arbitrary integer the user types.
*Note: If you want to try running this program in Xcode, make sure you create a new project as Command Line Utility > Foundation Tool.

This is the code I have so far:
#import <Foundation/Foundation.h>

// Program to add the digits of a number

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, next_digit, sum;
sum, next_digit, number = 0;

scanf ("%i", &number);
NSLog(@"The sum of the digits is:");

for ( ; number != 0; number /= 10 )
{
next_digit = number % 10;
sum += next_digit;
}

NSLog(@"%i", sum);

[pool drain];
return 0;
}

Everytime I execute the program I get the same answer of -1881141184.

Instead of staring at it for hours I thought I would ask for help. Thanks!

lee1210
Mar 7, 2009, 06:45 PM
What is sum set to when you start your program? I don't think the comma operator does what you think it does.

-Lee

Edit: The expression x = 0 evaluates to 0. That might help.

mdeh
Mar 7, 2009, 06:46 PM
I'm working out of "Programming in Objective-C 2.0" by Kochan. I'm in the chapter about loops and I'm on the last exercise. The problem is this:

*Note: If you want to try running this program in Xcode, make sure you create a new project as Command Line Utility > Foundation Tool.

This is the code I have so far:
#import <Foundation/Foundation.h>

// Program to add the digits of a number

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, next_digit, sum = 0, next_digit, number = 0;

scanf ("%i", &number);
NSLog(@"The sum of the digits is:");

for ( ; number != 0; number /= 10 )
{
next_digit = number % 10;
sum += next_digit;
}

NSLog(@"%i", sum);

[pool drain];
return 0;
}

Everytime I execute the program I get the same answer of -1881141184.

Instead of staring at it for hours I thought I would ask for help. Thanks!

You need to initialize sum, as I did for you.

Also, Kochan has a very nice web-site here

http://www.classroomm.com/objective-c/

lee1210
Mar 7, 2009, 06:51 PM
You need to initialize sum, as I did for you.

Also, Kochan has a very nice web-site here

http://www.classroomm.com/objective-c/

Don't just give it away! =)
You're a few months ahead of him on this book, so a lot of this stuff is obvious now. It's certainly helpful to point out the exact error... the person asking the question gets their program working and can proceed, but they may not have learned as much as they might if they figured it out on their own or with a little nudging.

-Lee

mdeh
Mar 7, 2009, 06:52 PM
Don't just give it away! =)
You're a few months ahead of him on this book, so a lot of this stuff is obvious now. It's certainly helpful to point out the exact error... the person asking the question gets their program working and can proceed, but they may not have learned as much as they might if they figured it out on their own or with a little nudging.

-Lee

Good point lee....Just trying to be as nice to him as you have been to me!! :)

jw2002
Mar 7, 2009, 11:09 PM
Why doesn't the compiler warn that the value is used before it is initialized? This is a pretty cut and dry case to detect cleanly at compile time. It's not like the case of say a function being passed in an uninitialized value can only be detected at run time.

gnasher729
Mar 8, 2009, 05:46 AM
int number, next_digit, sum;
sum, next_digit, number = 0;

Instead of blindly copying correct solutions that have been given here, can you try to look at the second line and explain to us exactly what it does and why? If you manage to do that, it will help you an awful lot understanding C, C++ and Objective-C programming. I mean _understanding_, which is very different from throwing code together until it works, kind of.

skochan
Mar 8, 2009, 09:41 AM
sum, next_digit, number = 0;

will only set number to 0, whereas this expression

sum = next_digit = number = 0;

will set all three variables to 0.

Cheers,

Steve Kochan

Robot-Scott
Mar 8, 2009, 11:43 PM
Thanks all, I got it to work with your help! :)