سؤال

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) {
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top