Topic name
Pascal Triangle
Title Description
By using the two-dimensional array, write C program to display a table, represents a Pascal triangle of any size. In Pascal Triangle, the first and the second rows is 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:
| 1 |
|
|
|
|
| 1 |
1 |
|
|
|
| 1 |
2 |
1 |
|
|
| 1 |
3 |
3 |
1 |
|
| 1 |
4 |
6 |
4 |
1 |
My Code
"Triangle.h" & "MAIN.C"
1#include <stdio.h>2 inta[ -][ -] = {0};3 voidPrintpascaltr (intsize) {4a[1][1] =1;5 intI, J;6 if(Size >=1) printf ("1\t\n");7 for(i =2; I <= size; i++) {8 for(j =1; J <= I; J + +) {9A[I][J] = a[i-1][J] + a[i-1][j-1];Tenprintf"%d\t", A[i][j]); One } Aprintf"\ n"); - } -}
1#include <stdio.h>2#include"triangle.h"3 voidPrintpascaltr (intsize);4 5 intMain () {6 intsize;7printf"Enter Pascal triangle Size:");8scanf"%d", &size);9 printpascaltr (size);Ten GetChar (); One return 0; A } - Standard Code
1 #ifndef Triangle_h2 #defineTriangle_h3#include <stdio.h>4 voidPrintpascaltr (intsize) {5 intPascaltr[size][size];6 intRow, col;7 //assign zero to every array element8 for(row =0; row < size; row++)9 for(col =0; Col < size; col++)TenPascaltr[row][col] =0; One //First and second rows is set to 1s Apascaltr[0][0] =1; -pascaltr[1][0] =1; -pascaltr[1][1] =1; the for(row =2; row < size; row++) { -pascaltr[row][0] =1; - for(col =1; Col <= Row; col++) { -Pascaltr[row][col] = Pascaltr[row-1][col-1] ++ Pascaltr[row-1][col]; - } + } A //Display the Pascal Triangle at for(row =0; row < size; row++) { - for(col =0; Col <= Row; col++) { -printf"%d\t", Pascaltr[row][col]); - } -printf"\ n"); - } in } - #endif/* Triangle_h */ to Problem
Mainly is the format problem, the topic also gives not too clear, at the same time oneself do not know the tab "\ T" is what meaning, the feeling Mark Chengliao complex point.
A place to learn
① tabs and spaces.
② This only hit a definition file inside is still a lot of unclear, why in MAIN.C there is already include "stdio.h" in the subroutine Inside "triangle.h" also want to play? There are also variables common to these problems, to learn.
③ nominal inside these define "", ifndef,endif is what mean, want to learn a bit.
(Eden) Pascal Triangle