C language Yang Hui triangle - completed independently

Posted by ldoozer on Thu, 10 Feb 2022 10:22:41 +0100

Author:beiyanyunyi
A confident man majoring in software engineering
Not so many people are diligent, but you don't act and fear others.
CSDN:weixin_62688213

preface

We transform Yang Hui's triangle into an image similar to the mathematical coordinate system. In this way, you can take the array to save the data.

1, Initialize

1. Header file representation

#define ROW 200
#define COL 200

2. Define a two-dimensional array

    int a[ROW][COL] ={0};

2, Define the number of rows required

    int lands=0;
	printf("Print lines:>");
	scanf("%d",&lands);
	//The following print array needs to start with a new line
	printf("\n");

3, Design function

1. Define the last and first assignment 1 of each column

Main function

	Start_End(a,lands);

function

thinking
The coordinate value of x=1.
The coordinates of y=x are all 1.
This is a cleverly defined array coordinate, which is the same as our high number.
--------------->x
|1(1,1)
|1(2,1)1(2,2)
|1(3,1)2(3,2)1(3,3)
|
y

void Start_End(int a[ROW][COL],int lands)
{
	int y,x; 
	for(y=1;y<=lands;y++)
	{
		for(x=1;x<=y;x++)
		{
			if( x==1 || y==x)
			{
				a[y][x]=1;
			} 
			
		}
	}
}

2. Intermediate assignment

Main function

	Midlle(a,lands);

function

The second and first rows have been defined in the step of "defining the last and first assignment 1 of each column".

We can add the first element and the second element in the second row to the second element in the third row. In the third row, we only need to add it once. Then in the fourth row, two numbers are unknown, and in the fifth row, three numbers are unknown. We only need the condition of x < Y-1 to know the number of unknowns in each line.
for example
lands=3
y=3,y<=3
If x=1 and x < 3-1 (x < 2), the third line can only be solved once.

The unknown coordinate value calculation method, his y coordinate minus 1, assigns the y value of the previous line. What about the X coordinate?? Each unknown number starts from x=2, so one X in the previous line remains unchanged and the other x-1 must be required.

void Midlle(int a[ROW][COL],int lands)
{
	int y,x; 
	for(y=3;y<=lands;y++)
	{
		for(x=1;x<y-1;x++)
		{
			a[y][x+1]=a[y-1][x]+a[y-1][x+1];
		}
	}
}

3. Print the array to get Yang Hui triangle

Main function

	print(a,lands) ;

function

void print(int a[ROW][COL],int lands)
{
	int y,x; 
	for(y=1;y<=lands;y++)
	{
		for(x=1;x<=y;x++)
		{
			printf("%4d ",a[y][x]);
			
		}
		printf("\n");
	}
}

summary

Not a triangle! How??

Praise first and then watch, form a habit!!! ^^ ❤️ ❤️ ❤️
code

#include<stdio.h>
#define ROW 200
#define COL 200

void Start_End(int a[ROW][COL],int lands);
void Midlle(int a[ROW][COL],int lands);
void print(int a[ROW][COL],int lands);

int main()
{
	//initialization 
	int a[ROW][COL] ={0};
	//How many rows are set
	int lands=0;
	printf("Print lines:>");
	scanf("%d",&lands);
	
	printf("\n");
	
	//Define the last and first assignment 1 of each column 
	Start_End(a,lands);
	
	//Intermediate assignment
	Midlle(a,lands);
	
	//Print array
	print(a,lands) ;
	
	return 0;
}
void Start_End(int a[ROW][COL],int lands)
{
	int y,x; 
	for(y=1;y<=lands;y++)
	{
		for(x=1;x<=y;x++)
		{
			if( x==1 || y==x)
			{
				a[y][x]=1;
			} 
			
		}
	}
}
void Midlle(int a[ROW][COL],int lands)
{
	int y,x; 
	for(y=3;y<=lands;y++)
	{
		for(x=1;x<y-1;x++)
		{
			a[y][x+1]=a[y-1][x]+a[y-1][x+1];
			
		}
	}
}
void print(int a[ROW][COL],int lands)
{
	int y,x; 
	for(y=1;y<=lands;y++)
	{
		for(x=1;x<=y;x++)
		{
			printf("%4d ",a[y][x]);
			
		}
		printf("\n");
	}
}

It's not easy to code. Everyone's persistence is the driving force for me to insist. After you like it, don't forget to pay attention to me!

If there is any mistake, please criticize and correct it^

Topics: C