Amazon.com Widgets Recent Posts
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 25, 2014, 04:02:45 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: July 14, 2014, 10:09:49 AM 
Started by harrykar - Last post by BrianLawson
Hi Harry,

Sorry I misunderstood the question. I also misread the %i (eye) for %l (ell) my bad.

Your example provides the answer to your question. Smiley When an integer value is read using the %i format specifier, it performs conversion between octal & hexadecimal inputs to a decimal value. Octal values are specified by entering a leading 0 (zero), and hexadecimal values are entered by leading with the characters '0x'. The %d format specifier reads whatever value is entered strictly as a decimal value. When 0x5 is input to the following code snippet scanf(%d, &x); printf("%i", x) 0 is what gets printed. scanf stops reading the input at the x character with the %d format specifier.

 22 
 on: July 14, 2014, 08:53:49 AM 
Started by harrykar - Last post by harrykar
You use %d when the variable you are scanning into is an int, %l is for a long int.

Hi  Brian
I know about the scanf()'s  format specifiers
Code: (Objective-C)
char c, s[10]; int i; float f; double d;

 scanf("%c", &c); // reads next character and puts its value in c

 scanf("%s", s); // reads next word and converts it to a string

 scanf("%i", &i); // reads next word and converts it to an integer

 scanf("%d", &i); // reads next word and converts it to an integer

 scanf("%f", &f); // reads next word and converts it to a float

 scanf("%lf", &d); // reads next word and converts it to a double

 scanf("%c %s %i %f", &c, s, &i, &f); // multiple reads

 printf("c=%c s=%s i=%i r=%3.1f\n", c, s, i, f);
 
 while (scanf("%c %s %i %f", %c, s, &i, &f) < 4) {

     // do the appropriate error treatment and retry
 
 }// at this step, the 4 input fields have been readed successfully

etc...



...but my point instead was different... let's illustrate it trough an example it's better  Smiley
Code: (Objective-C)
#include <stdio.h>
#include <stdbool.h> // for true macro in while loop


int main (void) {

  int n_i, n_d;   //scanf()'s input list
  int error_i, error_d; // scanf() returns the number of successfully matched and assigned input
           // items as a value


  printf("Press Ctrl-C to break the inf loop!!!\n\n");  // Prompt
  while ( true ) {

 printf("%%i Case: Give me a number: "); // Prompt
 error_i = scanf( "%i", &n_i);
 printf("you have pressed %i\n\n", n_i);
 printf("scanf's number of successfully matched and assigned input items: %d\n\n", error_i);

 printf("%%d Case: Give me a number: "); // Prompt
 error_d = scanf( "%d", &n_d);
 printf("you have pressed %d\n", n_d);
 printf("scanf's number of successfully matched and assigned input items: %d\n\n", error_d);

 printf("Press Ctrl-C to break the inf loop!!!\n\n");  // Prompt
  }

}

A)
if you try as input 10 (in either cases %i and %d) you get that output:
Code: (Objective-C)
Press Ctrl-C to break the inf loop!!!

%i Case: Give me a number: 10
you have pressed 10
scanf's number of successfully matched and assigned input items: 1

%d Case: Give me a number: 10
you have pressed 10
scanf's number of successfully matched and assigned input items: 1

B)
if you try as input 010 (in either cases %i and %d) you get that output:
Code: (Objective-C)
Press Ctrl-C to break the inf loop!!!

%i Case: Give me a number: 010
you have pressed 8
scanf's number of successfully matched and assigned input items: 1

%d Case: Give me a number: 010
you have pressed 10
scanf's number of successfully matched and assigned input items: 1

C)
if you try as input 0xA (in either cases %i and %d) you get that output:
Code: (Objective-C)
Press Ctrl-C to break the inf loop!!!

%i Case: Give me a number: 0xa
you have pressed 10
scanf's number of successfully matched and assigned input items: 1

%d Case: Give me a number: 0xa
you have pressed 0
scanf's number of successfully matched and assigned input items: 1

Now let's connect all that experiments;
Empirically we got that :
firstly scanf() reads and assign correctly his input and

in A) %d and %i have similar behaviour : scanf() converts the input string into a decimal (base 10) number

in B)  scanf(%i,) converts the input string 010  into an octal number (base 10) instead
        scanf(%d, ) converts the input string 010  into a decimal number (base 10)

in C)  scanf(%i,) converts the input string 01xA  into an hex number (base 16) instead  
        scanf(%d, ) converts the input string 01xA  into a decimal number (base 10) (in that case takes 0 and get rid of xA)



PS:
1. in the latest case for some reason the console's output goes speedily down  so to see the output i add
#include <stdlib.h>
system("pause");  // after the last printf()
and yes actually i am far away from my loved POSIX environment Sad


2.
Actually i haven't time but i promise to myself to clear that question soon; Almost i hope now my question should be clear

Ceers,
Harry

 23 
 on: July 14, 2014, 06:24:33 AM 
Started by harrykar - Last post by harrykar
I don't saw a errata and/or suggestion section for C here so i open that thread for report everything IMHO is not correct in the book or do suggestions


p 100 Chapter 7 Working with Arrays  
Code: (Objective-C)
Program 7.1 Output
values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

Although the 0's meaning in that list  is correctly (wordy) explained maybe is better signing with junk, cruft ( http://en.wikipedia.org/wiki/Cruft ) or otherwise instead of 0 values? before read the explanation below that list in book i thunk "values is automatic how is possible autoinitialize to 0 his unused places?"

 24 
 on: July 14, 2014, 05:23:10 AM 
Started by harrykar - Last post by BrianLawson
You use %d when the variable you are scanning into is an int, %l is for a long int.

 25 
 on: July 13, 2014, 03:35:49 PM 
Started by harrykar - Last post by harrykar
Actually i'm starting Ch 7 about arrays. So far in book i encountered  scanf("%i", ) but no scanf("%d", ); if i remember well somewhere in the book is explicitly reported that "we use the scanf("%i", ) throughout the book"; curiosity arise and i did some tests; as result i notice  different behaviour; Right now i haven't a clear idea about that issue; In substance my question is: what' s  precisely the difference among them ?

TIA
Harry G. K.  

 26 
 on: July 12, 2014, 08:36:21 AM 
Started by WREllis - Last post by ironcrush
My attempt in C.
I am actually following the "Programming in C" a wonderful book for C programming by Stephen Kochan.  But unfortunately i haven't found a support site like this of this book. This site is a great help.

Any feedback would be highly appreciated. Thanks.

Code: (Objective-C)
/* A program that displays integer digits in English */

#include <stdio.h>

int main(void)
{
    int number, i, DigitToPrint;
   
    printf("Enter a number: ");
    scanf("%i", &number);
   
    if(number < 0)
    {
        number = -number;
        printf("Negetive ");
    }
    if( number ==  0)
    {
        printf("zero ");
    }
    else
    {
        /*
        *Get the place value of the number in base 10
        *i.e if number is 12, then its place value is 10 since 10^1 = 10
        */
        for(i = 1; number > (i * 10); i *= 10);
       
        while(i > 0)
        {
            /*We have the place value in 'i'
            *to get the left-most digit devide the number by its place value 'i'.
            *The modulo 10 of the result will return the digit.
            */
            DigitToPrint = (number / i) % 10;
            switch (DigitToPrint)
            {
                case 0: printf("zero "); break;
                case 1: printf("one "); break;
                case 2: printf("two "); break;
                case 3: printf("three "); break;
                case 4: printf("four "); break;
                case 5: printf("five "); break;
                case 6: printf("six "); break;
                case 7: printf("seven "); break;
                case 8: printf("eight "); break;
                case 9: printf("nine "); break;
            }
            //Set the place value to the next left-most digit
            i /= 10;
        }
    }

    printf("\n");

    return 0;
}

 27 
 on: July 11, 2014, 12:11:16 PM 
Started by Braintrash - Last post by Braintrash
First, I took my time to be sure to grasp everything, which I think I did; correct me as much as needed, especially since I have the feeling I miss some things.

Then, an errata: on page 48, the output of program 3.4 miss the colon which is present in the NSLog string on the same page.

Finally, my answers.

1.
Int: reserved word
6_05: names can't start with a number
A$: $ is not a valid character

2.
Class: Computer
Actions: start it, update it, clean it, close it, fix it

3.
[myComputer start]
[myComputer update]
[myComputer clean]
[myComputer close]
[myComputer fix]

4.
Actions with a car: prep it, drive it, fill it with gas, wash it, service it
Actions with a boat: prep it, drive it, fill it with gas, wash it, service it
Actions with a motorcycle: prep it, drive it, fill it with gas, wash it, service it
There is an overlap: all the actions are the same.

5.
This allow an aconomy of code: I can use the same action for many objects from several classes instead of writing one action per object.

6.
I see a disadvantage: I have to multiply the same code in order to do the same thing to similar objects.

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

//---- @interface section ----

@interface XYPoint: NSObject

-(void) setAbscissa: (int) x;
-(void) setOrdinate: (int) y;
-(int) abscissa;
-(int) ordinate;

@end

//---- @implementation section ----

@implementation XYPoint
{
    int abscissa;
    int ordinate;
}

-(void) setAbscissa: (int) x
{
    abscissa = x;
}

-(void) setOrdinate: (int) y
{
    ordinate = y;
}

-(int) abscissa
{
    return abscissa;
}

-(int) ordinate;
{
    return ordinate;
}

@end

//---- program section ----

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

    @autoreleasepool {
        XYPoint *myXYPoint = [[XYPoint alloc] init];
        // Could have been: XYPoint *myXYPoint = [XYPoint new]
       
        // Set coordinate to 2,4
        [myXYPoint setAbscissa: 2];
        [myXYPoint setOrdinate: 4];
       
        // Display the coordinate
        NSLog (@"The value of myXYPoint is: %i,%i", [myXYPoint abscissa], [myXYPoint ordinate]);
    }
    return 0;
}

 28 
 on: July 10, 2014, 11:15:06 AM 
Started by Jimmy Pete - Last post by Jimmy Pete
Look for 'error' throughout code.

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

@interface Calculator : NSObject

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

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

  // utility methods
    - (double) changeSign;
    - (double) reciprocal;
    - (double) xSquared;

    - (BOOL) error;

@end

@implementation Calculator
{
    double accumulator;
    BOOL error;
}

- (void) setError:(BOOL)e {
    error = YES;
}

- (BOOL) error {
    return error;
}

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

- (void) clear {
    accumulator = 0;
}

- (double) accumulator {
    return accumulator;
}

// arithmetic methods
- (double) add:(double)value {
    return accumulator += value;
}

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

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

- (BOOL) divide:(double)value {
    if (value != 0.0) {
        accumulator /= value;
        return error = NO;
    } else {
        NSLog(@"Division by zero.");
        accumulator = NAN;
        return error = YES;
    }
}

- (double) changeSign {
    return -(accumulator);
}

- (double) reciprocal {
    return 1 / accumulator;
}

- (double) xSquared {
    return accumulator * accumulator;
}

@end

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

    @autoreleasepool {
        double value1, value2;
        char operator;
        BOOL error;
       
        Calculator *deskCalc = [[Calculator alloc] init];
       
        printf("Type in your expression: ");
        scanf ("%lf %c %lf", &value1, &operator, &value2);
       
        [deskCalc setAccumulator:value1];
       
        switch (operator) {
            case '+':
                [deskCalc add:value2];
                break;
            case '-':
                [deskCalc subtract:value2];
                break;
            case '*':
                [deskCalc multiply:value2];
                break;
            case '/':
                error = [deskCalc divide:value2];
                break;
            default:
                NSLog(@"Unknown character.");
                error = YES;
        }
       
        if (error != YES) {
            NSLog(@"%.2f", [deskCalc accumulator]);
        }
   
    }
    return 0;
}

 29 
 on: July 10, 2014, 06:30:31 AM 
Started by Jimmy Pete - Last post by Jimmy Pete
Here's how I did this exercise. It's just a little bit different than the author, but results are identical. I also give the user the opportunity to set the start and limit numbers so that it's possible to list larger prime numbers without having to wade through all the lower ones first.

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        int start = 0;
        int limit = 0;
        int value = 0;
        BOOL isPrime = NO;
        int loop = 0;
       
        printf("Start? ");
        scanf("%i", &start);
        if (start < 1) {
            start = 1;
        }
       
        printf("Limit? ");
        scanf("%i", &limit);
       
        printf("All Prime Numbers from %i to %i\n\n", start, limit);
       
        // test for prime
        for (int number = start; number <= limit; ++number) {
            for (int check = number - 1; check > 1; check--) {
                value = number % check;
                if ((value == 0 && check != number)) {
                    isPrime = NO;
                }
            }
           
            // Print the result
            if (isPrime) {
                if (loop == 10) {
                    loop = 0;
                    printf("\n");
                }
                if (loop < 10) {
                    printf("%6i\t", number);
                }
                loop++;
            }
            isPrime = YES;
        }
    }
    return 0;
}

 30 
 on: July 09, 2014, 07:53:05 PM 
Started by ChristineG - Last post by BrianLawson
There are two possible areas that could be visible after the program runs if you've output to the Console. One is the Console and the other is the Variable area. By default, the Variable area is on the left and the Console is on the right. There are two buttons in the lower right corner of the window that control the visibility of the two areas. Click the button on the right to toggle the Console's visibility.

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.