Objective-C loop logic

I am really new to programming in Objective-C, my background in labview, which is a graphical programming language, I worked with Visual Basic, as well as HTML / CSS. I am trying to figure out the logic for creating a data array for the template below. I will need a template later to extract data from the other 2 arrays in a specific order.

I can do this by specifying a = 1, b = 2, c = 3, etc., and then create an array using a, b, c, but I want to use a loop so that I don't have 8 links above the array . These links will be used to generate a different generation of data, so if I canโ€™t get help figuring out the logic, I actually get 72 links over the array.

// This is the first one that gives the pattern

0 0 0 0 (etc.) // 1 1 1 1 // 2 2 2 2 2

NSMutableArray * expSecondRef_one = [NSMutableArray array]; int a1 = 0; while (a1 < 9) { int a2 = 0; while (a2 < 8) { NSNumber * a3 = [NSNumber numberWithInt:a1]; [expSecondRef_one addObject:a3]; a2++; } a1++; } 

// This is the second one I stumble on, I'm looking for a template

 1 2 3 4 5 6 7 8 // 0 2 3 4 5 6 7 8 // 0 1 3 4 5 6 7 8 // 0 1 2 4 5 6 7 8 // etc to -> // 0 1 2 3 4 5 6 7 

If you run it on a line, every ninth number is -1, but I donโ€™t know how to do this from the pattern of 8.

Thanks in advance!

Graham

+4
source share
3 answers

I think you are looking for something like:

 for(int i = 0; i < 9; ++i) { for (int j = 0; j < 8; ++j) { if (j < i) { //Insert j into array } else { //Insert j + 1 into array } } } 

I left the code to actually insert numbers into the array.

I donโ€™t quite understand how you use this array, but if this is just the order of indexes for accessing data from another group of arrays, you can skip the first set of arrays and just use this loop to access your data later.

- edit -

If I understand you correctly, you want to compare each index in an array of 9 numbers with every other index, and then save the results in an array. If so, you can just do something like this:

 for (int i = 0; i < 9; ++i) { for (j = 0; j < 9; ++j) { if (j != i) { //Compare object at array index i with object at array index j } } } 
+2
source

This loop works fine at the meta level for what I was trying to do. The array I created was supposed to refer to the original array (9 cells). The algorithm you cited has eliminated them and I can refer to the original array exactly as I wanted.

Thank you very much.

Greetings

Graham

0
source
 NSMutableArray *a=[[NSMutableArray alloc]init]; for(int i=0;i<8;i++){ NSMutableString *s=[[NSMutableString alloc]init]; for(int j=0;j<8;j++){ if(i!=j){ [s appendString:[NSString stringWithFormat:@"%i",j]]; } } [a addObject:s]; } NSLog(@"%@",a); } 

output:

 1234567, 0234567, 0134567, 0124567, 0123567, 0123467, 0123457, 0123456 
0
source

Source: https://habr.com/ru/post/1334801/


All Articles