Amazon.com Widgets Exercise 5.8 - problems with negative numbers
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2013, 04:06:55 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 5
| | | |-+  Exercise 5.8 - problems with negative numbers
Pages: [1]   Go Down
Print
Author Topic: Exercise 5.8 - problems with negative numbers  (Read 712 times)
davej
Newbie
*
Posts: 9






« on: May 11, 2009, 08:56:08 PM »

I wanted to make sure that the program would still tally the individual digits properly even if the user entered a negative integer, but after some tinkering this is the best I could come up with:


Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int number, digit, total;

    NSLog (@"Enter your number.");
scanf ("%i", &number);

total = 0;

while ( number < 0 ) // using the while statement to force the entered integer to positve
{
number *= -1;
}

while ( number != 0 )
{
digit =  number % 10;
total += digit;
number /= 10;
}

NSLog (@"The sum of the digits is %i.", total);

    [pool drain];
    return 0;
}

That finally worked, but it ain't pretty.  That first while statement seemed like a real roundabout way to get there.  Originally my thinking was more along these lines:


Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int number, digit, total;

    NSLog (@"Enter your number.");
scanf ("%i", &number);

total = 0;

number = (unsigned) number +0;

while ( number != 0 )
{
digit =  number % 10;
total += digit;
number /= 10;
}

NSLog (@"The sum of the digits is %i.", total);

    [pool drain];
    return 0;
}

But that didn't work.  The line:

Code: (Objective-C)
	number = (unsigned) number +0;

doesn't seem to do the trick.  What am I missing here?  Is there a simple way to force and integer to be positive?  I feel like it has something to do with converting it to and unsigned int, right? But I can't figure out how to do it.

Thanks.

--Dave--
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: May 11, 2009, 10:09:19 PM »

You can't do it without testing to see if it's less than zero, in which case you could just use the unary minus operator to negate it, as in

Code: (Objective-C)
number = -number;

Wait until you learn about making decisions in the next chapter and then you can fix your program.

Cheers,

Steve Kochan
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.