Home > Enterprise >  For C: Why does my Pascals Triangle program print infinite results rather then the user input asked
For C: Why does my Pascals Triangle program print infinite results rather then the user input asked

Time:05-01

//Im trying to ask for the user input and I am unable to get it to work. I know there is a way to do this without functions, but I want to have my main function separate from the algorithm. I want the output to display the correct level inputted.

#include <stdio.h>
int main () {
    int levels, i, j, result;
    printf("Please enter how many levels of Pascal's Triangle you would like to see:");
    scanf("%d",&levels);
    newfunc();
}
int newfunc() {
    int levels, i, j, result;
    int num[28];
    for(i=0; i < levels; i  ) {
        num[i] = 1;
        for (j = i - 1; j > 0; j--) {
            num[j]  = num[j - 1];
            }
            result = (levels - i);
            for (j = 0; j <= i; j  ) {
                printf("%d ", num[j]);
            
            
            }
            printf("\n");
    }
} 

CodePudding user response:

Not sure about infinite. In newfunc():

  1. levels is not initialized so it could be very large number.
  2. If level >= 28 then num will overflow and anything could happen as you overwrite memory (most likely a segmentation fault).
  3. result is not used.
  4. The function doesn't return anything.
  5. Not a bug but as negative values don't make sense for your problem switch from int to unsigned. Note comment for 2nd loop.
#include <stdio.h>

void newfunc(unsigned levels) {
    // protect against vla overflow
    if(levels > 1000) return;
    unsigned num[levels];
    for(unsigned i = 0; i < levels; i  ) {
        num[i] = 1;
        // when i = 0 it causes j to underflow so add i > 0
        for (unsigned j = i - 1; i > 0 && j > 0; j--) {
            num[j]  = num[j - 1];
        }
        for (unsigned j = 0; j <= i; j  ) {
            printf("%u ", num[j]);
        }
        printf("\n");
    }
}

int main () {
    unsigned levels;
    printf("Please enter how many levels of Pascal's Triangle you would like to see:");
    scanf("%u", &levels);
    newfunc(levels);
}

Example session:

Please enter how many levels of Pascal's Triangle you would like to see:7
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 

When you look at the output you may notice that it's left/right symmetric, so you could change the algorithm to only calculate i/2 1 of num and then tweak the print loop only use the "left" part of num when needing to the right side of the triangle.

CodePudding user response:

You should declare 'int newfunc()' before 'int main()'. So try like this.

#include <stdio.h>
int newfunc();
int main () {
    int levels, i, j, result;
    printf("Please enter how many levels of Pascal's Triangle you would like to see:");
    scanf("%d",&levels);
    newfunc();
}
int newfunc() {
    int levels, i, j, result;
    int num[28];
    for(i=0; i < levels; i  ) {
    num[i] = 1;
    for (j = i - 1; j > 0; j--) {
        num[j]  = num[j - 1];
        }
        result = (levels - i);
        for (j = 0; j <= i; j  ) {
            printf("%d ", num[j]);
        
        
        }
        printf("\n");
    }
} 
  • Related