MacRumors Forums I'm stuck on integer math, exercise 5-8 from Kochan book

Mar 7, 2009, 06:22 PM   #1
Robot-Scott
macrumors newbie

Join Date: Jan 2009
I'm stuck on integer math, exercise 5-8 from Kochan book

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:
Quote:
 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:
Code:
```#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!
0
 Mar 7, 2009, 06:45 PM #2 lee1210 macrumors 68040     Join Date: Jan 2005 Location: Dallas, TX 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. 0
Mar 7, 2009, 06:46 PM   #3
mdeh
macrumors 6502

Join Date: Jan 2009
Quote:
 Originally Posted by Robot-Scott 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: Code: ```#import // 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; NSLog(@"Enter your number:"); 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/
0
Mar 7, 2009, 06:51 PM   #4
lee1210
macrumors 68040

Join Date: Jan 2005
Location: Dallas, TX
Quote:
 Originally Posted by mdeh 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
0
Mar 7, 2009, 06:52 PM   #5
mdeh
macrumors 6502

Join Date: Jan 2009
Quote:
 Originally Posted by lee1210 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!!
0
 Mar 7, 2009, 11:09 PM #6 jw2002 macrumors 6502   Join Date: Feb 2008 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. 0
Mar 8, 2009, 05:46 AM   #7
gnasher729
macrumors G5

Join Date: Nov 2005
Quote:
 Originally Posted by Robot-Scott Code: ``` 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.
0
 Mar 8, 2009, 09:41 AM #8 skochan macrumors regular   Join Date: Apr 2006 Location: California Code: `sum, next_digit, number = 0;` will only set number to 0, whereas this expression Code: `sum = next_digit = number = 0;` will set all three variables to 0. Cheers, Steve Kochan 0
 Mar 8, 2009, 11:43 PM #9 Robot-Scott Thread Starter macrumors newbie   Join Date: Jan 2009 thanks Thanks all, I got it to work with your help! 0

 MacRumors Forums