Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 7, 2009, 07: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;
	
	
	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!
Robot-Scott is offline   0 Reply With Quote
Old Mar 7, 2009, 07:45 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
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.
lee1210 is offline   0 Reply With Quote
Old Mar 7, 2009, 07:46 PM   #3
mdeh
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by Robot-Scott View Post
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 <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;
	
	
	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/
mdeh is offline   0 Reply With Quote
Old Mar 7, 2009, 07:51 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by mdeh View Post
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
lee1210 is offline   0 Reply With Quote
Old Mar 7, 2009, 07:52 PM   #5
mdeh
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by lee1210 View Post
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!!
mdeh is offline   0 Reply With Quote
Old Mar 8, 2009, 12:09 AM   #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.
jw2002 is offline   0 Reply With Quote
Old Mar 8, 2009, 06:46 AM   #7
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Robot-Scott View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Mar 8, 2009, 10: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
skochan is offline   0 Reply With Quote
Old Mar 9, 2009, 12:43 AM   #9
Robot-Scott
Thread Starter
macrumors newbie
 
Join Date: Jan 2009
thanks

Thanks all, I got it to work with your help!
Robot-Scott is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Kids Garden Math - Learn Math While Harvesting Fruits PyjamasApps iPad Apps 0 Jul 16, 2013 04:56 AM
3rd Grade Math: Summer Review Splash Math App klkatielong iPad Apps 0 Jul 18, 2012 01:56 PM
[Math App] Homework teaches kids math and reports to parents Gaea deBomb iPhone and iPod touch Apps 0 Jul 3, 2012 05:35 AM
A Middle and High School Math App: Cool Math mathfantasia iPad Apps 2 Jun 19, 2012 07:35 PM
5+7+10+5+2+3= ? Math Aloud ! true mental math ... left & right brain. mobile.esd.mx iPhone and iPod touch Apps 0 Jun 1, 2012 05:17 PM

Forum Jump

All times are GMT -5. The time now is 01:36 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC