Objective-C loop logic - objective-c

I'm really new to programming in Objective-C, my background is in labview which is a graphical programming language, I've worked with Visual Basic some and HTML/CSS a fair amount as well. I'm trying to figure out the logic to create an array of data for the pattern below. I need the pattern later to extract data from another 2 arrays in a specific order.
I can do it by referencing a = 1, b = 2, c = 3 etc and then creating the array with a, b, c but I want to use a loop so that I don't have 8 references above the array. These references will be used to generate another generation of data so unless I can get help figuring out the logic I'll actually end up with 72 references above the array.
// This is the first one which gives the pattern
0 0 0 0 (etc) // 1 1 1 1 // 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 which I'm stumbling over, I am looking for the pattern
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 in a line every 9th number is -1 but I don't know how to do that over a pattern of 8.
Thanks in advance!
Graham

I think you're 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 out the code to actually insert the numbers into the array.
I'm not totally clear on how you're using this array, but if this is just an order of indexes to access data from another group of arrays, you may be able to skip the first set of arrays and just use this loop to access your data later.
--edit--
If I'm understanding you correctly, you want to compare each index in an array of 9 numbers to every other index, then store the results in an array. If that's the case, you could 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
}
}
}

That loop works perfectly on a meta level for what I was trying to do. The array's that I was creating were to reference the original array (9 cells) With the algorithm you made I eliminated those and can reference the original array exactly as I wanted.
Thank you very much.
Cheers
Graham

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

Related

Looping over an NSmutatable Array starting from a certain index

I have a quick question how can I loop over an NSMutable array starting from a certain index.
For Example I have these double loops I want k to start from the same index as l.
for (Line *l in L)
{
for (Line *k in L)
{
............
}
}
To elaborate further, lets say L has 10 object so l start from 0-10 and k from 0 -10. What I want is if l is equal 1 k should start from 1-10 rather than 0 - 10 and when l is equal 2 k should start from 2- 10 rather than 0. Any help is Appreciated
Objective-C is an extension of C, lookup the C for loop and you'll have your answer. HTH
Addendum
I was going to let you benefit from the learning experience of looking up the C for yourself, however at the time of writing all other answers since added give the code but it is not complete, so here is what you need to produce the l and k values in the order you wish:
for(NSInteger lIndex = 0; lIndex < L.count; lIndex++)
{
Line *l = L[lIndex]; // index into your array to get the element
for(NSInteger kIndex = lIndex; kIndex < L.count; kIndex++)
{
Line *k = L[kIndex];
// process your l and k
}
}
As you can see the for has three sub-parts which are the initialisation, condition, and increment. The initialisation is performed first, then the condition to determine whether to execute the for body, and the increment is executed after the statements in the body and before the condition is tested to determine if another iteration should be performed. A for loop is roughly (there are some differences that are unimportant here) to the while loop:
initialisation;
while(condition)
{
body statements;
increment;
}
You simply need to modify for-statement.
NSInteger indexYouNeed;
NSInteger iterationCount;
for (int i = indexYouNeed; i < iterationCount; i++) {
/* Your code here */
}
You may find this link helpfulll.
You have to use an indexed (ordinary) for loop instead of fast enumeration (for-in):
int l;
for (l=startValue; l<=endValue; l++)
{
int i;
for (int i=l; i<=endValue; i++)
{
…
}
}

Large 2D Array Always Returns Undefined

I have a 2D array that absolutely will not return the values I need. I start off with this array:
var userdata:Array = new Array(new Array(1000),new Array(4))
Then I try to set all values to 0, with this:
this.onLoad()
{
for (i = 0; i < 1000; i++)
{
for (j = 0; j < 4; j++)
{
userdata[i][j] = 0
trace(userdata[i][j])
}
}
}
This trace returns 8 0s and then a giant amount of "undefined"s. I can't figure out why this would be. I try something like this as well:
userdata[5][0] = 0
trace(userdata[5][0])
It still returns "undefined". Can anyone help with this?
To understand why you got only 8 "zeros" and many undefined values, let's start by your array declaration :
var userdata:Array = new Array(new Array(1000),new Array(4));
Here you should understand that you have created an array with only 2 cells ( that's why userdata[5][0] is undefined ) : the 1st cell is an array of 1000 elements and the 2nd one is an array of 4 elements, and that's why you can only set 8 items ( 2 x 4 ) : the 4th first items from the 1000 of the the 1st cell + the the 4th first items from the 4 of the 2nd cell.
Let's return to your question, you want create a multidimensional array of 1000 rows and 4 columns. To start, we create an array of 1000 rows (cells) :
var a:Array = [1000]; // you can write it : new Array(1000);
Then, we create 4 columns for every row, and set values like this :
var i:Number, j:Number;
for (i = 0; i < 1000; i++)
{
// create the 4 columns
a[i] = [4]; // you can write it : a[i] = new Array(4);
for (j = 0; j < 4; j++)
{
a[i][j] = 0;
}
}
Then we can verify our array :
trace(a[0][0]); // gives : 0
trace(a[255][2]); // gives : 0
trace(a[255][5]); // gives : undefined, because we have only 4 columns
trace(a[1500][0]); // gives : undefined, because we have only 1000 rows
Hope that can help.

Checking a series of numbers for consistency

I maintain an array of integers. It is important that at all times the integers in this array are in sequence from 0. For example, if there are 5 integers in the array, their values must be 0, 1, 2, 3, 4 (though in any order).
I would like to design a simple, efficient method that checks this. It will return true if the array contains all positive integers in sequence from 0 to array.count - 1.
I would love to hear some different ideas for handling this!
Or, given the integers [0..N-1] if you raise 2 to the power of each in turn the sum will be -1+2^N. This is not a property that any other set of N integers has.
I offer this as an alternative, making no claim about suitability, performance or efficiency, and I recognise that there will be problems as N gets large.
Basically what you want to test is if your array is a permutation of [0...n-1]. There are easy algorithms for this that are O(n) in both time and memory. See for example this PDF file.
Sometimes I use a very simple check that is O(n) in time and O(1) in memory. It can in theory return false positives, but it is a good way to find most mistakes. It is based on the following facts:
0 + 1 + 2 + ... + n-1 == n * (n-1) / 2
0 + 1² + 2² + ... + (n-1)² == n * (n-1) * (2 * n - 1) / 6
I don't know objective-c, but the code would look like this in C#:
bool IsPermutation(int[] array)
{
long length = array.Length;
long total1 = 0;
long total2 = 0;
for (int i = 0; i<length; i++)
{
total1 += array[i];
total2 += (long)array[i] * array[i];
}
return
2 * total1 == length * (length - 1) &&
6 * total2 == length * (length - 1) * (2 * length - 1);
}
This isn't too different from your itemsSequencedCorrectlyInSet: method, but it uses a mutable index set which will be faster than doing -[NSSet containsObject:]. Probably not an issue until you've got thousands of table rows. Anyway, the key insight here is that the Pigeonhole Principle says if you've got N integers less than N and none is duplicated, then you have each of 0...N-1 exactly once.
-(BOOL)listIsValid:(NSArray*)list
{
NSMutableIndexSet* seen = [NSMutableIndexSet indexSet];
for ( NSNumber* number in list )
{
NSUInteger n = [number unsignedIntegerValue];
if ( n >= [array count] || [seen containsIndex:n] )
return NO;
[seen addIndex:n];
}
return YES;
}
Here's my current implementation (testSet is a set of NSNumbers) -
- (BOOL)itemsSequencedCorrectlyInSet:(NSSet *)testSet{
for (NSInteger i = 0; i < testSet.count; i++) {
if (![testSet containsObject:[NSNumber numberWithInteger:i]]) {
return NO;
}
}
return YES;
}

What's wrong with my pascal's triangle?

I've been looking for some simple coding challenges recently, and discovered about Pascal's triangle (here), and I've tried to generate one myself in C/Objective-C. For those that don't know what it is, that link explains it pretty well.
I'm starting to get oddness after the fourth row, and I just can't figure out why.
My output for 5 iterations currently looks like this:
1
1 1
1 2 1
1 3 3 1
4 6 3 1
It should look like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Here is my code so far. The first loop is just a reset loop (setting all the values to 0). The actual logic happens mostly in the second loop. The third loop is where the values are concatenated and formatted in a string.
I've commented this code much more than I would for myself just to aid readability.
int iterations, i, b, mid, chars, temp;
NSLog(#"Please enter the number of itereations");
scanf("%i",&iterations); // take users input and store it in iterations
// calculate where the first 1 should go.
if (iterations % 2 == 0) mid = (iterations)/2;
else mid = (iterations+1)/2;
chars = iterations*2;
int solutions[iterations][chars];
// reset loop
for (i = 0; i<iterations; i++) {
for (b = 0; b<chars; b++) {
solutions[i][b] = 0;
}
}
solutions[0][mid] = 1; // place the initial 1 in first row
for (int row = 1; row<iterations; row++) {
for (int chi = 0; chi<chars; chi++) {
temp = 0;
if (chi > 0) {
temp += solutions[row-1][chi-1]; // add the one diagonally left
}
if (chi < iterations) {
temp += solutions[row-1][chi+1]; // add the one diagonally right
}
solutions[row][chi] = temp; // set the value
}
}
// printing below...
NSMutableString *result = [[NSMutableString alloc] initWithString:#"\n"];
NSMutableString *rowtmp;
for (i = 0; i<iterations; i++) {
rowtmp = [NSMutableString stringWithString:#""];
for (b = 0; b<chars; b++) {
if (solutions[i][b] != 0) [rowtmp appendFormat:#"%i",solutions[i][b]];
else [rowtmp appendString:#" "]; // replace any 0s with spaces.
}
[result appendFormat:#"%#\n",rowtmp];
}
NSLog(#"%#",result);
[result release];
I have a feeling the problem may be to do with the offset, but I have no idea how to fix it. If anyone can spot where my code is going wrong, that would be great.
It appears (from a brief look) that the original midpoint calculation is incorrect. I think it should simply be:
mid = iterations - 1;
In the example of 5 iterations, the midpoint needs to be at array position 4. Each iteration "moves" one more position to the left. The 2nd iteration (2nd row) would then place a 1 at positions 3 and 5. The 3rd iteration at 2 and 6. The 4th at 1 and 7. And the 5th and last iteration would fill in the 1s at 0 and 8.
Also, the second if statement for the temp addition should be as follows otherwise it reads past the end of the array bounds:
if (chi < iterations - 1) {

Number of possible combinations

How many possible combinations of the variables a,b,c,d,e are possible if I know that:
a+b+c+d+e = 500
and that they are all integers and >= 0, so I know they are finite.
#Torlack, #Jason Cohen: Recursion is a bad idea here, because there are "overlapping subproblems." I.e., If you choose a as 1 and b as 2, then you have 3 variables left that should add up to 497; you arrive at the same subproblem by choosing a as 2 and b as 1. (The number of such coincidences explodes as the numbers grow.)
The traditional way to attack such a problem is dynamic programming: build a table bottom-up of the solutions to the sub-problems (starting with "how many combinations of 1 variable add up to 0?") then building up through iteration (the solution to "how many combinations of n variables add up to k?" is the sum of the solutions to "how many combinations of n-1 variables add up to j?" with 0 <= j <= k).
public static long getCombos( int n, int sum ) {
// tab[i][j] is how many combinations of (i+1) vars add up to j
long[][] tab = new long[n][sum+1];
// # of combos of 1 var for any sum is 1
for( int j=0; j < tab[0].length; ++j ) {
tab[0][j] = 1;
}
for( int i=1; i < tab.length; ++i ) {
for( int j=0; j < tab[i].length; ++j ) {
// # combos of (i+1) vars adding up to j is the sum of the #
// of combos of i vars adding up to k, for all 0 <= k <= j
// (choosing i vars forces the choice of the (i+1)st).
tab[i][j] = 0;
for( int k=0; k <= j; ++k ) {
tab[i][j] += tab[i-1][k];
}
}
}
return tab[n-1][sum];
}
$ time java Combos
2656615626
real 0m0.151s
user 0m0.120s
sys 0m0.012s
The answer to your question is 2656615626.
Here's the code that generates the answer:
public static long getNumCombinations( int summands, int sum )
{
if ( summands <= 1 )
return 1;
long combos = 0;
for ( int a = 0 ; a <= sum ; a++ )
combos += getNumCombinations( summands-1, sum-a );
return combos;
}
In your case, summands is 5 and sum is 500.
Note that this code is slow. If you need speed, cache the results from summand,sum pairs.
I'm assuming you want numbers >=0. If you want >0, replace the loop initialization with a = 1 and the loop condition with a < sum. I'm also assuming you want permutations (e.g. 1+2+3+4+5 plus 2+1+3+4+5 etc). You could change the for-loop if you wanted a >= b >= c >= d >= e.
I solved this problem for my dad a couple months ago...extend for your use. These tend to be one time problems so I didn't go for the most reusable...
a+b+c+d = sum
i = number of combinations
for (a=0;a<=sum;a++)
{
for (b = 0; b <= (sum - a); b++)
{
for (c = 0; c <= (sum - a - b); c++)
{
//d = sum - a - b - c;
i++
}
}
}
This would actually be a good question to ask on an interview as it is simple enough that you could write up on a white board, but complex enough that it might trip someone up if they don't think carefully enough about it. Also, you can also for two different answers which cause the implementation to be quite different.
Order Matters
If the order matters then any solution needs to allow for zero to appear for any of the variables; thus, the most straight forward solution would be as follows:
public class Combos {
public static void main() {
long counter = 0;
for (int a = 0; a <= 500; a++) {
for (int b = 0; b <= (500 - a); b++) {
for (int c = 0; c <= (500 - a - b); c++) {
for (int d = 0; d <= (500 - a - b - c); d++) {
counter++;
}
}
}
}
System.out.println(counter);
}
}
Which returns 2656615626.
Order Does Not Matter
If the order does not matter then the solution is not that much harder as you just need to make sure that zero isn't possible unless sum has already been found.
public class Combos {
public static void main() {
long counter = 0;
for (int a = 1; a <= 500; a++) {
for (int b = (a != 500) ? 1 : 0; b <= (500 - a); b++) {
for (int c = (a + b != 500) ? 1 : 0; c <= (500 - a - b); c++) {
for (int d = (a + b + c != 500) ? 1 : 0; d <= (500 - a - b - c); d++) {
counter++;
}
}
}
}
System.out.println(counter);
}
}
Which returns 2573155876.
One way of looking at the problem is as follows:
First, a can be any value from 0 to 500. Then if follows that b+c+d+e = 500-a. This reduces the problem by one variable. Recurse until done.
For example, if a is 500, then b+c+d+e=0 which means that for the case of a = 500, there is only one combination of values for b,c,d and e.
If a is 300, then b+c+d+e=200, which is in fact the same problem as the original problem, just reduced by one variable.
Note: As Chris points out, this is a horrible way of actually trying to solve the problem.
link text
If they are a real numbers then infinite ... otherwise it is a bit trickier.
(OK, for any computer representation of a real number there would be a finite count ... but it would be big!)
It has general formulae, if
a + b + c + d = N
Then number of non-negative integral solution will be C(N + number_of_variable - 1, N)
#Chris Conway answer is correct. I have tested with a simple code that is suitable for smaller sums.
long counter = 0;
int sum=25;
for (int a = 0; a <= sum; a++) {
for (int b = 0; b <= sum ; b++) {
for (int c = 0; c <= sum; c++) {
for (int d = 0; d <= sum; d++) {
for (int e = 0; e <= sum; e++) {
if ((a+b+c+d+e)==sum) counter=counter+1L;
}
}
}
}
}
System.out.println("counter e "+counter);
The answer in math is 504!/(500! * 4!).
Formally, for x1+x2+...xk=n, the number of combination of nonnegative number x1,...xk is the binomial coefficient: (k-1)-combination out of a set containing (n+k-1) elements.
The intuition is to choose (k-1) points from (n+k-1) points and use the number of points between two chosen points to represent a number in x1,..xk.
Sorry about the poor math edition for my fist time answering Stack Overflow.
Just a test for code block
Just a test for code block
Just a test for code block
Including negatives? Infinite.
Including only positives? In this case they wouldn't be called "integers", but "naturals", instead. In this case... I can't really solve this, I wish I could, but my math is too rusty. There is probably some crazy integral way to solve this. I can give some pointers for the math skilled around.
being x the end result,
the range of a would be from 0 to x,
the range of b would be from 0 to (x - a),
the range of c would be from 0 to (x - a - b),
and so forth until the e.
The answer is the sum of all those possibilities.
I am trying to find some more direct formula on Google, but I am really low on my Google-Fu today...