Amazon.com Widgets Recent Posts
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 20, 2014, 09:02:12 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
|-+  Recent Posts
Pages: 1 2 [3] 4 5 ... 10

 21 
 on: December 09, 2014, 06:43:58 PM 
Started by windallt23 - Last post by windallt23
i have a translation app that i am working on and it translates a few colors and animals to Cherokee language  i need a sound button, when pressed I have a recorded wav that says black- gvnige te next screen has blue and it has its own button

 22 
 on: December 09, 2014, 09:40:19 AM 
Started by Saneok777 - Last post by kotyara
//  Rectangle.h

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
@property int width, height;
-(void) setWidth: (int) w andHeight: (int) h;
-(void) draw;

@end

//  Rectangle.m

#import "Rectangle.h"

@implementation Rectangle
@synthesize width, height;
-(void) setWidth:(int)w andHeight:(int)h{
    width = w;
    height = h;
}

-(void) draw{
    for (int i = 1; i<=height+2; ++i) {
        if (i==1 || i == height+2) {
            for (int j = 1; j<=width; ++j) {
                if (j != width) {
                    printf("-");
                } else {
                    printf("-\n");
                }
            }
        } else {
            for (int k = 1; k<=width; ++k) {
                if (k == 1) {
                    printf("|");
                }
                else if (k>1 && k<width) {
                    printf(" ");
                }
                else
                    printf("|\n");
            }
        }
    }
}
@end

//  main.m

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Rectangle *myRect = [[Rectangle alloc]init];
        [myRect setWidth:10 andHeight:3];
        [myRect draw];
    }
    return 0;
}



 23 
 on: December 03, 2014, 07:12:16 PM 
Started by Justwarrior - Last post by jonr
I discovered this too...the need to use 'self'.  In fact it's a typo and it's addressed in the errata sheet.  It seems when they put together the book they copy/pasted the code from what was part of an earlier chapter's exercises and forgot to add the 'self'.

I think the reason for this is that the methods in the category don't have direct access to the instance variables numerator and denominator. Thus, you have to use the 'getter' to do this.  Here's what I mean:
Code: (Objective-C)
self.numerator
is the same as
Code: (Objective-C)
[self numerator]
For me (as more of a beginning Obj-C coder) I tend to forget this.  Although I like dot notation, I find it gets in the way sometimes of what's actually going on 'under the hood'.

 24 
 on: December 03, 2014, 02:23:30 PM 
Started by nicjth0 - Last post by BrianLawson
They are equally precise, mine is just a bit more efficient. Your algorithm calculates each factorial starting at one for every number between 1 and 10. That means by the time you get to factorial 10, you have re-calculated factorial 2 8 times. Mine simply uses the fact that I have already calculated the factorial of 2 and goes from there to figure out the factorial of 3.

 25 
 on: December 03, 2014, 01:17:44 PM 
Started by nicjth0 - Last post by nicjth0
Thanks for the response and explanation. I would be lying if I said I did't have to use these boards to get a basis for my coding, for 5.3, perhaps there I started on the wrong foot. I see how yours is much easier and precise. Thank you.

 26 
 on: December 03, 2014, 03:04:32 AM 
Started by sylvester711 - Last post by kotyara
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        long unsigned number;
        long unsigned numberHolder;
        long unsigned reversed = 0;
        NSLog(@"Enter your number");
        scanf("%lu", &number);
   
// in case the number is zero
       
        if (number == 0) {
            NSLog(@"zero");
        }
        else
        {
        numberHolder = number;

// reverse the number

        while (number != 0) {
            long unsigned remainder = number % 10;
            reversed = reversed * 10 + remainder;
            number = number / 10;
        }
       
// print the last digit of the reversed number
       
        while (reversed != 0) {
            long unsigned remainder = reversed % 10;
            switch (remainder)
            {
                case 1:
                    NSLog(@"one");
                    break;
                case 2:
                    NSLog(@"two");
                    break;
                case 3:
                    NSLog(@"three");
                    break;
                case 4:
                    NSLog(@"four");
                    break;
                case 5:
                    NSLog(@"five");
                    break;
                case 6:
                    NSLog(@"six");
                    break;
                case 7:
                    NSLog(@"seven");
                    break;
                case 8:
                    NSLog(@"eight");
                    break;
                case 9:
                    NSLog(@"nine");
                    break;
                case 0:
                    NSLog(@"zero");
                    break;
            }
            reversed = reversed/10;
        }
   
// take care of trailing zeros in the number
       
        while (numberHolder % 10 == 0) {
            NSLog(@"zero");
            numberHolder = numberHolder / 10;
        }
        }
    }
    return 0;
}

 27 
 on: December 03, 2014, 02:59:41 AM 
Started by 星如雨 - Last post by kotyara
#import <Foundation/Foundation.h>

@interface Calculator : NSObject
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
-(double) add: (double) value;
-(double) subtract: (double) value;
-(double) multiply: (double) value;
-(double) divide: (double) value;
@end

@implementation Calculator
{
    double accumulator;
}
-(void) setAccumulator: (double) value{
    accumulator = value;
    NSLog(@"= %.2f", accumulator);
}
-(void) clear{
    accumulator = 0;
}
-(double) accumulator{
    NSLog(@"= %.2f", accumulator);
    return accumulator;
}
-(double) add:(double)value{
    accumulator += value;
    NSLog(@"= %.2f", accumulator);
    return accumulator;
}
-(double) subtract:(double)value{
    accumulator -= value;
    NSLog(@"= %.2f", accumulator);
    return accumulator;
}
-(double) multiply:(double)value{
    accumulator *= value;
    NSLog(@"= %.2f", accumulator);
    return accumulator;
}
-(double) divide:(double)value{
    if (value != 0) {
        accumulator /= value;
        NSLog(@"= %.2f", accumulator);
        return accumulator;
    } else {
        NSLog(@"Error - division by zero.");
        return accumulator = NAN;
    }
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Calculator * descCalc = [[Calculator alloc] init];
       
        double number;
        char operator;
       
        NSLog(@"Enter your number and operator.");
        scanf("%lf %c", &number, &operator);
 
        while (operator != 'E' && operator != 'e'){
            if (operator == 'S' || operator == 's') {
                [descCalc setAccumulator:number];
                NSLog(@"Enter your number and operator.");
                scanf("%lf %c", &number, &operator);
            }
            else if (operator == '+'){
                [descCalc add: number];
                NSLog(@"Enter your number and operator.");
                scanf("%lf %c", &number, &operator);
            }
            else if (operator == '-'){
                [descCalc subtract: number];
                NSLog(@"Enter your number and operator.");
                scanf("%lf %c", &number, &operator);
            }
            else if (operator == '*'){
                [descCalc multiply: number];
                NSLog(@"Enter your number and operator.");
                scanf("%lf %c", &number, &operator);
            }
            else if (operator == '/'){
                [descCalc divide:number];
                NSLog(@"Enter your number and operator.");
                scanf("%lf %c", &number, &operator);
            }
            else{
                NSLog(@"Unknown operator. Try again.\nEnter your number and operator.");
                scanf("%lf %c", &number, &operator);
            }
        }
       
        NSLog(@"End of program.");
        [descCalc accumulator];
    }
    return 0;
}

 28 
 on: December 03, 2014, 02:54:49 AM 
Started by 0NobHut - Last post by kotyara
#import <Foundation/Foundation.h>

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

        int number, rightDigit;
        NSLog(@"Enter your number");
        scanf("%i", &number);
       
        if (number > 0) {
            do {
                rightDigit = number % 10;
                NSLog(@"%i", rightDigit);
                number /= 10;
            } while (number != 0);
        }
        else {
            number = -number;
            do {
                rightDigit = number %10;
                NSLog(@"%i", rightDigit);
                number /=10;
            } while (number != 0);
            NSLog(@"-");
        }
    }
        return 0;
}

 29 
 on: December 02, 2014, 06:58:40 AM 
Started by nicjth0 - Last post by BrianLawson
This algorithm is very inefficient in that it has 2 nested for loops to compute the factorial where 1 is all that is needed. The outer loop controls how many factorials are computed and the inner loop does the actual factorial computation. Because the factorial is computed in the inner loop, starting from 1 each time, the factorial variable needs to be reset to one before each run of the inner loop.

Here is a better way to do it:
Code: (Objective-C)
#import <Foundation/Foundation.h>

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

@autoreleasepool {
   
   int fact = 1;
NSLog(@"Factorials of 1 through 10:");

for (int n = 1; n <= 10; n++)
{
fact *= n;
NSLog(@"%2i! = %i", n, fact);
}
   
}
    return 0;
}

 30 
 on: December 02, 2014, 02:01:27 AM 
Started by nicjth0 - Last post by nicjth0
Hi All, I was wondering if someone would be able to give me a better understanding of how the last 'Factorial = 1' works in the code, below. I understand that it does something to the end result after each increment but I don't quite get exactly what that is.

My code:
#import <Foundation/Foundation.h>


int main (int argc, char * argv[])
{
    @autoreleasepool {
        int n, m, factorial;
        
        NSLog(@"Table: 1- 10 and their factorials");
        NSLog(@"1-10---Factorial #");
        NSLog(@"---    ---");
        
        factorial = 1;
        
        for (n = 1; n <= 10; ++n)
        {
               for (m=1;m<=n;++m)
        {
               factorial *=m;
        }
               NSLog(@"%2i   %3i", n, factorial);
               factorial = 1;
        }
}
    return 0;
}

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.