The Student Room Group

code help please! (c language )

i wrote a code and its not outputting what i want
Original post by rasberyhd123
i wrote a code and its not outputting what i want

#include <stdio.h>
int triangle(int n);
int factorial(int n);
int choose(int n, int r);

int main(int argc, char **argv)
{
int row, x, y, z;

printf("Enter the number of rows:");
scanf("%d", &row);

while (row>=0 && row<=13){
for(x=0; x<row; x++){
for(y=row-x; y>0; y--){
printf(" ");
}
for(z=0;z<=x;z++){
printf("%d ", triangle(row));
}
printf("\n");
}
printf("Enter the number of rows:");
scanf("%d", &row);

}
return 0;
}

int triangle(int n){

int factorial(int n){
if (n<=1){
return 1;
}
else
return n * factorial(n-1);
}
int choose(int n, int r){
return factorial(n)/(factorial(r)*factorial(n-r));
}
return 0;
}

this is the code and Its supposed to be outputting Pascal triangle but it only outputting zero
Reply 2
Original post by rasberyhd123
#include <stdio.h>
int triangle(int n);
int factorial(int n);
int choose(int n, int r);

int main(int argc, char **argv)
{
int row, x, y, z;

printf("Enter the number of rows:");
scanf("%d", &row);

while (row>=0 && row<=13){
for(x=0; x<row; x++){
for(y=row-x; y>0; y--){
printf(" ");
}
for(z=0;z<=x;z++){
printf("%d ", triangle(row));
}
printf("\n");
}
printf("Enter the number of rows:");
scanf("%d", &row);

}
return 0;
}

int triangle(int n){

int factorial(int n){
if (n<=1){
return 1;
}
else
return n * factorial(n-1);
}
int choose(int n, int r){
return factorial(n)/(factorial(r)*factorial(n-r));
}
return 0;
}

this is the code and Its supposed to be outputting Pascal triangle but it only outputting zero

my c is rusty but as far as I can see your triangle function includes a factorial function and a choose function, but never calls the choose function - it just returns 0 all the time. Stick some debugging statements into your functions to see what is being called, when, and with what parameters.
Original post by davros
my c is rusty but as far as I can see your triangle function includes a factorial function and a choose function, but never calls the choose function - it just returns 0 all the time. Stick some debugging statements into your functions to see what is being called, when, and with what parameters.

thank you for the help but I'm kind of new to c language so I mostly understood what you said but would be too much if i asked for an example code of what you're saying?
Reply 4
Original post by rasberyhd123
thank you for the help but I'm kind of new to c language so I mostly understood what you said but would be too much if i asked for an example code of what you're saying?

Well you've defined a function called choose which takes 2 input parameters (n and r) and returns the binomial coefficient nCr (i.e. the appropriate entry in Pascal's triangle) but you never call the function and ask it to calculate anything! At some point you should have an assignment statement like result = choose(n, r) and then use the result variable somewhere else e.g. in a printf statement or in some further calculation.

Quick Reply

Latest

Trending

Trending