Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:21:14 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: 15.10  (Read 1368 times)
clp274
Newbie
*
Posts: 8


Email
« on: December 23, 2011, 01:45:16 PM »

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        NSNumber *myInt =  [NSNumber numberWithInt:0];
        NSNumber *myInt1 = [NSNumber numberWithInt:1];
        NSNumber *myInt2 = [NSNumber numberWithInt:2];
        NSNumber *myInt3 = [NSNumber numberWithInt:3];
        NSNumber *myInt4 = [NSNumber numberWithInt:4];
        NSNumber *myInt5 = [NSNumber numberWithInt:5];
        NSNumber *myInt6 = [NSNumber numberWithInt:6];
        NSNumber *myInt7 = [NSNumber numberWithInt:7];
        NSNumber *myInt8= [NSNumber numberWithInt:8];
        NSNumber *myInt9 = [NSNumber numberWithInt:9];
   
   
    NSArray *numArray = [NSArray arrayWithObjects:myInt,myInt1,myInt2,myInt,myInt,myInt,myInt,myInt,myInt5,myInt5,myInt4,myInt3,myInt,myInt4,myInt8,myInt7,myInt9,myInt3,myInt4,myInt6, nil];
   
    NSCountedSet *freq = [[NSCountedSet alloc] initWithArray:numArray];
   
     NSLog(@"0: %lu", [freq countForObject:myInt]);
     NSLog(@"1: %lu", [freq countForObject:myInt1]);
     NSLog(@"2: %lu", [freq countForObject:myInt2]);
     NSLog(@"3: %lu", [freq countForObject:myInt3]);
     NSLog(@"4: %lu", [freq countForObject:myInt4]);
     NSLog(@"5: %lu", [freq countForObject:myInt5]);
     NSLog(@"6: %lu", [freq countForObject:myInt6]);
     NSLog(@"7: %lu", [freq countForObject:myInt7]);
     NSLog(@"8: %lu", [freq countForObject:myInt8]);
     NSLog(@"9: %lu", [freq countForObject:myInt9]);
    [pool drain];
    return 0;
}

I don't like the fact that I have to hard-code everything. Have a general question. Maybe someone can help me. Is it possible to create a C array such as something like

int numbers[100000];

for( int i =0; i <100000; i++) {
         numbers = rand() % 15;
}

Can I then store those registered values into a NSCountedSet and find the frequency of each of the numbers 0-14?

Logged
fav321
Newbie
*
Posts: 8


Email
« Reply #1 on: December 27, 2011, 09:51:19 PM »

Here's my version. It kinda shows how you can do it with less lines of code.
Notice the #define statement near the top.

Code: (Objective-C)
#import <Foundation/Foundation.h>		
#define INTVAL(x) [NSNumber numberWithInteger: x]

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

NSArray *nums = [NSArray arrayWithObjects: INTVAL(1), INTVAL(81), INTVAL(-6), INTVAL(21), INTVAL(81), INTVAL(3), INTVAL(3),INTVAL(81),
INTVAL(81), INTVAL(46), INTVAL(2), INTVAL(19), nil];

NSCountedSet *myCountedSet = [[NSCountedSet alloc] initWithArray: nums];

// Generate frequency chart
NSLog(@"%-10@ %-10@", @"Number", @"Frequency");
for (NSNumber *theNum in myCountedSet)
NSLog(@"%-10i %-3i", [theNum integerValue], [myCountedSet countForObject: theNum]);

[myCountedSet release];
[pool drain];
    return 0;
}
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.