RosettaCodeData/Task/Create-a-two-dimensional-array-at-runtime/Objective-C/create-a-two-dimensional-array-at-runtime.m

27 lines
628 B
Mathematica
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
#import <Foundation/Foundation.h>
int main()
{
2014-04-02 16:56:35 +00:00
@autoreleasepool {
int num1, num2;
scanf("%d %d", &num1, &num2);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
NSLog(@"%d %d", num1, num2);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
NSMutableArray *arr = [NSMutableArray arrayWithCapacity: (num1*num2)];
// initialize it with 0s
for(int i=0; i < (num1*num2); i++) [arr addObject: @0];
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
// replace 0s with something more interesting
for(int i=0; i < num1; i++) {
for(int j=0; j < num2; j++) {
arr[i*num2+j] = @(i*j);
}
2013-04-10 22:43:41 -07:00
}
2014-04-02 16:56:35 +00:00
// access a value: i*num2+j, where i,j are the indexes for the bidimensional array
NSLog(@"%@", arr[1*num2+3]);
}
2013-04-10 22:43:41 -07:00
return 0;
}