Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- void printMatrix(NSMutableArray *dataArray, int size){
- NSMutableString *output = [[NSMutableString alloc] init];
- for (int i = 0; i < size; i++){
- for (int j = 0; j < size; j++){
- [output appendFormat:@"%4d", [dataArray[i][j] intValue]];
- }
- [output appendString:@"\n"];
- }
- NSLog(@"\n%@", output);
- }
- void fillMatrix(NSMutableArray *arrayMatrix, int size){
- int counter = 1;
- int y = 0;
- int x = 0;
- int newX = -1;
- int newY = 0;
- int direction = 1;
- while(counter <= size*size){
- switch(direction%4){
- case 0: newY--; break;
- case 1: newX++; break;
- case 2: newY++; break;
- case 3: newX--; break;
- }
- if(0 <= newX && newX < size && 0 <= newY && newY < size && [arrayMatrix[newY][newX] intValue] == 0){
- y = newY;
- x = newX;
- arrayMatrix[y][x] = [NSNumber numberWithInt: counter];
- } else{
- newX = x;
- newY = y;
- ++direction;
- continue;
- }
- ++counter;
- }
- }
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- int size = 5;
- NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity: size];
- for(int i = 0; i < size; i++){
- NSMutableArray *row = [[NSMutableArray alloc] init];
- for(int j = 0; j < size; j++){
- [row addObject: @0];
- }
- [dataArray insertObject:row atIndex:i];
- }
- fillMatrix(dataArray,size);
- printMatrix(dataArray,size);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement