0 like 0 dislike
3.7k 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.7k views

102 Answers

0 like 0 dislike
Hidden content!
int * * **

    int in;

    ** * *

        ** ** * * me a number 1~20: **

        **** * **** * * ****

        fun(in);

    }

    return 0;

}

void fun(int n){

    ***** * ** || n <1){

        ** ** * * *** *

        return;
** **** *   }*/
****   if(n==0){
* * *** **** ** * * ** * *** * * * ** *** ** **
* * ****   }
* *   else if(n==1){
* ** **** ***** * * *** * ** ** *** *** ****** * * *****

    }

    else{
***** ** * * * * *** ** * ** * * ** * ** * * * *
*** * ** ** * **** **** *** * *** ** **** * * ** * i, j;
** * * * *** *** * ********** *** * * * *** * * ***** * **
* * ** * ** * * ** * * ** * * * * ** * ** * ** * ** ** * * **
*** * * * * ** * ** * * ** * *** ** ** * *** ** *** ***** ** * * * ** *** = buf[j] + buf[j-1];
***** *** * * ***** * *** * **** * * * ** * ** * ** *** ***
** *** * * *** * ***** * * ** ** ** * ***** ** * **** * * ** ***** ** ** ** ** ** **
*** ** *** ******** ** * * * *** ** ** ** * ******* ** ** * ** * ***** ******** ** ****** * * ** * * ** *
* ** * * ** **** * *** * * *** * * * ** *** * * ** * * *
** ** ***** * * * ***** ** *** * * * *** ** * * ** * * *** * *****
* * *** * * ** *** **** * * * * ***** ***** *** * * *** ** **** * * **** ******* ** * ", buf[j]);
* * * ** *** *** ** ** *** **** *********** * ***** ** ** **** ****** * * * *** ****** ** * * * *** * ** ** **
* * * * * * *** * * *** * ** ****** * * * *** * *** * ***** ** **
** *** * * * ** *** * * * ** ** * * * **** ** ******* ****** * ** *** * *
* * * * ** * ** ***** ** * ****** ** * ***** * ** ** * * *** * ** ** ** * ** *****
* **** * * ** * * *** * * *** ****** * * * * *** *
** ** ** **

}
answered by (-32 points)
edited by
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x;
*** ** **** * *** * *
int a[100][100],i,j,k;
if(x == 0)
{
** * * ** * * * * ** == 0;
*** *** * *** ***** ** *****
}
for(i = 0 ; i < x ; i++)
{
**** * * * * ** ** * ** = 1 ; k <= x-i-1 ; k++)
** * **** * * ** ******
* **** ** * *** ** * *** * * * *** * * * *** *** ");
** * * *** **** **** *
* * * * * ** ** ** * = 0 ; j <= i ; j++)
** ** **** * * * ***

* **** ***** * * *** ** ** ** * * * * *** *** * == 0 || j == i)
* * ** ** * **** ***** ****** * ******** *** * * *** ** * ** ** * = 1;
** ** * * **** * * ******* ** **** *** * * *
* * * ********** ***** *** ** ** ** ** * ** * ***** **** * ** * ** ** **** = a[i-1][j-1] + a[i-1][j];
* ** **** * ** ** * *** ** * * * * ** * == 0)
* * ** ** ** ****** * ** * ********** ** * * ** ** ** * * * **** ** ** * * ** ** * * **** ****** * **
*** ** ********* ** * * ****** ***** * ***
* * *** * *** * * * * ***** * * * ** ** **** ** * *** ***** * * ****** * %d",a[i][j]);
* ** * * * *** **

* * * * * ** **** != x-1)
* ** *** * **** * ** ** *** ** * * * ** * *** * * ****
}
* * **** * * ** ** * * ** 0;
}
answered by (-127 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!
* * * *
** * * *******

int main()
{
int x;
** ***** * * ** **** ***
int * * * **
if(x == 0)
{
** * * *** * ** *** ****** == 0;
* *** ** *** * **** * ** * *
}
for(i = 0 ; i < x ; i++)
{
** * ** ** * * ** * ** * = 1 ; k ** * * * x-i-1 ; k++)
* ** ** ** **** * *
**** ** * ** ** * ** ***** * ** **** ** ** ** * ** * *
* ** * * *** **** ** **
** * * * ** * **** * * = 0 ; j ** * i ; j++)
* * * * * ** **** **** *
answered by (-127 points)
0 0
prog.c: In function 'main':
prog.c:21:5: error: expected declaration or statement at end of input
     {
     ^
prog.c:21:5: error: expected declaration or statement at end of input
prog.c:21:5: error: expected declaration or statement at end of input
0 like 0 dislike
Hidden content!
#include<stdio.h>
   void main ()
{
    int noOfRows;

    //printf("Number of rowss to be printed\n");
* ** * ** ***** **** * * * *** *** *** ** * &noOfRows);

    int row,colspace,colchar;
** * ** *** * ** * ** **** ******** ** **
    {
*** * * **** ** ** **** ***** * ***** * * * * * * **
*** **** * * ** * *** **** * *** * ** ***** ** ** * **
* *** * * ** ***** * ** ***** * * * * * * * * ** ***** ** *
******* * ** * ** * * * * ** * ** * * ***
* ** * * ** * ** *** *** * ** **** * ** * ***** * ***** *** * * * * * * ");
* *** ** **** ****** * * * * * *** ***** ** *** *
** *** ** * *** ** * * * *** * ** ******** printchar=1;
** * * ** *** ****** ** * * *** * * num=row,den=1;

**** ***** ** ** **** * ** ****** ** * *** * *** * * *** *** **
******* ** * * **** ** * **** * * * **** ** ** *
* ** ** **** * * **** **** * ** ** ** *** * * *** ** * ", printchar);
** * * * **** * * * ****** ** * * *** ** * * * * ** * *** ** ** *** * * ** * ***
* * * **** * * * *** * * * * * ** * * ****** ** **** ** *** **** ** **
***** * *** ** * **** ***** * ** ** ** ** * * ***** ** ** * * *
** ** ** * *** * ** ** * * ** * * ** **** * ** * * * * ** **
** ** * * * * * * ** *** * ** * * *
** **** *** **** * * ** * * * * * ** * * * **** *
    }
}
answered by (-167 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
  void main ()
{
    int noOfRows;

** * * * * * ** ** * * * ** * * of rowss to be printed\n");
** *** * * * *** * * * ******** * * &noOfRows);

    int row,colspace,colchar;
* * * * * * ** ** * ** *** * ** * ****
    {
**** **** ** ** * * * ** *** ** *** *** * * * ***** **
** ** ****** * * ** ** * * ** * **** ****** *** * * * **** *
* ** * ** ***** * **** * * ** ** * * ** *** * * * * ***** *** ** *
** ** ** ******* **** ** * * ** * * ** *** *
** ** ** ** * * * ***** * ** * * *** ***** ** * * * * ***** * ** ** ** ");
*** ** ** * * **** *** * * ** ** * * *
* * * *** *** * *** * * ** ***** * * printchar=1;
* ***** ** *** * * ** * **** **** num=row,den=1;

* ** ** * * * * * * ** ** ** * * * ** ** ** ** *** * ** * ** * *
*** * * **** ** ** * * *** ** **** ** * * *
*** * ******* ** * * * ** ** ****** * ** * ** * ** * * * * * *** * ", printchar);
* * ** * **** * **** ** ** ** ** ***** * ** * ** * * ** ** *
** ** **** ** * *** * * * ** ** * ** ** * ** * * * * ** *** ***** * **
* * * ** * ** * * * ** ** ** **** ** * ** * * * *** * * * **
* * * ** * ***** * * * * * ** ** * * *** * * ** * *** *
*** * * ** * *** *** **** * *** ** * ** *** ** **
*** * * *** *** ** ** ** * ** * ** ** ** * * ***
    }
    return 0;
}
answered by (-167 points)
0 0
prog.c: In function 'main':
prog.c:31:12: warning: 'return' with a value, in function returning void
     return 0;
            ^
prog.c:2:8: note: declared here
   void main ()
        ^~~~
0 like 0 dislike
Hidden content!
#include<stdio.h>
 void main ()
{
    int noOfRows;

    //printf("Number of rowss to be printed\n");
* * ** * * ** * * ** *** ** * * * ** ** &noOfRows);

    int row,colspace,colchar;
********* * * ** ** * * *** *** ** ****
    {
****** *** * **** * * *** * * *** ***** * * ** *
* * *** * * * * ** ** ** * * *** *** ** ** ** * ***
** * *** ** *** * ** **** * * *** * * * ** * * * ** ** * *
**** * * *** ** ****** * * *** * *** **
* * * *** * ** * * ** * *** ******* ** * **** * **** * * * * ** * ");
***** * * ** * * ******* * * * * * ****
* ** * * * ** **** * * **** * * * **** printchar=1;
** * * * * ** * * *** * * ** * ** num=row,den=1;

* * ** * * **** **** ** ** * **** * *** *** *** ** * *** ** **** **** *
* * * ** * ** * *** * * * *
** * ***** * ** *** ** * * **** * *** * ***** * ** * ** **** ***** *** ** * * ", printchar);
** * *** * * * * * ****** * **** ** *** **** ********** * *** * ** * * *
* ** * * ***** * *** **** ** * * *** ****** * * * * ** * * * * * *
* * * * *** ***** * **** * * * *** ** * * * * * *****
*** *** * ** ** ** ***** * * * * * * ****** *** * * ** ** * ** *** * *
* ** * * ** ** ** * ****** ** ***** *** *** **
* ** * * *** * * ** ** ** * * * ** * * ********
    }
}
answered by (-167 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
 void main ()
{
    int noOfRows;

**** ** * * ** ** * ** ** ** * ** of rowss to be printed\n");
* ** ** * * * * * ** * ***** * ** * * *** * * &noOfRows);

    int row,colspace,colchar;
* * ** *** **** * ******* ** *** *** **
    {
* ** * *** ****** * ** * * **** ***** **
***** * *** * ** ** * **** ** * * ** ** * * * *
** * * ***** ** ** *** ** *** * ** * *** * * * * * * **** * ** * * ***
**** ** * * **** * ** * * ** ** ** ** **
* * * * *** *** ***** *** *** * * ** ** * * * * * ******* * **** *** * ");
* *** *** * ** * ** ** * *** * * *** ****
* * ** * ** **** ** * *** * ***** *** * * ** * * ** ** printchar=1;
* ** ** * * * *** *** * ** ** *** * * * num=row,den=1;

** * * **** * * ** * * * * *** * * ** ** * *** * * * * ** *** * **
**** * * * * ** * ***** ** * ** * *** ** * **
* * * **** * * ** ** ** ****** ** ** * * ** * *** ** ** * * * * * *** *** **** ", printchar);
* * ** * ******** * **** * * **** * ** * ** *** * ** * * * ** * ***** * *
** ** *** *** * * * *** * *** ** ** * * *** *** * * **** * ** ** * ** * * **
**** ** * ** ******* **** *** * ** ** * ** ****** * *** *
** ** *** * *** ** * * *** ** *** * ** * * * **** ** * *** *
*** * **** ** ***** * * * * ** *** * ***** * * * *
***** *** **** * * ** *** ** * * * * * ** ** ** * * ** ** ***
    }
    return 0;
}
answered by (-167 points)
0 0
prog.c: In function 'main':
prog.c:31:12: warning: 'return' with a value, in function returning void
     return 0;
            ^
prog.c:2:7: note: declared here
  void main ()
       ^~~~
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x;
* * * * *** ** **
int a[100][100],i,j,k;
if(x == 0)
{
** ***** ** * ** * * * == 0;
* * * ** * * *** * * *** ** ** **
}
for(i = 0 ; i < x ; i++)
{
* * * * ** * * * * **** = 1 ; k <= x-i-1 ; k++)
** * ** * * ** * **
* *** * * ** * * *** * * * * ** * * ** ** ");
* ******* * ** * **
**** ***** * ** * ** * *** *** = 0 ; j <= i ; j++)
** ** *** *** ** ** ** **

* * * ** * * *** ** ** ** * * * *** *** == 0 || j == i)
** ***** * *** * ** * * ** ** ** * ** ** **** ** ** **** * *** ***** = 1;
** **** * * * ** * * * *** *** *
* **** ** *** ** * *** * * * ** * * * ** * ** * *** ** *** * * * ** ** = a[i-1][j-1] + a[i-1][j];
** **** *** * ********* **** **** * ** ** ** * ** == 0)
* ** * * * * * * *** ** * ** * ** * * ** * * ** * ** ****** * ***
***** ** * **** * ** *** ** * * ** * **
* *** * ** * * * * * ** ** * *** *** ** ** *** * ** ** *** %d",a[i][j]);
*** ********** *** *

* * * * ** *** *** != x-1)
** **** * **** * * * * *** * ***** * ***** * * * *** *
}
*** *** ** ** *** ** 0;
}
answered by (-255 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 <stdio.h>
#include <stdlib.h>

int main()
{
int x;
** ** * ** * *** *
int a[x][x],i,j,k;
if(x == 0)
{
* *** ** ***** ** ****** * *** * *** *
}
for(i = 0 ; i < x ; i++)
{
***** * **** * ** *** * *** = 1 ; k <= x-i-1 ; k++)
* **** ** **** * **** * *
**** * * ** **** * * * ** ** * ** **** * * *** * * * ");
*** *** * *** ** *
**** ** **** * ** ** = 0 ; j <= i ; j++)
* * * ****** * ***

*** *** * ** * * * ** * * *** * *** * * * ** == 0 || j == i)
* * * * ** * ** ** * *** * **** * * * *** * ******** * * * = 1;
* *** ********* ** * * * * * ** ** ** *
* * * * ** ** *** ******* *** ** **** * ** *** ** ** ** ** * * = a[i-1][j-1] + a[i-1][j];
* * *** ** * ** * * ** ** * * ** * **** * == 0)
********* * *** * ********* * * ** * * ** *** *** * ******* * ****** * ** * * ** **** *
* * * ** * *** ** ** ***** * * ** * * * *
* ** *** ***** ** ** * * * * ******* * *** ** ** ** * * * * * * * %d",a[i][j]);
**** * * * * ** **** ***

** ** * ** * * ** * * ** != x-1)
* * ****** * * ** ** * ** ** * ** ** *** * * * * * **** **
}
*** * * * ** ** **** 0;
}
answered by (-255 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>
#include <stdlib.h>

int main()
{
int x;
******** * ** **
int a[x][x],i,j,k;
if(x == 0)
{
* ** ****** * ***** ** == 0;
* * * * * ** **** * ** ** * * * ** * *
}
for(i = 0 ; i < x ; i++)
{
****** * ** ** ** * = 1 ; k <= x-i-1 ; k++)
*** ** ***** *** * * * **
* *** ** * * * * * **** ** ******* *** * * *** ");
* ******* * * * * ** * **
** * **** * * * * *** * * = 0 ; j <= i ; j++)
* * ** ** * * ****

* ** ** ** * * ** *** * * * **** ** * * * * == 0 || j == i)
* * * **** *** * * *** ** **** * * ******* * * ****** * * * * = 1;
* * *** ** * * ***** ******* ** * *** ***
* ***** ** ** ** *** ** * ** ****** * ***** * ******* * *** * ** = a[i-1][j-1] + a[i-1][j];
** ** * * ** * **** * **** * *** * *** * == 0)
* ** ***** **** * * * ** * ** ** * **** *** * * *** * * ** ** * **** * *** * **** * ** * *
** ****** ** ** ** * * ** * **** * * * ** * *
** * * * ** ** * **** * * ** * ** * * * * **** * * * ** ******* %d",a[i][j]);
* * ** ** * * * *** ***

* *** * ** * ****** ** != x-1)
* * ** ** ** **** ** *** ******* * * ******* * ** ***** *** *
}
** ** * * *** *** ** ** *** ** * 0;
}
answered by (-255 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.127.116
©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.6k 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.1k views
12,783 questions
183,443 answers
172,219 comments
4,824 users