Amazon.com Widgets Exercise 4.4 Did I get it?
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 06:43:14 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 4
| | | |-+  Exercise 4.4 Did I get it?
Pages: [1]   Go Down
Print
Author Topic: Exercise 4.4 Did I get it?  (Read 929 times)
JackRabbit
Jr. Member
**
Posts: 54



Email




« on: April 20, 2012, 12:11:01 AM »

Greetings everyone.

Here is my answer for Exercise 4.

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

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

        @autoreleasepool
            {
                float x = 2.55;
                float total;
                
                total = 3 * (x * x * x) - 5 * (x * x) + 6;
                
                NSLog (@"3 * (x * x * x) - 5 * (x * x) + 6 = %g", total);
            }
        
        return 0;
    }

Out put:

 3 * (x * x * x) - 5 * (x * x) + 6 = 23.2316

Well, did I get it?

JR
Logged
rue
Jr. Member
**
Posts: 53






« Reply #1 on: April 22, 2012, 10:49:45 AM »

I think so, I just got the same answer too.

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

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

    @autoreleasepool {
       
        // evaluate 3x^3 - 5x^2 + 6 for x=2.55
       
        double x,answer;
       
        x = 2.55;
       
        answer = 3*(x*x*x) - (5*(x*x)) + 6 ;
       
        NSLog(@"Answer is %g",answer);
       
    }
    return 0;
}

Answer is 23.2316
Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




« Reply #2 on: April 23, 2012, 09:44:06 PM »

Hello rue!

What a relief! I hate this math stuff. But I better learn to love it! OK. On to Exercise 4.5.

And thank you for your feedback.

JR
Logged
clouded
Full Member
***
Posts: 123






« Reply #3 on: May 09, 2012, 07:24:14 AM »

Here's my solution pretty similar:

/
Code: (Objective-C)
/ Chapter 4 Exercise 4.
// Write a program to evaluate the polynomial shown here
// (to calculate the exponents, just do straight
// multiplication — there is no exponentiation operator
// in Objective-C):
//              3x^3 - 5x^2 + 6
//              for x = 2.55

#include <Foundation/Foundation.h>

int main(int argc, const char *argv[]) {
    
    @autoreleasepool {
        double eval, x = 2.55;
        eval = (3 * (x * x * x)) - (5 * (x * x)) + 6;
        NSLog(@"3x^3 - 5x^2 + 6; for x = 2.55 equals %f", eval);
    }
    return 0;
}
« Last Edit: May 11, 2012, 05:35:05 PM by clouded » Logged
Sequals3
Newbie
*
Posts: 1






« Reply #4 on: June 17, 2012, 02:22:48 PM »

I might have cheated on this one, because I used the math.h class that looks to be included in Foundation.h when it's imported.  Here is my code and solution.  What you guys have is correct though.

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

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

    @autoreleasepool {
        double x = 2.55, result = 3*(pow(x, 3))-5*(pow(x, 2))+6;
        NSLog(@"3x^3 - 5x^2 + 6 = %g", result);
    }
    return 0;
}

« Last Edit: June 17, 2012, 02:26:37 PM by Sequals3 » 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.