Amazon.com Widgets Chapter 6 Exercise 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 24, 2014, 02:40:17 PM
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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 6
| | | |-+ Chapter 6 Exercise 5
Pages: [1] Go Down
Print
Author Topic: Chapter 6 Exercise 5 (Read 1409 times)
flatlander
Newbie
*
Posts: 4






on: February 22, 2012, 04:57:02 AM

Has any one done this? I seem to have mental block.
my code is as below, but I've bodged it!

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
       
        int number, right_digit;
       
        NSLog(@"Enter your number?");
        scanf("%i", &number);
       
        if (number < 0) {
            number = abs(number);
            while (number != 0) {
                right_digit = number % 10;
                NSLog(@"%i", right_digit);
                number /= 10;
               
            }
            NSLog(@"-");
        }
        else {
        do {
            right_digit = number % 10;
            NSLog(@"%i", right_digit);
            number /= 10;
           
        } 
        while (number != 0);
        }
    }
    return 0;
}
Logged
jodles
Newbie
*
Posts: 23






Reply #1 on: February 25, 2012, 03:22:23 PM

I did it without using an if-else clause as I didn't want to repeat the code for the calculations, although mine introduces a new variable:

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

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

    @autoreleasepool {
        int number, right_digit;
        BOOL negative = NO;
       
        NSLog(@"Enter your number.");
        scanf("%i", &number);
   
        if (number < 0) {
            negative = YES;
            number = -number;
        }
       
        do {
            right_digit = number % 10;
            NSLog(@"%i", right_digit);
            number /= 10;
        }
        while (number != 0);
       
        if ( negative ) {
            NSLog(@"-");
        }
       
    }
    return 0;
}
Logged
Trizney
Newbie
*
Posts: 8


Email




Reply #2 on: February 28, 2012, 11:57:22 PM

jodies,

I like the way you did this. I had originally had it change the negative right_digit each time. Yours simplifies it. Very nice.

-trey
Logged
Bunchadna
Newbie
*
Posts: 27






Reply #3 on: April 16, 2012, 09:40:43 PM

Just to show another way.
Logged
clouded
Full Member
***
Posts: 123






Reply #4 on: May 11, 2012, 12:47:48 PM

Here's my version, I didn't want to complicate it:

Code: (Objective-C)
// Chapter 6 Exercise 5.
// We developed Program 5.9 to reverse the digits of an integer
// typed in from the terminal. However, this program does not
// function well if you type in a negative number. Find out what
// happens in such a case, and then modify the program so that
// negative numbers are correctly handled. By this, we mean that
// if the number -8645 were typed in, for example, the output of
// the program should be 5468-.

// Program to reverse the digits of a number
#import <Foundation/Foundation.h>

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        int number, right_digit, neg_sign = 0;
        
        NSLog (@"Enter your number.");
        scanf ("%i", &number);
        
        if (number < 0) {
            number *= -1;
            neg_sign = 1;
        }
        do {
            right_digit = number % 10;
            printf("%i", right_digit);
            number /= 10;
        }
        while ( number != 0 );
        if (neg_sign == 1) {
            printf("-");
        }
    }
    return 0;
}
Output:

Enter your number.
-8645
5468-
Last Edit: May 11, 2012, 05:24:06 PM by clouded Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #5 on: July 25, 2012, 10:10:38 AM

here's mine:

Code: (Objective-C)
int main(int argc, const char * argv[])
{
    @autoreleasepool {       
        int number, right_digit, pos_number;
       
        NSLog(@"Enter your number.");
        scanf("%i", &number);
       
        pos_number = number < 0 ? -number : number;
       
        do {
            right_digit = pos_number % 10;
            NSLog(@"%i", right_digit);   
            pos_number /= 10;
        }
        while (pos_number != 0);
       
        if (number < 0)
            NSLog(@"-");
    }   
    return 0;
}

output:

2012-07-25 18:05:32.159 prog5[6141:403] Enter your number.
-12345
2012-07-25 18:05:39.446 prog5[6141:403] 5
2012-07-25 18:05:39.447 prog5[6141:403] 4
2012-07-25 18:05:39.447 prog5[6141:403] 3
2012-07-25 18:05:39.447 prog5[6141:403] 2
2012-07-25 18:05:39.448 prog5[6141:403] 1
2012-07-25 18:05:39.448 prog5[6141:403] -
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.