-1

I am trying to create a program outputting the pascal's triangle, using C in OnlineGDB and repl.it using a 2x2 array asking how many lines the user wants to print. The main problem is that the program works, but only until printing for 7 rows in onlinegdb, and only 3 rows in repl.it.

There is no error in OnlineGDB, and repl.it says "signal: segmentation fault (core dumped)"

Additionally, I added 3 "PASS" print lines to see where the error occurs, and when reaching the 8th line in onlineGDB it passes all 3 of the for statements filling the array. When reaching the 4th line in repl.it passes all 3 of the for statements filling the array, but both of them doesn't print out the correct numbers. Again, inputted numbers below these values show that all of the code works.

Is there a fix for this, or is it an error with websites handling arrays?

#include <stdio.h>

int main(void){
    int intCount;
    int intCount1;
    int intRows;
    int intColumns;
    
    printf("HOW MANY ROWS DO YOU WANT?? ");
    scanf("%i", &intRows);
    
    intColumns = intRows;
    int intNum[intRows][intColumns];


    printf("PASS ");
    
    // FIRST FILL ARRAY WITH 0
    for(intCount = 0; intCount <= intRows+1; ++intCount){
        for(intCount1 = 0; intCount1 <= intColumns+1; ++intCount1){
            intNum[intCount][intCount1] = 0;
        }
    }
    
    printf("PASS ");
    
    
    // SET STARTING POINT (1) 
    intNum[0][0] = 1;
    

    
    // NOW FILL ARRAY WITH PASCAL TRIANGLE
    for(intCount = 0; intCount <= intRows; ++intCount){
        for(intCount1 = 0; intCount1 <= intColumns; ++intCount1){
            intNum[intCount+1][intCount1+1] = ((intNum[intCount][intCount1+1])+ (intNum[intCount][intCount1]));
        }
    }


    printf("PASS\n");
    

    // NOW PRINT ARRAY
    for(intCount = 0; intCount <= intRows; ++intCount){
        for(intCount1 = 0; intCount1 <= intColumns; ++intCount1){
            
            // WITHOUT ZEROES:
            
            /*if(intNum[intCount][intCount1] != 0){
                printf("%5i",intNum[intCount][intCount1]);
            }*/
            
            // WITH ZEROES:
            printf("%4i",intNum[intCount][intCount1]);
        }
        printf("\n");
    }
    return 0;
}

Logic: In the code above I created a 2 x 2 array with height and width dimensions one larger than the user asks for. I then fill the array with zeroes, and start with a 1 in the top left corner. From there I can use the pascals triangle formula by adding the two numbers above it

I tried changing the counting variables of arrays to make sure everything was correct, but it did not help. I originally coded on onlineGDB but used repl.it to see if there was any further errors, to which there was none. Additionally checked other questions on stack.

Desired Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

OnlineGDB Output: (limited to 7 rows in the input)

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

repl.it Output: (limited to 3 rows in the input)

1
1 1
1 2 1

Note: In the output, my code also prints the 0s at the moment and the whole array so that I can visualize it. I am also 100% sure it's the same code uploaded to both

This could be something simple, but I appreciate all the help I can get. I'm more curious why the outputs are different on separate websites with the same code.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Lucas
  • 1
  • 3
    `intCount <= intRows` will cause an off by one error and access the array out of bounds on the last iteration. Valid indices are from 0 to size-1. The `+1` in these `intNum[intCount+1][intCount1+1]` just makes it worse. Tools like AddressSanitizer help to find these issues. https://godbolt.org/z/KE1zGaWzj – Retired Ninja Feb 19 '23 at 03:21
  • When debugging in particular, it is crucial on Unix-like systems to ensure that debugging `printf()` statements end the format string with a newline. Otherwise, the output may be delayed until long after the `printf()` statement, which misleads you about where the problem occurs. – Jonathan Leffler Feb 19 '23 at 04:47
  • At this stage in your career, the chances of you finding a bug in a respectable compilation system are negligible. Assume the problem is yours until you've removed every possibility of it not being so. Removing every possibility of it not being a problem in your code is a time-consuming process — bugs in modern compilers are usually very subtle and hard to diagnose, as well as being very rare. – Jonathan Leffler Feb 19 '23 at 04:49
  • What value are you typing to the `scanf()`? Have you checked that the `scanf()` succeeded? Have you printed out the value read? Do you know what `%i` reads if you type `08` or `09`? Do you know why it returns that? Are you sure you wanted to use `%i` and not `%d`? – Jonathan Leffler Feb 19 '23 at 04:51

1 Answers1

0

Why are you using loops like

for (intCount = 0; intCount <= intRows+1; ++intCount)
{
    for (intCount1 = 0; intCount1 <= intColumns+1; ++intCount1)
    {
        intNum[intCount][intCount1] = 0;
    }
}

when you allocated intNum[intRows][intColumns]? You are trampling way out of bounds. That's why your code crashes. That's why you get different behaviours in different systems.

Use:

for (int i = 0; i < intRows; i++)
{
    for (int j = 0; j < int columns; j++)
        intNum[i][j] = 0;
}

or an equivalent. Note that you use < and not <=; you use the declared limit, not that limit plus one.

Here is some working code, printing without the zeros. Your algorithm for generating the values in Pascal's Triangle was flawed on at least two counts. As before, it trampled way out of bounds of the array, and it also produced two rows with a single 1 in the output (when zeros were not printed). This code avoids those flaws. It also uses i and j as the loop counters — old Fortran programmers die hard.

/* SO 7549-7765 */
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int intRows;
    int intColumns;

    printf("How many rows do you want? ");
    if (scanf("%i", &intRows) != 1)
    {
        fprintf(stderr, "failed to read an integer\n");
        exit(1);
    }
    if (intRows < 1 || intRows > 64)
    {
        fprintf(stderr, "value %d is outside the range 1..64\n", intRows);
        exit(1);
    }
    printf("Rows: %d\n", intRows);

    intColumns = intRows;
    int intNum[intRows][intColumns];

    printf("PASS\n");

    // First, fill array with zeros
    for (int i = 0; i < intRows; i++)
    {
        for (int j = 0; j < intColumns; j++)
        {
            intNum[i][j] = 0;
        }
    }

    printf("PASS\n");

    // Set starting point (1)
    intNum[0][0] = 1;

    // Now fill array with Pascal's Triangle
    for (int i = 1; i < intRows; i++)
    {
        intNum[i][0] = intNum[i-1][0];
        for (int j = 1; j <= i; j++)
        {
            intNum[i][j] = intNum[i-1][j-1] + intNum[i-1][j];
        }
    }

    printf("PASS\n");

    // Now print array
    for (int i = 0; i < intRows; i++)
    {
        for (int j = 0; j < intColumns; j++)
        {
            // Without zeros:
            if (intNum[i][j] != 0)
                printf(" %5d", intNum[i][j]);
            // With zeros:
            // printf(" %5d", intNum[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Note the use of " %5d" in the printing format. That space ensures that the numbers remain separate even if there are 6 or more digits in the values (which first happens with 21 rows requested).

Sample output:

How many rows do you want? 15
Rows: 15
PASS
PASS
PASS
     1
     1     1
     1     2     1
     1     3     3     1
     1     4     6     4     1
     1     5    10    10     5     1
     1     6    15    20    15     6     1
     1     7    21    35    35    21     7     1
     1     8    28    56    70    56    28     8     1
     1     9    36    84   126   126    84    36     9     1
     1    10    45   120   210   252   210   120    45    10     1
     1    11    55   165   330   462   462   330   165    55    11     1
     1    12    66   220   495   792   924   792   495   220    66    12     1
     1    13    78   286   715  1287  1716  1716  1287   715   286    78    13     1
     1    14    91   364  1001  2002  3003  3432  3003  2002  1001   364    91    14     1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278