Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 09:39:37 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Excercise 6.6  (Read 2134 times)
Cor
Newbie
*
Posts: 3


Email
« on: June 09, 2016, 12:31:00 AM »

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int number, right_digit, n, originalnumber, left_digit;
        
        n = 1;
        NSLog(@"Enter your number.");
        scanf ("%i", &number);
        
        originalnumber = number;
        
        // calculate number of digits multiplied by 10
        do
        {right_digit = number%10;
            number/=10;
            n = n*10 ;}
        while (number !=0);
        
        while (n > 1 )
        {n = n/10; // power of ten needed to find the most left digit
            
            left_digit = originalnumber/n;
            
            switch (left_digit)
            {
                case 9:
                    NSLog (@"nine");
                    break;
                    
                case 8:
                    NSLog (@"eight");
                    break;
                    
                case 7:
                    NSLog (@"seven");
                    break;
                    
                case 6:
                    NSLog (@"six");
                    break;
                    
                case 5:
                    NSLog (@"five");
                    break;
                    
                case 4:
                    NSLog (@"four");
                    break;
                    
                case 3:
                    NSLog (@"three");
                    break;
                    
                case 2:
                    NSLog (@"two");
                    break;
                    
                case 1:
                    NSLog (@"one");
                    break;
                    
                default :
                    NSLog (@"zero");
                    break;
                    
            }
            
            // continue with remaining part of the number
            originalnumber = originalnumber%n;
            
        }
        
    }
    return 0;
}
« Last Edit: June 09, 2016, 01:03:29 AM by Cor » Logged
djt
Newbie
*
Posts: 7



Email
« Reply #1 on: June 09, 2016, 02:36:52 PM »

I tried about a dozen numbers with your program and they all came up with the correct responses.

Just a comment or two:

You might want to add a space after each digit name: e.g. "nine " instead of "nine"

The variable right_digit is "set but never used" according to my compiler. It can be deleted. You should turn on all error messages and warnings from your compiler.

The variable left_digit is used only briefly and can also be deleted if you replace

Code: (Objective-C)
left_digit = originalnumber/n;  
              
    switch (left_digit)

with

    
Code: (Objective-C)
switch (originalnumber / n)
 

Case zero should have its own case -- use default to catch an unexpected / error case.

Nice program overall.

« Last Edit: June 09, 2016, 02:41:53 PM by djt » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.