Amazon.com Widgets Recent Posts
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 04, 2015, 10:53:33 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
|-+  Recent Posts
Pages: 1 2 [3] 4 5 ... 10

 21 
 on: July 20, 2015, 01:11:13 PM 
Started by arjenhouter - Last post by pif
Here's my solution to this. However, I would like to have the program go back to asking for two numbers to be entered if it encounters zero. As it is, it just ends.

Can anyone help me solve this?

Code: (Objective-C)
// get two numbers, check if the 1st one is evenly divisble by the 2nd one and display the message

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
    @autoreleasepool {
        int a, b;
        BOOL statement;
        
        NSLog(@"\nPlease enter two numbers:");
        scanf (" %i %i",&a, &b);
        switch (b)
        {
            case 0:
                NSLog(@"\nDivision by zero error");
                return NAN;
                break;
            default:
                if (a%b==0)
                    statement=YES;
                else
                    statement=NO;
                break;
        }
        if (statement==YES)
            NSLog(@"\n%i is divisible by %i!", a, b);
        else
            NSLog(@"\n%i is NOT divisible by %i!", a, b);
    }
    return 0;
}

 22 
 on: July 16, 2015, 03:31:52 PM 
Started by loop - Last post by pif
And a definitely less elegant solution to 5.8 exercise, although it is a v1.0:

Code: (Objective-C)
// sum of all integers

#import <FOundation/Foundation.h>

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

        long int number, remainder, transfer, original_number;
       
        remainder = 0;
       
        transfer = 0;
       
        NSLog(@"\nThis program calculates the sum of all integers within a number. \nEnter a number, please:");
        scanf ("%li", &number);
       
        original_number = number;
       
        do {
           
            transfer = number%10;
            number = number / 10;
            remainder += transfer;
           
        }
       
        while (number != 0);
       
        NSLog(@"\nThe sum of all integers in %li is %li.", original_number, remainder);

    }
   
    return 0;
}


 23 
 on: July 16, 2015, 01:19:44 PM 
Started by barrettlikespizza - Last post by pif
Thanks for this thread. I'm actually running Yosemite on my Mac with Xcode 5.1 (yes, it's possible).

I didn't want to upgrade to a newer version in fear I won't be able to use this book anymore.

I guess I can. Smiley

 24 
 on: July 16, 2015, 04:12:08 AM 
Started by loop - Last post by pif
I have used two for loops to solve the Exercise 5.3, but I must say I like your solution better. It's more readable, more elegant so to speak.

Here's mine:

Code: (Objective-C)
// first ten integers factorial table

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])
{
    @autoreleasepool {
       
        int n, counter, factorial;
       
        NSLog(@"\nno. -------- factorial");
       
        for (n=1; n<=10; n++)
           
        {
            factorial = 1;
           
                for (counter=1; counter<=n; counter++)
                {
                    factorial = factorial * counter;
                }
               
            NSLog(@"\n%-2i -------- %8i", n, factorial);
           
        }
       
       
    }
   
    return 0;
}

 25 
 on: July 15, 2015, 03:00:43 PM 
Started by loop - Last post by pif
Exercise 5.1 states that we need a table for n and n2 respectively. You're missing n2 table.

Here's my take on it:

Code: (Objective-C)
// multiplication table exercise

#import <Foundation/Foundation.h>

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

{
    @autoreleasepool {
        int number, counter, nx, ndx;
       
        NSLog(@"\nThis program will print a multiplication table ranging from 1 to 10 for n and n squared. \nPlease enter n:");
        scanf ("%i", &number);
       
        NSLog(@" n ---- nx ----- n2x");
       
        for (counter=1; counter<=10; counter++)
           
        {
            nx = counter * number;
            ndx = counter * number * number;
           
            NSLog(@"\n%i ---- %i ----- %i", counter, nx, ndx);
        }
    }
   
    return 0;
}


 26 
 on: July 14, 2015, 01:24:56 PM 
Started by ballgeier - Last post by ballgeier
Hello,

I first started using Xcode a few months ago. I initially saw at the of the console a message that said the exit code is 0. I am now using Xcode 6.4 and I no longer see this exit code message. Is this the same for everybody? Is there some way for me to still see the exit code?

Thanks

 27 
 on: July 08, 2015, 07:00:39 PM 
Started by stuartjj - Last post by Pyr0T3chN3ck
I wonder if this is one of the situations where just because it "can" doesn't mean that it should. I know the book spoke about the use of static typing and why it is easier, also that consideration should be given to the simplicity of readability. I wonder if since the id type can hold the object as it is initialized just fine if that is isn't recommended. Also I wonder what happens when the dataValue is assigned another object? Does the rectangle simply disappear at that point?

 28 
 on: July 08, 2015, 12:45:31 AM 
Started by Learner - Last post by imsachinraut
may be this diagram will explain whole thing in simple language.

 29 
 on: July 05, 2015, 07:37:55 PM 
Started by Neo - Last post by djt
Just a comment or two:

This line

Code: (Objective-C)
for (d = 2; d < p ; ++p) 

should perhaps read

Code: (Objective-C)
for (d = 2; d < p ; ++d) 

Also, it's probably not a good idea to change the value of a loop index variable inside of the loop.

Lastly -- in case the answer to this question is not available elsewhere -- this same exercise is in the book "Programming in C, 3rd edition" and the solution can be found on this Forum.

From the Home page of the Forum scroll down and locate "Programming in C, 3rd ed." Then select "Answers to Odd-Numbered Exercises." Next select "Answers in this post." Then scroll down to the answer to exercise 6-7.

The program is in C rather than Objective-C but it's essentially the same.

djt



 30 
 on: July 04, 2015, 08:14:30 PM 
Started by Pyr0T3chN3ck - Last post by Pyr0T3chN3ck
Just looking for a little feedback on this one from someone. Smiley

//
//  Rectangle.h
//  Exercise 8.6 Code
//

#import <Foundation/Foundation.h>
#import "XYPoint.h"

@interface Rectangle: NSObject

@property float width, height;

- (void) setWidth: (float) w andHeight: (float) h;
- (void) setOrigin: (XYPoint *) aPoint;
- (XYPoint *) origin;
- (BOOL) containsPoint: (XYPoint *) aPoint;

@end

//
//  Rectangle.m
//  Exercise 8.6 Code
//

#import "Rectangle.h"

@implementation Rectangle
{
    XYPoint *origin;
}

@synthesize width, height;

- (void) setWidth: (float) w andHeight: (float) h
{
    width = w;
    height = h;
}

- (void) setOrigin: (XYPoint *) aPoint
{
    if (! origin)
        origin = [[XYPoint alloc] init];
   
    origin.x = aPoint.x;
    origin.y = aPoint.y;
}

- (XYPoint *) origin
{
    return origin;
}

- (BOOL) containsPoint: (XYPoint *) aPoint
{
    if ((aPoint.x < origin.x && aPoint.x > origin.x - width)
        && (aPoint.y > origin.y && aPoint.y < origin.y + height))
        return YES;
    else
        return NO;
           
}

@end

//
//  XYPoint.h
//  Exercise 8.6 Code
//

#import <Foundation/Foundation.h>

@interface XYPoint: NSObject

@property float x, y;

- (void) setX: (float) xVal andY: (float) yVal;

@end

//
//  XYPoint.m
//  Exercise 8.6 Code
//

#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

- (void) setX: (float) xVal andY: (float) yVal
{
    x = xVal;
    y = yVal;
}

@end

//
//  main.m
//  Exercise 8.6 Code
//

#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "XYPoint.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Rectangle *myRec = [[Rectangle alloc] init];
        XYPoint *myOrigin = [[XYPoint alloc] init];
        XYPoint *ptToCheck = [[XYPoint alloc] init];
       
        [myRec setWidth:4 andHeight:2];
        [myOrigin setX:1 andY:1];
       
        [myRec setOrigin:myOrigin];
       
        [ptToCheck setX:-1 andY:2];
       
        if ([myRec containsPoint:ptToCheck])
            NSLog(@"The point is contained in the rectangle");
        else
            NSLog(@"The point is not contained in the rectangle");
       
    }
    return 0;
}

2015-07-04 23:12:05.261 Exercise 8.6 Code[3238:1740464] The point is contained in the rectangle

changed point to x:10 y:10

2015-07-04 23:14:19.715 Exercise 8.6 Code[3248:1751516] The point is not contained in the rectangle

Pages: 1 2 [3] 4 5 ... 10


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.