Amazon.com Widgets Answer to Exercise 6.1
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 24, 2014, 11:01:22 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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 6
| | | |-+ Answer to Exercise 6.1
Pages: [1] Go Down
Print
Author Topic: Answer to Exercise 6.1 (Read 2454 times)
DrDavid
Newbie
*
Posts: 26


Email




on: December 08, 2011, 02:37:04 PM

Code: (Objective-C)
//
//  main.m
//  Exercises
//
//  Created by David Wolf on 12/8/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <stdio.h>

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

@autoreleasepool {
int num1, num2;

NSLog(@"Please enter two numbers:");
scanf("%i %i",&num1, &num2);

if ( num1 % num2 )
NSLog(@"Not evenly divisible");
else
NSLog(@"Evenly divisible");

}
    return 0;
}

Logged
estschudi
Newbie
*
Posts: 15


Email




Reply #1 on: December 21, 2011, 09:03:10 AM

Here is my answer.  It looks like the == 0 part in my answer is redundant.  I need to go back and re read so that I can understand the difference.

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

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

    @autoreleasepool {
       
        int value1, value2;
       
        NSLog(@" Please enter two integer values:");
        scanf("%i %i", &value1, &value2);
       
        if (value1 % value2 == 0)
            NSLog(@" %i is evenly divisible by %i", value1, value2);
       
        else
            NSLog(@" %i is not evenly divisible by %i", value1, value2);
        }
       
   
    return 0;
}

 
Logged
estschudi
Newbie
*
Posts: 15


Email




Reply #2 on: December 22, 2011, 08:18:52 AM

Thanks, this has me a little confused still.  I'm discovering that my brain gets it a little better when it's a bit later in the day.  But on page 119 it says:
 "The if statement tests the result of the expression evaluation.  If the result is nonzero, the statement that immediately follows is executed;  otherwise, the statement is skipped."  So I see, in Dr.David's program, the if statement is true when there is a remainder because it is a number >=1.  This idea seems to add an element that sits outside of everything else thus far, if that makes any sense.  Maybe I'm not looking at it right though.  Thanks again, onto exercise 2. 

 
Logged
Soniarod
Newbie
*
Posts: 1






Reply #3 on: January 31, 2012, 07:36:23 AM

Here is my solution using the conditional operator. I don't know what %s means though I just got %i corrected to %s by Xcode:

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
       
        int num1, num2; 
       
        NSLog(@"Please enter two numbers:"); 
        scanf("%i %i",&num1, &num2);
       
     
       
        NSLog (@"%s", ( num1 % num2 ) ? "Not evenly divisible" : "Evenly divisible");
       
    }
    return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: January 31, 2012, 04:25:15 PM

%s means to display what's called a C-style character string (that's a string of characters inside a pair of double quotes).  In Chapter 15 you'll learn how to use %@ to display a string object (A double quoted string of characters preceded by an '@' character).

Cheers,

Steve Kochan
Logged
Sebastiaan76
Newbie
*
Posts: 7


Email




Reply #5 on: February 03, 2012, 07:01:47 AM

Hi,
Here is my contribution. I probably made this a bit more complicated than I had to, but i wanted to use objects/methods.

I also managed this without referring back to the book once!

( i admit, I have read ahead, and come back to this, some the @property/Synthesize directives arent covered until chapter 7 - but i like them!)

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

@interface Divide : NSObject

@property int Firstno, Secondno;
-(void) evenDivFind;

@end

@implementation Divide

@synthesize Firstno, Secondno;
-(void) evenDivFind
{
    if (Firstno % Secondno !=0)
    {
        printf("%i and %i are not evenly divisible \n", Firstno, Secondno);
    }
    else printf("%i and %i are evenly divisible \n", Firstno, Secondno);
   
}

@end

int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
        int u, v;
       
        printf ("please enter 2 numbers:");
        scanf ("%u %u", &u, &v);
       
        Divide *myDiv = [Divide new];
        [myDiv setFirstno: u];
        [myDiv setSecondno: v];
        [myDiv evenDivFind];
    }
    return 0;
}
Logged
honsan
Newbie
*
Posts: 1


Email




Reply #6 on: February 15, 2012, 04:12:51 PM

It seems that no one make a test for the denominator (value 2) is zero, is it redundant for the modulus function? Huh
Logged
elkie13
Newbie
*
Posts: 17






Reply #7 on: February 21, 2012, 01:33:12 PM

I did check for zero and I assumed they wanted the answer anyway.

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

{
    @autoreleasepool {
        int n1, n2;
       
        NSLog(@"Please enter two integer values.");
        scanf("%i %i",&n1,&n2);
       
        if(n2!=0)
        {
            if (n1 % n2 == 0)
                NSLog(@"These numbers are evenly divisable the result is %i",(n1/n2));
            else
                NSLog(@"These numbers are not evenly divisable the result is %g", (n1/n2));
        }
        else
            NSLog(@"Are you crazy?  You can't divide by zero!");
       
                   }
    return 0;
Logged
Chingon
Newbie
*
Posts: 3






Reply #8 on: May 01, 2012, 10:47:53 AM

This was my solution, it includes a check for division by zero:

Code: (Objective-C)
//
//  Exercise 6.1
//  Divisible or not?
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        int a, b;
       
        NSLog(@"Enter two integers, separated by a space:");
        scanf("%i %i", &a, &b);
       
        if ( b == 0 )
               NSLog(@"Division by zero.");
           else if ( a % b )
               NSLog(@"%i is not divisible by %i.", a, b);
           else
               NSLog(@"%i is divisible by %i.", a, b);
    }
    return 0;
}
Logged
Harry_4696
Newbie
*
Posts: 4






Reply #9 on: May 09, 2012, 06:58:47 AM

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

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

    @autoreleasepool {
       
        int first_1, first_2;
       
        NSLog(@"Enter two number to test:" );
        scanf("%i %i", &first_1, &first_2);
       
       
        if (first_2 == 0) {
            NSLog(@"you cant divide by zero");
        }
        else if (first_1 / first_2) {
            NSLog(@"They divide equally");
                } else {
                    NSLog(@"they dont divide equally");
                        }
       
        }
    return 0;
}
Logged
clouded
Full Member
***
Posts: 123






Reply #10 on: May 11, 2012, 06:10:54 AM

Here's mine, the ==0 to me sounds more like what the assignment asked:

Code: (Objective-C)
// Chapter 6 Exercise 1.
// Write a program that asks the user to type in two integer
// values. Test these two numbers to determine whether the
// first is evenly divisible by the second and then display
// an appropriate message at the terminal.

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        long int val1, val2;
        
        printf("Enter two integer values: ");
        scanf(" %li %li", &val1, &val2);
        
        if (val1 % val2 == 0) {
            NSLog(@"The first number is evenly divisible by the second");
        } else {
            NSLog(@"The first number is not evenly divisible by the second");
        }
    }
    return 0;
}

Output:

Enter two integer values: 2 1
The first number is evenly divisible by the second

Enter two integer values: 1 2
The first number is not evenly divisible by the second
Last Edit: May 11, 2012, 08:42:56 AM by happyzhb Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #11 on: July 24, 2012, 09:15:27 AM

here's mine:

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

    @autoreleasepool {
       
        int int1, int2;
       
        NSLog(@"Enter two integer values.");
        scanf("%i %i", &int1, &int2);
       
        if (int1 % int2 == 0)
            NSLog(@"%i is evenly divisible by %i", int1, int2);
        else
            NSLog(@"%i is not evenly divisible by %i", int1, int2);
       
    }
    return 0;
}

output:


2012-07-24 17:13:57.394 prog6[4678:403] Enter two integer values.
100 10
2012-07-24 17:14:02.937 prog6[4678:403] 100 is evenly divisible by 10
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.