0 like 0 dislike
3.9k views

By using two-dimensional array, write C program to display a table that represents a Pascal triangle of any size. In Pascal triangle, the first and the second rows are set to 1. Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it. See the example Pascal triangle(size=5) below:

使用二維陣列,寫一個顯示巴斯卡三角形的程式。

Example input1:

5

Example output 1:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Example input 2:

0

Example output 2:

0

 

[Exercise] Coding (C) - asked in Chapter 8: Arrays by (5.2k points)
ID: 39984 - Available when: Unlimited - Due to: Unlimited

edited by | 3.9k views

102 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>

void fun(int);
int main(void)
{
    int in;
**** * * * * * ** * * *** * * * &in);
    if(in==0)
* * * ****** * * ** * ***** ** *** * * ***** * ** * * ** ** *
    else
*** * ** * *** * *** * ** * **** *** * * *
* ** ** *** *** *** ****** * * * *** * *
    return 0;
}

void fun(int n)
{
** * * * ** * *** ** * *
    {
* ** ** *** ** * * ** *** * *** ***** *** buf[1000]= {1};
* ** * * ** *** ****** ** ** *** ** ** *** i, j;
*** * * * * ** * ** * * * * * * *** i<n; i++)
** **** * * ***** * ****** ** ** * *** ***
* * * ** ** * * * * * **** *** * * *** * ** * ** * * ** ********** j>0; j--) buf[j] = buf[j] + buf[j-1];
* * ***** *** *** ********* ** ** * ** ** * * ** *** * *** j<n-i; j++)printf(" ");
* *** * **** * ** * * * * ** ** ** ** * ***** ** ** **** * * *** j<=i; j++)
*** *** * * * * **** *** * * ** * * ** ** * ** ** *** ** ***** *
* ** ** * ** * ** *** ** *** * *** ******** ** ********** *** **** * * ** * * *** * * *** *** ** * *
* * ** ** ***** ** * * **** * * ** ** * ** ** ** ** * * **** ** * **** ******** * ** * * *** ****** * ** *** ***** ** * * * ** ** ", buf[j]);
** ** * * * * **** *** ***** * ** ******* * * * *** *** * * ** *** * * ******* * * **** *** ** * *
***** ** ** * * ** ** ******* * * *** * * * ***** * ** *** *** * ** ** * * * * * * ** * ** ** **** ** ** * * ** buf[j]);
**** **** * ** ******* ** * * ****** ** ** * *** * ** * * * * *
* * * * *** * **** *** *** ****** * * * * * * ** * ** * ****** * * **
*** ** ***** * * **** ** ** * ** * * *** * *** * * ** *** ** ** * ** * ** * **** * * ** ******** *** * * *** * ** ***
* * *** * * * * * * * *** * * ***** ***
    }
}
answered by (-284 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>

void fun(int);
int main(void)
{
    int in;
** ********* ** ** * * * *** * ** * &in);
    if(in==0)
**** * ** * *** ***** * * ** * * * * *** * *** *** ****
    else
* * ** ** ** ** * **** **** ** *** * ** * ** *
* ** **** ** * * * * **** ** * *** * **
    return 0;
}

void fun(int n)
{
** *** ** *** * ******
    {
***** * * ******* ** * * *** * ** ** ** ** ****** * ** buf[1000]= {1};
* **** *** ** * * *** ** ** * ** * * * i, j;
* *** * ** * ** * **** ****** * *** **** *** * ** i<n; i++)
***** * ** * * * *** ** ***** * * * * * ** *** *
*** ***** * ** **** * ***** ** * * * * *** * * ** * * **** * * ** ** j>0; j--) buf[j] = buf[j] + buf[j-1];
* * **** * *** ** ** * ** * * * * ** ***** ** ** ***** * * ** * * j<n-i; j++)printf(" ");
**** * ** * ** ** * * ***** ** * * * ** * * * * * *** * **** j<=i; j++)
* * * ** * ** ** ***** ******** * ***** * * **** * * * *
* * *** * * * *** *** * * ** * ****** ****** *** ** ** * *** ** ** * *** ** ** * ** *
******* * * ** * * * ** * * ** **** ** * ** ** *** ** ***** * ** * ** * * *** * ** ******** * ** * * * ** **** ", buf[j]);
* * ******* *** * ** * * ** * ** ********* * * **** * * ***** ** *** *** * *** * *** ***** *
* * * *** * * * ** ** ** *** * ** ** *** * ** **** * * *** * ** ** * * * ** **** * ** * *** ****** *** * * * * * * buf[j]);
*** * * * * * * ** ** ***** ** * ***** * * * * ******* * ** * * *
* * * ** * * ** * ** * **** ** * *** * ** * * *** ** *** ** ** **
* * ******** ** * * ** * **** **** ************** ****** **** * * ** * * * * ** * * * *** ** ** **** *
** ** * ******* * * * **** * **** **** **
    }
}
answered by (-108 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include * ** * **** **

int main()
{
* * ** int n, k;
* * * * * *** * *** ***** **** * * ** *
* * * * * * * * * ** **
* ** * * * *** i,j ;
* * * * * ** ** if(n == 0)
* ** * ** * {
** * ** * *** ** ** * **** ** * *
* * * * * * }
* ** ** * ** * else
* ** *** ** {
* **** * *** *** * * **** ** ***** * * * * **
** ** * * * ** * {
** ** **** ******** **** * *** * *** ** *** ** * ** ** * *** = 1;
***** ** *** * *** ** * ** * **** ***** * ** *** * * = 1;
**** ** * * ** * * * * * * }
** ***
* * ** * **** ** **** ** ** * **** *** * *
* ****** * ** * *** * ** {
*** *** * *** * ** * *** ** ** * * * * * ******* ***
* **** * * * *       {
******               * ** * *** = ***** ** * + * **** *
**           }
* * ***** *** * * * *** ** * * *   }
* * ** * * * ** * * ** for(i = 0; i < n; i++)
***** *** ** {
**** * * *** * * **** * for(j = i + 1; j < n; j++)
*** ** ** *** ** * * * * * **** {
* * * * *** * * * * ** **** ** * ** ** * * * * *
* **** ** * ** *** ** * * *** }
* * ** * ** * * * * *** * * *** for(j = 0; j < i + 1; j++)
* * * * * ** * ** * * * **** ** {
* ** * ** *** * * * * * * * **** * *** if(j == i)
** ** * ** * ** * ** * * * *** * * * * {
** * *** * * * ** ** **** ** ** * * * *** ** ** * ** * **** * * ** * * * **
** **** * ** ** * * * * ** * * * * *** }
* * * * ***** * ** * * ** * * ****** * **** else
* ** ** * * * * * * * * * **** {
* * ** * ****** *** ** ** ***** *** *** **** * ** ** * * * * * * *** ***
** * ** ** * ** * * ********   **   }
* * *       ** ** }
* *** ** ** ** * **** * if(i != n - 1)
** * * * * ** * * ** * * * ** * ** ** **** * *** ** *** **
* **** * * * * *** * ** ** * } * * * *
* ** * ** }
** *** * *** ** * ** * 0 ;
}
answered by (-285 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include * **** * ***

int main()
{
*** * *** int n, k;
* ** ** ** * * ** * ** ** ** ******* *
*** * ** *** * ** * ** **** * *
* * ** **** *** ** * i,j ;
* *** * * ** if(n == 0)
****** * * {
** * * * * * * **** * * * * *** * **
*** * ** ** }
* * * * * else
* * * ** ** * ** {
******* ******* ** **** ** ** ** * * * ** ** * *
* ** * * * * ** ** {
* * *** **** * *** *** * * *** * * ** *** * * * = 1;
* * * * ** * *** * * ** ** ** * *** ** * * ** * * ** * * = 1;
* *** ** **** ***** * ******* }
** **
* **** * * * *** **** * * ** * * *** **** * * *
* * ** ** * * {
* * * **** * * ** ** * *** ******
* * **       {
*               * * ** * = ** * + ** * * *
* *           }
** * ****** **** ** *** * *   }
* * ** *** ** ** * * * for(i = 0; i < n ; i++)
** * *** ***** *** ****** {
* * * ** * * ** * ** ****** * ** for(j = i; j < n; j++)
* ** ** *** *** * * {
* * * * * ** * ** * * * * * * * * * * **** * *** *
*** * * ** * * ** * ** * * }
* * * *** ** *** ** * ** * ** * for(j = 0; j < i + 1; j++)
* * * *** * * ** * * * * *** * {
* **** ** * * * ** * * * ** **** * ** if(j == i)
** * * * ** * * ***** * * * ***** * ** {
* * * ***** * ** * **** * ** * * **** ** ** **** * ** * * * * * * ****
** ** * * * **** ** *** *** * * * *** * * * }
* **   * * ** * ** ** ** * **** * * * else
** ** ** * * * *** * * * ** * * * * ** {
* ***** * * ** * * * * ** * ** **** * * ** ***** ****** * ** * ** ** * * ****
* *** * **** **** * ** ** ** ** * * * *   }
* * ***       ** * ** ** }
*** * * * * * * *** * ** * if(i != n - 1)
*** * ** **** ** * ** ** * **** *** ** * ** * ** ** * * * *
*** ** * * ** * * ** } ** * *** ** *
*** * * * }
* * * *** ** ** 0 ;
}
answered by (-285 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
* ** ** * *
int * ***
{
***** ** int n;
* *** ** * * ** * ** * * ***** **
* * * * * * int * * **
* *** * * *
***** ** if(n==0)
** * * * ** ** ***** *** * ***
* ** ** ** ***
** ** * ** int i,s;
* ****** ** int j;
* * * ** * * * * *
** **** * * {
***** * * ***** * * * * ** ** **** * * * *
** * * * ****** ** * ** * ******* *
* ** * ** ** ***
* **       * * * ** * *
* *       {
** **   ** **   * if(j==0 || j==i)
** * * *** ***   **** **   a[i][j]=1;
* * * ** * * * * *** ** *** else
* *** *** ** * *** ****** *** ** * ** *** * * ****
** ** * *** ** * * * * ** if(j==0)
** * * * * **** * *** * * * * ** * ** * *** * **
** * ** * * *** ** * * ** * * ** else
* *** *** * * ** * * * * * ** * * * * * * * * *
* ** ** *** * * *** * * * }
* ** * ***** ** * ** * ** ** **
**** * * * *** * * ** * *
** *** ** * * }
* * ** ** ** return 0;
* ** * **
*** * * ** *
}
answered by (-249 points)
edited by
1 0
prog.c: In function 'main':
prog.c:6:1: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
 scanf("%d",&x);
 ^~~~~
prog.c:6:1: warning: incompatible implicit declaration of built-in function 'scanf'
prog.c:6:1: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c:11:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
     printf("0");
     ^~~~~~
prog.c:11:5: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:11:5: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:17:9: warning: incompatible implicit declaration of built-in function 'printf'
         printf(" ");
         ^~~~~~
prog.c:17:9: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:27:13: warning: incompatible implicit declaration of built-in function 'printf'
             printf("%d",a[i][j]);
             ^~~~~~
prog.c:27:13: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:29:13: warning: incompatible implicit declaration of built-in function 'printf'
             printf(" %d",a[i][j]);
             ^~~~~~
prog.c:29:13: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:33:9: warning: incompatible implicit declaration of built-in function 'printf'
         printf("\n");
         ^~~~~~
prog.c:33:9: note: include '<stdio.h>' or provide a declaration of 'printf'
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Case 3: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>

void fun(int);
int main(void)
{
* ******* * ** * * * in;
* * ** * ** * * ****** * * * *** ****** &in);
*** ** ** ** ****
**** ** ** ** * ** * *** * ** * * * * * ** *** ** ** ** * * **
**** *** * *** ******** * *
* **** *** *** * ** *   fun(in);
** **** ***** * ** ** * *****  
* * * *** * * * *** * 0;
}

void fun(int n)
{
*** * * ** * * * **
    {
** * * **** * ** ** ** **** * * * ** buf[1000]= {1};
* * *** * * * **** * *** * * * * ** * ***** ** * i, j;
**** * **** * * * ** * * *** ****** * * ** ** * i<n; i++)
****** ** * * * * *** **** ** **
*** * ******** * ** * * **** * *** * ** * * *** * ****** * ****** ** * j>0; j--) buf[j] = buf[j] + buf[j-1];
** * ** * * * * * ** * **** ** ** * **** * * * ******* * * ** j<n-i; j++)printf(" ");
* * **** *** ** ** *** **** * * ** * *** ** ** * * * ** * ** * ** j<=i; j++)
***** * ** *** * ** ***** ** ** ** ********** * * *
***** *** ***** * **** * ** **** * * *** ***** ** * * * *** *** * * ** ** ** *** * **
* * * * ** * *** * * *** * * ** ** * * ** ** **** * *** *** * ** ** * * * ****** *   printf("%d ", buf[j]);
* ***** * *** * * ***** * ** * *** *** * ** * *** * * ** * * * ** ******** *** * * **
* ***** *********** *** * * ******* ** *** * ** ** * * * ** ** * *** * * **** ** * *** **   printf("%d", buf[j]);
* * ****** * ** *** * ** * *** **** *** * ** * * * * *
** ** ** **** *** * **** ** *** * ** * * ** * *** *** * ** ** * * **
* *** * * **** ** * ** * * * ** * * * * ** ** **** **** *** ***       printf("\n");
* * * * ** **** ** ** **** * ** * * ** *
    }
}
answered by (-258 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void fun(int);
int main(void)
{
    int in;
* * ***** ** *** *** ** * * * ** &in);
* ** * * * ****** *** **** ***
***** * ** * * ** * ** ** *** ** *** ** * ** * * * * *
    else
** *** * ** * **** * **
    return 0;
}
void fun(int n)
{
** ** * * ** *** * ***** ** * *
    {
* * * * ***** * * * **** *** ** ** * * * * * buf[1000]= {1};
* * * *** ** * ** ****** * ** * * * ** i, j;
*** ** * ** ** *** ** ** *** ** ** **** * * *** * i<n; i++)
** * * **** * ***** * * **** * ** ** ** *
*** *** ** * **** * ***** ** * ***** ** ** * * ** * * * ****** *** * j>0; j--) buf[j] = buf[j] + buf[j-1];
** * ** * * * * *** ** ** ** ** * * *** *** * * * *** *** j<n-i; j++)printf(" ");
** *** *** * ** * ****** ** * * ** **** ** * * **** **** * * **** j<=i; j++)
*** * * * * *** * ***** * * * * **** ****** * * ** ** * * *** ****
***** * * * ** ** *** ** * *** ** * * * ***** **** * *** * ***** * ** ** ** * * * ** ** * *** **
* * * * ** * * * * ** * ** * * * ** * * * * * * ** * **** ***** ** * ******** ***** * ** ** ", buf[j]);
* ** ** **** * ** * * ** ***** * * * * * * * * **** ****** *** * * *********** * ** * ** ** **
* *** *** * * **** ** *** * ** **** ***** * *** * * *** ** * * * * ** ********* * * ** buf[j]);
** ** ** * ***** * * * ***** *** *** **** ** ** *** ** ***** **
* ** *** * ** ** * * * * ** * * ** * * ** ** * * * * ** * **
*** *** * * * * *** * ** * **** * **** ** **** ** * ** * ** ****** * ** * * * *
***** * * ** * ** ** *** *** ** **
    }
}
answered by (-258 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>

void fun(int);
int main(void)
{
    int in;
* **** * * * ***** * ** * * ** &in);
***** *** * * * ** * *
*** ** * * *** * * ** ** *** ** *** * * * * * * ** ** *
    else
** ** * * * **** * **   fun(in);
* ****** * ** * ** **** ** **  
    return 0;
}

void fun(int n)
{
* ** *** * **** *** ** ** * ***
    {
* **** * ** * * * * * * ***** * * * * *** * ** **** buf[1000]= {1};
* * *** * ** ** ** ** *** ** i, j;
* * * * * ***** * ** * ** * ******* ** * * ** ** * ** i<n; i++)
* ****** * ** * ** ** * * **** ** ** *** * *
* ** ** ** ** * **** **** *** **** *** ** * * * ****** * ** j>0; j--) buf[j] = buf[j] + buf[j-1];
* * ** *** * * * **** ** * * *** *** ** ** ** * * * ** * ****** j<n-i; j++)printf(" ");
**** ***** * * * ** ** ** * ** * * * * * ** * * * ** * ******* * * * j<=i; j++)
*** * * ** * **** * * ** * * * ****** * * ** * *
*** * * ** * * ** **** *** *** ** ** * * * * *** * *** ** **** ********* ** ** * ** ** ***
*** *** * * * **** * ** ** * ** **** *** *** *** * ** ** * *** * ** **** **** * * ** **** ", buf[j]);
* * * ** ** * ** * ** * ** * * **** * **** ** **** * ** * * * *** * ** * * *** *
* * *** * ** * * * ***** * *** * *** * * * ** * * ** * * * * ** ** * * *** ** * * * * *** *** * buf[j]);
* * * * * * * *** *** * ** *** * * ** *** * * * * ** **** *
* ** ****** * * **** ** ** ** *** ** ** * ***** ** ** *
** ** *** ** ******* * * ** * * *** **** * ** * * * ** * ** * * *******
** * ** *** ** * ** *** * *** *
    }
}
answered by (1.1k points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void fun(int);
int main(void)
{
    int in=0;
* ** ** * * * ***** * * * * * * ** * * &in);
* * * * * ** * * * * ** * ** *
** *** **** **** * * * **** * * * *** * * * * * * *
    else
** * *** ** *** ** ** * ***
    return 0;
}
void fun(int n)
{
* ** ****** * * *** ** ** ** * * * *
    {
* ** ** ** * ****** * * ** * * * buf[1000]= {1};
* * **** ** ** *** * * **** ** * **** * *** * i, j;
** * ******** ** ** ** *** ** *** * * *** ** * * * ** i<n; i++)
* **** ********* ***** ****** *** ** * ** **
** **** * ** * * ***** ** * ***** * * * * **** ** * ** ** j>0; j--) buf[j] = buf[j] + buf[j-1];
*** ** ** * ** * *** * *** **** * * ** *** * * * *** ****** **** * j<n-i; j++)printf(" ");
* * *** ** *** * * ** *** * ** ** * *** **** ******** *** * ** * ** ** * j<=i; j++)
* * * * ** ** * * * * * ** * * * * * * ** * * ****
** ** ** ***** ** ** * * *** * * **** * ** * ** * * ** * **** * * * * ** *
* * *** * * ** * * * ** * * * * **** * * * * ** * * *** ** * ****** ** ** * * * ****** ** * * ******* ", buf[j]);
* * * * ** * *** ***** * * ** ** *** * ***** ** * ** *** * * ** * * * * * ****
** *** ***** * ** * ****** * * * *** *** ** ** * * ******** * * * ***** * * ** * ***** * **** buf[j]);
* ********** * ****** ** * * ** * * * * * * * * *** **
* * *** * ***** ** ***** ** ******* *** * * **** * * ** * * *** ** * *
* * * * * * * *** * *** * * ** ** * ***** * ** ** ** * * * * ***** * **
** * * * * ** *** *** *** ** *** * ** *
    }
}
answered by (-258 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
void fun(int);
int main(void)
{
    int in;
* ** ** ** ** *** * ** ** ** **** ****** &in);
    fun(in);
    return 0;
}
void fun(int n)
{
* ** ** **** ** *
    {
** ** * ** * **** **** ** * * * *** buf[1000]= {1};
* * **** * *** * * * * * **** * * ********* * * * i, j;
* ** ** * ** ** ** ** *** * ** ** * * **** * * i<n; i++)
**** *** * * *** *** **** * * * **** * *******
* * * * ***** * ** * * * * * * ** * * ** ****** * * *** j>0; j--) buf[j] = buf[j] + buf[j-1];
* * ***** **** * ** ** ** * * * * **** * ** *** * *** *** * ** ** *** j<n-i; j++)printf(" ");
* * * *** * ** ** ** * * * * **** ** **** * * * **** * * ** * * * * j<=i; j++)
****** ** ******* * * ** * ** * * ** * * ***** ** *** **** **
* *********** * *** ** * ****** * ** * *** ** * * ** ** * * * * * * *** * * *** * ** * ** * *
* * ** *** ** ** * * *** *** * * * **** ** ** * **** * ****** **** * * **** * ** * * * ** * * ", buf[j]);
************ ** * ** * ******* * ** * * ******* *** * * * ** **** ** * **** * ***
* ** *** *** * ** **** * ***** *** * ** * ** * *** * * * *** *** * *** ** * ** * * ** * ** ** * * *** buf[j]);
* ** * * *** ** ** *** **** * ** * ***** ** * * * *** ******** *
** **** * *** * ** * * * ** * * * * ** ** ** * *** * ** *
* * * ** ** ** * **** * * * ** * * ***** *** * ** * * ****** ** * ** * ** * * ** **
*** ** * * ** ***** * * ****** ** **
    }
}
answered by (-258 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.100.160
©2016-2024

Related questions

0 like 0 dislike
31 answers
[Exercise] Coding (C) - asked Dec 28, 2017 in Chapter 8: Arrays by semicolon (5.2k points)
ID: 39985 - Available when: Unlimited - Due to: Unlimited
| 1.7k views
0 like 0 dislike
55 answers
[Exercise] Coding (C) - asked Nov 29, 2017 in Chapter 8: Arrays by semicolon (5.2k points)
ID: 34968 - Available when: 2017-11-30 18:00 - Due to: Unlimited
| 2.3k views
12,783 questions
183,443 answers
172,219 comments
4,824 users