C language Print Yang Hui's triangle with one array and two dimensional array

Source: Internet
Author: User
Tags first row

C language Print Yang Hui's triangle with one array and two dimensional array

#include <stdio.h>/* using one-dimensional arrays and two-dimensional arrays to print Yang Hui's triangles
/#define N-
#define M

one_dim_array (int *a,int num) Prints
{
	int i,j,k with a one-dimensional array;
	printf ("%5d\n", a[0]); Print first row
	 of data for (i=2;i<=num;i++)
		{
			for (j=i-1;j>0;j--)
				a[j]=a[j]+a[j-1];////Every row of data from right to left

			for (k=0;k<i;k++)//print data for each line
				printf ("%5d", A[k]);
			printf ("\ n")
		;
}


int main ()
{
	int arr[n]={1,0};//Here defines a one-dimensional array with the first item 1 and the other item 0
	int a[m][m];
	int n;
	int i,j;
	printf ("Input The Lines:");
	scanf ("%d", &n);
	printf ("Using one_dim_array:\n");
	One_dim_array (arr,n);

	printf ("Using double_dim_array:\n");
	for (i=0;i<m;i++)
    	{
	    	a[i][0]=1;
     		a[i][i]=1;
    	}

	for (i=2;i<m;i++) for
		(j=1;j<i;j++)
		 a[i][j]=a[i-1][j-1]+a[i-1][j];
	for (i=0;i<m;i++)
	{
	  for (j=0;j<=i;j++)
		printf ("%5d", A[i][j));
	  printf ("\ n");
	}
	return 0;

Running results under Linux:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.