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

102 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>
int Pascal(int a[], int r, int c){}
int main()
{

    int n,i,j;

    scanf("%d",&n);

    if(n<=0)
    {
       printf("0");
    }
    int a[100][100]={{0},{1},{1,1}};

    for(i=3; i<n+1; i++)
    {
**** * * * ** *** * **** *** * ** * *** ** **** *** ** *** ** ****** j<i; j++)
            {
**** * * * * * *** ***** ** * * **** * * * * * * *** ** ** * * * * **** * * * * ****** * **** *** *
* * ****** * * **** *** ** * *** *** * ******* * * ** ** *** *** ** * ** * * **** ** * * **** ** *
* * ** * ** ** *** * *** * * *** * *** * * ** ** * * * ** * ** * * * **** ** ** * *** * ** *** ** ** **** *
* ** * * *** ** * *** ** **** * ** ** * **** ** * ** * * ** ** ** * *** * * * * ** * ** **
** * *** * ****** * ** ** * * **** * ******** * ** * * *** *** * * * * * * * * * * * * if (j<i-1)
** * * ** *** ***** * * *** * ** * ** * * ** **** * * * * ** **** * ** * ** * **** ****
* ** * *** * * * ******** ** **** * * * **** ** ** * ***** * **** ******* ** **** * *** * * ** * ****** *** * * * ** **** ** **** *** * ***** * * *
** * * * * * * * *** * **** * ** * **** * * * * * *** * ** * ** * * * * * * * * ****
*** * * * * *** ***** * * * * ** *** * **** **** *** * * * * * * **** ** ** * * * * ** * * * * ** *
** * * * *** ** ** ** ** * * * * * ** * * * * * * * * **** * * * ** ** * * * ********* * ** ** * * *** ** *** * * *
* * *** * ** * * * * *** **** * ***** *** * * * **** ***** ** ** ** * * * ** * *** * *** *** * * * ***** ** ** ** *** * **
*** * * * ** ** * * * *** * * *** ** *** * * *** * ** ** *** * * *** ** * * *** ** ** * * * ** ** * * * * ** ** * ** *
** * * ** ** ** ** * *** *** ** * * ** * * *** ** * ** * ** **
     }
     for(i=1; i<n+1; i++)
     {
* ** * * ** ** ** ** ** ** ** * * ****** *** * *** * **** * * ***** j>0; j--)
*** ** **** * *** * ** ** * ** ** * ** * ** * * ** * **** ** *** **
**** **** ** * *** ** ** ** * * * ** * *** * * **** **** ** ** ** ** ** ** * * ***** ** **** *** ** * *** ** * * * ");
** * * ***** * ********* **** * ** **** *** * ** * * * ** * * * *
* * * ** ****** * ** * * ** ** * * *** * ** **** * *** j=0; j<i; j++)
****** ** ** ** ***** *** * * * **** * * * *** ****** *** ** * * * *****  if(j==0)
** * * * * * * ** * ** *** * **** *** * ** * * * ** * ****** *** **** * *** ** * * * * * * ** * * ** *** * * * * * ** * **** * *
** * ** * ***** **** * * * * *** ** **** ** ** ** * *** ** * * * * ****** *
** * * *** ******** ** *** *** ** *** * ** * **** * * * ******* ******* * ** * ** * **** * ****** *** *** ** ** * %d",a[i][j]);
** ** *************** ***** ** * * * ** ** **** *** ** ***** * **** * * **
* ** * *** * * ** * ** ** ** * * * ********* * ** * ** ** ***
* * *** *** ** * * * **** * **** * * * * * * * * * *** ***** *** * * *** *
     }
   return 0;
}
answered by (-304 points)
edited by
0 0
prog.c:2:18: fatal error: conio.h: No such file or directory
 #include<conio.h>
                  ^
compilation terminated.
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
remove newline at the end, output is 0 when input is 0
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Case 3: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Case 3: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong 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: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong 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>
#include <stdlib.h>

int main()
{
int x;
** *** * * ** **** **
int a[100][100],i,j,k;
if(x == 0)
{
***** ***** * * * ** *** *** * * ** *
}
else{
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 (5.2k points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
prog.c:2:1: error: expected identifier or '(' before numeric constant
 0
 ^
In file included from /usr/include/stdio.h:74:0,
                 from prog.c:4:
/usr/include/libio.h:302:3: error: unknown type name 'size_t'
   size_t __pad5;
   ^~~~~~
/usr/include/libio.h:305:67: error: 'size_t' undeclared here (not in a function)
   char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
                                                                   ^~~~~~
/usr/include/libio.h:333:62: error: expected declaration specifiers or '...' before 'size_t'
 typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
                                                              ^~~~~~
/usr/include/libio.h:342:6: error: expected declaration specifiers or '...' before 'size_t'
      size_t __n);
      ^~~~~~
/usr/include/libio.h:464:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before '_IO_sgetn'
 extern _IO_size_t _IO_sgetn (_IO_FILE *, void *, _IO_size_t);
                   ^~~~~~~~~
In file included from prog.c:4:0:
/usr/include/stdio.h:339:20: error: expected declaration specifiers or '...' before 'size_t'
       int __modes, size_t __n) __THROW;
                    ^~~~~~
/usr/include/stdio.h:388:44: error: expected declaration specifiers or '...' before 'size_t'
 extern int snprintf (char *__restrict __s, size_t __maxlen,
                                            ^~~~~~
/usr/include/stdio.h:392:45: error: expected declaration specifiers or '...' before 'size_t'
 extern int vsnprintf (char *__restrict __s, size_t __maxlen,
                                             ^~~~~~
/usr/include/stdio.h:711:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'fread'
 extern size_t fread (void *__restrict __ptr, size_t __size,
               ^~~~~
/usr/include/stdio.h:717:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'fwrite'
 extern size_t fwrite (const void *__restrict __ptr, size_t __size,
               ^~~~~~
0 0
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: Wrong output
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: Correct output
0 0
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 0
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
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.6.236
©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
| 3.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
| 5.2k views
12,783 questions
183,443 answers
172,219 comments
4,824 users