array program in c

/*writer array  program in c  to accept elements in two single dimension 2D array  and add them into third array */

 

# include<stdio.h>
main()
{
	int arr1[5], arr2[5], arr3[5];
	int i=0;
	printf("\n Enter elements for first array");
	for(i=0;i<=4;i++)         // First for loop to accept first array elements.
	{
		printf("\nEnter number:");
		scanf("%d",&arr1[i]);
	}
	printf("\n Enter elements for second array");
	for(i=0;i<=4;i++)    	// Second for loop to accept second array elements
	{
		printf("\nEnter number:");
		scanf("%d",&arr2[i]);
	}
	printf("\n addition stored in third array");
	for(i=0;i<=4;i++)      // Third for loop to add elements of array into third.
	{
		arr3[i]=arr1[i]+arr2[i];  // Putting added elements of arr1 and arr2 into arr3 at ith location.
		printf("\n arr3[%d]=%d ",i, arr3[i]);
	}
	getch();
}

 

write a array program in c to add and subtract two 3X3 matrices

# include<stdio.h>
main()
{
	int arr1[3][3], arr2[3][3];
	int arradd[3][3], arrsub[3][3];
	int i=0,j=0;
	clrscr();
	printf("\n Enter elements of first array\n");
	for(i=0;i<=1;i++)
	{
		printf("\n");
		for(j=0;j<=2;j++)
		{
			printf("Enter arr[%d][%d]:\t", i,j);
			scanf("%d", &arr1[i][j]); // Accepting array elements for first array arr1.
		}
	}
	printf("\n Enter elements of second array\n");
	for(i=0;i<=2;i++)
	{
		printf("\n");
		for(j=0;j<=2;j++)	 	// Accepting array elements for second array arr2.
		{
			printf("Enter arr[%d][%d]:\t", i,j);
			scanf("%d", &arr2[i][j]);
		}
	}
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{
			arradd[i][j]=arr1[i][j]+arr2[i][j];	

			arrsub[i][j]=arr1[i][j]-arr2[i][j];	
		}
	}
	printf("\n Addition of two matrices\n\n");
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{
			printf("arradd[%d[%d]=%d\t", i, j, arradd[i][j]); 	
		}
		printf("\n");
	}
	printf("\n Subtraction of two matrices\n\n");     
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{
			printf("arradd[%d[%d]=%d\t",i,j,arrsub[i][j]);   	
		}
		printf("\n");
	}
	getch();
}

 

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up