Amazon.com Widgets Recent Posts
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 02, 2015, 11:17:09 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: June 24, 2015, 08:13:40 PM 
Started by Pyr0T3chN3ck - Last post by Pyr0T3chN3ck
Just looking for a little feedback.

//
//  Exercise 6.3 Code
//

#import <Foundation/Foundation.h>

// ----- @interface section
@interface Fraction: NSObject

- (void) print;
- (void) setNumerator: (double) n;
- (void) setDenominator: (double) d;
- (int) numerator;
- (int) denominator;
- (double) convertToNum;

@end

// ----- @implementation section
@implementation Fraction
{
    int numerator;
    int denominator;
}

- (void) print
{
    if (numerator == 0)
        NSLog(@"Zero");
    else if (numerator % denominator == 0)
        NSLog(@"%i", numerator / denominator);
    else
        NSLog(@"%i/%i", numerator, denominator);
}

- (void) setNumerator: (double) n
{
    numerator = n;
}

- (void) setDenominator: (double) d
{
    denominator = d;
}

- (int) numerator
{
    return numerator;
}

- (int) denominator
{
    return denominator;
}

- (double) convertToNum;
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}

@end

// ----- program section
int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Fraction *myFraction = [Fraction new];
       
        [myFraction setNumerator:0];
        [myFraction setDenominator:10];
       
        [myFraction print];
    }
    return 0;
}

 22 
 on: June 24, 2015, 08:06:50 PM 
Started by Pyr0T3chN3ck - Last post by Pyr0T3chN3ck
Just looking for a little feedback, gets the expected results as outlined in the exercise

//
//  Exercise 6.4 Code
//

#import <Foundation/Foundation.h>

//----- @interface section -----//
@interface Calculator: NSObject
//----- accumulator methods
- (void) setAccumulator: (double) value;
- (void) clear;
- (double) accumulator;

//----- arithmetic methods
- (void) add: (double) value;
- (void) subtract: (double) value;
- (void) multiply: (double) value;
- (void) divide: (double) value;
@end

//----- @implementation section -----//
@implementation Calculator
{
    double accumulator;
}

- (void) setAccumulator: (double) value
{
    accumulator = value;
}

- (void) clear
{
    accumulator = 0;
}

- (double) accumulator
{
    return accumulator;
}

- (void) add: (double) value
{
    accumulator += value;
}

- (void) subtract: (double) value
{
    accumulator -= value;
}

- (void) multiply: (double) value
{
    accumulator *= value;
}

- (void) divide: (double) value
{
    if (value == 0)
        NSLog(@"Division by Zero");
    else
        accumulator /= value;
}

@end

//----- program section -----//
int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        BOOL readyToExit = NO;
        double number;
        char operator;
       
        Calculator *simpleCalc = [Calculator new];
       
        NSLog(@"Simple Printing Calculator");
        NSLog(@"Accepts the Following Operations");
        NSLog(@"     - + : Addition");
        NSLog(@"     - - : Subtraction");
        NSLog(@"     - * : Multiplication");
        NSLog(@"     - / : Division");
        NSLog(@"     - S : Set Accumulator");
        NSLog(@"     - E : Exit Calculator");
       
        do
        {
            scanf("%lf %c", &number, &operator);
           
            switch (operator)
            {
                case '+':
                {
                    [simpleCalc add: number];
                    break;
                }
                case '-':
                {
                    [simpleCalc subtract: number];
                    break;
                }
                case '*':
                {
                    [simpleCalc multiply: number];
                    break;
                }
                case '/':
                {
                    [simpleCalc divide: number];
                    break;
                }
                case 'S':
                {
                    [simpleCalc setAccumulator: number];
                    break;
                }
                case 'E':
                {
                    readyToExit = YES;
                    break;
                }
                default:
                {
                    NSLog(@"Invalid Operator Entered");
                    break;
                }
            }
            NSLog(@"= %lf", [simpleCalc accumulator]);
        }
        while (readyToExit == NO);
       
        //NSLog(@"= %lf", [simpleCalc accumulator]);
    }

    NSLog(@"End of Calculations");
   
    return 0;
}

 23 
 on: June 23, 2015, 08:09:52 PM 
Started by Pyr0T3chN3ck - Last post by Pyr0T3chN3ck
Just looking for a little feedback again. My changes did result in the output not displaying the accumulator if division by 0 or an invalid operator is entered. Personally I think there would be a better way to do it, but this way was quick and obtained the required results.

//
//  Exercise 6.2 Code
//

#import <Foundation/Foundation.h>

@interface Calculator: NSObject

// accumulator methods
- (void) setAccumulator: (double) value;
- (void) clear;
- (double) accumulator;

// arithmetic methods
- (void) add: (double) value;
- (void) subtract: (double) value;
- (void) divide: (double) value;
- (void) multiply: (double) value;

@end

@implementation Calculator
{
    double accumulator;
}

- (void) setAccumulator:(double) value
{
    accumulator = value;
}

- (void) clear
{
    accumulator = 0;
}

- (double) accumulator
{
    return accumulator;
}

- (void) add: (double) value
{
    accumulator += value;
}

- (void) subtract: (double) value
{
    accumulator -= value;
}

- (void) divide: (double) value
{
    accumulator /= value;
}

- (void) multiply: (double) value
{
    accumulator *= value;
}

@end

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        double value1, value2;
        char operator;
        BOOL okToDisplay;
       
        Calculator *deskCalc = [[Calculator alloc] init];
       
        NSLog(@"Type in your expression.");
        scanf("%lf %c %lf", &value1, &operator, &value2);
       
        okToDisplay = YES;
       
        [deskCalc setAccumulator:value1];
       
        if ( operator == '+' )
            [deskCalc add:value2];
        else if ( operator == '-' )
            [deskCalc subtract:value2];
        else if ( operator == '/' )
            if ( value2 == 0 )
            {
                NSLog(@"Division by zero");
                okToDisplay = NO;
            }
            else
                [deskCalc divide:value2];
            else if ( operator == '*' )
                [deskCalc multiply:value2];
            else
            {
                NSLog(@"Unknown Operator.");
                okToDisplay = NO;
            }
       
        if (okToDisplay)
            NSLog(@"%.2f", [deskCalc accumulator]);
       
    }
    return 0;
}

 24 
 on: June 23, 2015, 08:46:19 AM 
Started by Pyr0T3chN3ck - Last post by BrianLawson
That's a much better method, the program is both smaller and faster.

 25 
 on: June 23, 2015, 06:39:08 AM 
Started by Pyr0T3chN3ck - Last post by Pyr0T3chN3ck
Brian,

Thank you for the reply sir, I had started thinking about if there was a better way, I've been told that nested loops while "ok" to use, should be avoided if at all possible, so I had started to work to refine my code down to one loop. Here's what I coded.

//
//  Exercise 5.3 Code
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
     
        int num, factoral = 1;
       
        NSLog(@"TABLE OF FACTORIAL NUMBERS");
        NSLog(@" Number       Factorial");
        NSLog(@"--------------------------");
       
        for (num = 1; num <= 10; ++num)
        {
            factoral *= num;
            NSLog(@" %2i            %7i", num, factoral);
        }
       
    }
    return 0;
}

 26 
 on: June 22, 2015, 07:38:17 PM 
Started by Pyr0T3chN3ck - Last post by BrianLawson
The loop within a loop makes this algorithm very inefficient and much slower than it needs to be. While you don't notice the speed when computing 10!, it would make a huge difference on 500!. Smiley Your method keeps recomputing the same factorials over and over.

Keep in mind that n! = (n-1)! * n. This means that once you have computed the factorial of say 2, you don't need to do it again to compute the factorial of 3 since 3! = 2! * 3.

See if you can find an algorithm that lets you use this to compute the factorial table in just one loop. Ask questions if you need and I'll try to guide you through it.

 27 
 on: June 22, 2015, 04:21:31 PM 
Started by Pyr0T3chN3ck - Last post by Pyr0T3chN3ck
Just wanted some feedback on how I accomplished this exercise.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
     
        int num, factoral;
       
        NSLog(@"TABLE OF FACTORIAL NUMBERS");
        NSLog(@" Number       Factorial");
        NSLog(@"--------------------------");
       
        for (num = 10; num > 0; --num)
        {
            factoral = num;
            for (int i = num - 1; i > 0; --i)
            {
                factoral *= i;
            }
            NSLog(@" %2i         %10i", num, factoral);
        }
    }
    return 0;
}

 28 
 on: June 21, 2015, 08:44:43 PM 
Started by johnnydeveloper - Last post by johnnydeveloper
I want to draw gantt chart in UIcollectionviewcontroller in iPod? But I don't know how can I draw it? Is there any library in IOS help me to draw it? so Could you give me Idea to help me draw it?

 29 
 on: June 02, 2015, 10:07:42 AM 
Started by Uchenna - Last post by BrianLawson
You're welcome, I'm glad I could help.

Another thing that helps when trouble shooting these types of problems is to step through the program, line-by-line, with pencil and paper, writing down the value of each variable as it changes with the program's execution. Later in the book, you'll be introduced to the debugger which allows you to watch the program's execution and track the variables without having to use the pencil and paper. Smiley

 30 
 on: June 02, 2015, 08:09:02 AM 
Started by Uchenna - Last post by Uchenna
Thank you so much Brian Lawson. I understand it now.  Smiley Smiley Smiley Smiley

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.