Amazon.com Widgets Possible Solution to Exercise 13-3
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 25, 2014, 03:11:48 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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 13
| | | |-+  Possible Solution to Exercise 13-3
Pages: [1]   Go Down
Print
Author Topic: Possible Solution to Exercise 13-3  (Read 2637 times)
MrM
Newbie
*
Posts: 1






« on: March 18, 2009, 08:15:18 AM »

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

int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int i;
int n = 150;

int p[n + 1];
for (i = 2; i <= n; i++)
p[i] = 0;

for (i = 2; i <= n; i++)
{
if (p[i] == 0)
NSLog (@"%i", i);

for (int j = 1; i * j <= n; j++)
p[i * j] = 1;
}
   
        [pool drain];
        return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #1 on: March 18, 2009, 10:14:54 AM »

Excellent solution (also Jérôme's in the zip)!

Steve
Logged
Wc1100cc
Newbie
*
Posts: 2






« Reply #2 on: June 25, 2009, 02:54:20 PM »

Can someone kindly extrapolate how this code works. For some reason, I'm having a tough time understanding some parts of the code. for example, why is  int p[n + 1];  being used for the initialization of the array?
thanks in advance!
WC
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







« Reply #3 on: June 25, 2009, 09:51:37 PM »

This program uses C arrays, so make sure you're looking at this example after reading Chapter 13.

The declaration

Code: (Objective-C)
int  p [n + 1];

is a variable-length array declaration.  If you're a C programmer and haven't seen it before, it's because it's part of the ANSI C-99 definition. Also, because support for this feature wasn't added to all compilers at the same time, I don't think it gets used much by programmers.

Cheers,

Steve Kochan
« Last Edit: November 20, 2009, 02:17:00 PM by skochan » Logged
Wc1100cc
Newbie
*
Posts: 2






« Reply #4 on: June 26, 2009, 02:24:18 AM »

Thanks Steve!
Great book and great user support!!

Will
Logged
mdziedzic
Newbie
*
Posts: 41



WWW




« Reply #5 on: November 20, 2009, 02:06:35 PM »

Yet another solution.

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


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int n = 150;

int p[n], i, j;

// step 1
for (int k = 2; k <= n; k++)
p[k] = 0;

// step 2
i = 2;

// step 3
while (i <= n) {

// step 4
if (p[i] == 0)
NSLog(@"Prime: %i", i);

// step 5
j = 1;
while (j * i <= n) {
p[i * j] = 1;
j++;
}

// step 6
i++;
}
    
[pool drain];
    return 0;
}

« Last Edit: November 20, 2009, 02:08:36 PM by scarbelly » Logged
tadej5553
Full Member
***
Posts: 145


Email




« Reply #6 on: November 29, 2009, 07:32:50 AM »

This program uses C arrays, so make sure you're looking at this example after reading Chapter 13.

The declaration

Code: (Objective-C)
int  p [n + 1];

is a variable-length array declaration.  If you're a C programmer and haven't seen it before, it's because it's part of the ANSI C-99 definition. Also, because support for this feature wasn't added to all compilers at the same time, I don't think it gets used much by programmers.

Cheers,

Steve Kochan

OK, so let me get this right. You can actually declare and define the array with a variable as it's lenght?

But does this get done in compile or run time? Because this would really be useful if you could use scanf for a variable and then assign it as a array lenght.
And also, this doesn't work for me sometimes, do I have to change something in the project settings?
Logged
tadej5553
Full Member
***
Posts: 145


Email




« Reply #7 on: December 02, 2009, 03:17:51 AM »

So can somebody please answer me??
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #8 on: December 03, 2009, 04:53:09 AM »

Quote
But does this get done in compile or run time?
This is done at run time as the value of n is not set during compiling.

Quote
And also, this doesn't work for me sometimes, do I have to change something in the project settings?
Doesn't work how? Is there an error message?

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

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

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
 
  int maxCharacters = 0;
  char string1[11];
 
  NSLog (@"Enter the maximum number of characters for string2.");
  scanf ("%i", &maxCharacters);
   
  char string2[maxCharacters+1];
 
  NSLog(@"string1 size: %i", sizeof(string1));
  NSLog(@"string2 size: %i", sizeof(string2));
 
  [pool drain]; 
  return 0; 
}
Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
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.