Amazon.com Widgets Chapter 10 Exercise 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 22, 2014, 08:43:16 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 10
| | | |-+ Chapter 10 Exercise 6
Pages: [1] Go Down
Print
Author Topic: Chapter 10 Exercise 6 (Read 876 times)
clouded
Full Member
***
Posts: 123






on: May 31, 2012, 01:44:02 PM

Output is based on specifiers provided in NSLog:

Code: (Objective-C)
// Chapter 10 Exercise 6
// Based on the following definitions
//     float f = 1.00;
//     short int i = 100;
//     long int l  = 500L;
//     double d = 15.00;
//
// and the seven steps outlined in this chapter for the conversion of operands in
// expressions, determine the type and value of the following expressions:
//     f + i
//     l / d
//     i / l + f
//     l * i
//     f / 2
//     i / (d + f)
//     l / (i * 2.0)
//     l + i / (double) l

//  main.m

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])
{
    @autoreleasepool {
        
        float f = 1.00;
        short int i = 100;
        long int l  = 500L;
        double d = 15.00;
        
        NSLog(@"f + i = %f", f + i);
        NSLog(@"l / d = %g", l / d);
        NSLog(@"i / l + f = %f", i / l + f);
        NSLog(@"l * i = %li", l * i);
        NSLog(@"f / 2 = %f", f / 2);
        NSLog(@"i / (d + f) = %f", i / (d + f));
        NSLog(@"l / (i * 2.0) = %f", l / (i * 2.0));
        NSLog(@"l + i / (double) l = %g", l + i / (double) l);
        
    }
    return 0;
}

Output:

f + i = 101.000000
l / d = 33.3333
i / l + f = 1.000000
l * i = 50000
f / 2 = 0.500000
i / (d + f) = 6.250000
l / (i * 2.0) = 2.500000
l + i / (double) l = 500.2
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #1 on: August 21, 2012, 07:03:44 AM

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        float     f = 1.00;
        short int i = 100;
        long int  l = 500L;
        double    d = 15.00;
       
        NSLog(@"%.2f", f + i);
        NSLog(@"%.2f", l / d);
        NSLog(@"%.2f", i / l + f);
        NSLog(@"%li", l * i);
        NSLog(@"%.2f", f / 2);
        NSLog(@"%.2f", i / (d + f));
        NSLog(@"%.2f", l / (i * 2.0));
        NSLog(@"%.2f", l + i / (double) l);
       
    }
    return 0;
}
2012-08-21 15:03:03.114 FractionTest[817:303] 101.00
2012-08-21 15:03:03.116 FractionTest[817:303] 33.33
2012-08-21 15:03:03.116 FractionTest[817:303] 1.00
2012-08-21 15:03:03.117 FractionTest[817:303] 50000
2012-08-21 15:03:03.118 FractionTest[817:303] 0.50
2012-08-21 15:03:03.119 FractionTest[817:303] 6.25
2012-08-21 15:03:03.119 FractionTest[817:303] 2.50
2012-08-21 15:03:03.120 FractionTest[817:303] 500.20
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.