rasberyhd123
Badges: 2
Rep:
?
#1
Report Thread starter 1 week ago
#1
i wrote a code and its not outputting what i want
0
reply
rasberyhd123
Badges: 2
Rep:
?
#2
Report Thread starter 1 week ago
#2
(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
0
reply
davros
  • Study Helper
Badges: 16
Rep:
?
#3
Report 1 week ago
#3
(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.
1
reply
rasberyhd123
Badges: 2
Rep:
?
#4
Report Thread starter 1 week ago
#4
(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?
0
reply
davros
  • Study Helper
Badges: 16
Rep:
?
#5
Report 1 week ago
#5
(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.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Which of these would you use to help with making uni decisions?

Webinars (63)
12.65%
Virtual campus tours/open days (117)
23.49%
Live streaming events (45)
9.04%
Online AMAs/guest lectures (48)
9.64%
A uni comparison tool (114)
22.89%
An in-person event when available (111)
22.29%

Watched Threads

View All