Amazon.com Widgets Exercise 1,2,3,4
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 06:27:31 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 12
| | | |-+ Exercise 1,2,3,4
Pages: [1] Go Down
Print
Author Topic: Exercise 1,2,3,4 (Read 972 times)
alexsom
Jr. Member
**
Posts: 63



Email




on: January 27, 2012, 06:51:48 PM

12.1

I've found the files. Float.h was right under my Fraction crap.h. I noticed first sight they were full of macros both. Should I go ahead and change them all?  Grin Grin Grin.(( i change them all) ?( the world will be a better place) : (keep living like that)) Why are there 5 of float.h and 6 of limits.h...Huh

12.2

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

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

    @autoreleasepool
    
  {
    #define Min(x,y) (((x) < (y)) ? (x) : (y))    
      
       int a, b;
        
        NSLog(@"Type in your first number");
        scanf("%i", &a);
        NSLog(@"Type in your second number");
        scanf("%i", &b);
        NSLog(@"The minimum between the two is %i", Min(a,b));
   }
    return 0;
}

12.3

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
    #define Max(x,y,z) (((x) > (y)) && ((x) > (z)) ? (x): ((y) > (z)) ? (y) : (z))
      
       int a, b, c;
      
      
        NSLog(@"Type in your first number");
        scanf("%i", &a);
        NSLog(@"Type in your second number");
        scanf("%i", &b);
        NSLog(@"Type in your second number");
        scanf("%i", &c);
        NSLog(@"The maximum between the three integers is %i", Max(a,b,c));
  
     }
    return 0;
}

12.4

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
#define IS_UPPER_CASE(x,y,z)  (((x) >= 'A') && ((x) <= 'Z')) ? ('1') : ('0')
      
       char a;
      
      
      NSLog(@"Type in alphabetic character:");
      scanf("%c", &a);
      NSLog(@"Character was %c, therefore turned into %c",a, IS_UPPER_CASE (a,b,c));
  
     }
    return 0;
}


Last Edit: January 27, 2012, 08:32:19 PM by alexsom Logged
ljaygould
Newbie
*
Posts: 16






Reply #1 on: February 28, 2012, 10:15:42 PM

12.1 - I note that you stayed out of hot water by using upper/lower case i.e. Min rather than MIN as prescribed in the exercise. Apparently there IS a macro called MIN that if you try to override it throws a warning...
Logged
that_bman_again
Newbie
*
Posts: 3






Reply #2 on: March 02, 2012, 04:24:04 PM

How about this for 12.3 (get the maximum value out of 3)?

#define MAX3(a,b,c) ( MAX(MAX((a),(b)), (c)) )

Logged
that_bman_again
Newbie
*
Posts: 3






Reply #3 on: March 02, 2012, 04:37:55 PM

In the IS_UPPER_CASE definition it only needs 1 argument (x) as you're only checking for that so:

#define IS_UPPER_CASE(x)  (((x) >= 'A') && ((x) <= 'Z')) ? ('1') : ('0') 

this could be further refined as:

#define IS_UPPER_CASE(x)  ( ((x) >= 'A') && ((x) <= 'Z') )

as the test would return true or false (1 or 0)
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.