Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:13:19+00:00 2026-05-27T14:13:19+00:00

I’ve been looking for some simple coding challenges recently, and discovered about Pascal’s triangle

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:13:20+00:00Added an answer on May 27, 2026 at 2:13 pm

    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) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.