-2

I am 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");
    }
} 
  • [Similar Question/Answer](https://stackoverflow.com/a/72072788/3422102) you may find useful both for this and for printing a full balanced triangle. Also make sure you are compiling with full-warnings enabled. Add `-Wall -Wextra -pedantic -Wshadow` to your `gcc/clang` compile string. For **VS** (`cl.exe` on windows), add `/W3`. (all other compilers will have similar warning options available) **Do not** accept code until it *compiles without warning* – David C. Rankin May 01 '22 at 06:12
  • you need to pass levels to mewfunc – pm100 Jul 31 '23 at 21:59

2 Answers2

2

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.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Wow that works. Just Confused, why wouldn't I need to initialze the "i" and the "j" as well? –  May 01 '22 at 04:58
  • 1
    They are being declared and initialized as part of the loop (`for(int i=0; ...`). The general approach here is to reduce the scope of variable as much as possible. Also, note, while you use `j` for both of the two inner loops they are actually different variables. – Allan Wind May 01 '22 at 04:59
-1

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");
    }
} 
minseo
  • 1
  • 1