C: Two-dimensional arrays - Applications resolved


1) It will develop a two-dimensional arrays with integer components by initializing when declared.
#include<stdio.h>
void main()
{
	int a[][3]={
			{2,-9,0},
			{7,11,-8},
			{2,2,-20},
			{12,7,90}
			}; 
	//Display operation declared the array with 4 rows and 3 columns	
        for(int i=0; i<4; i++)
	{
		for(int j=0; j<3; j++)
			printf("%5d", a[i][j]);
		printf("\n");
	}
 
	getchar();
	int k;
	scanf("%d", k);
}
2) Will write a routine that will read a two-dimensional arrays with integer components, after which it will display.
#include<stdio.h>
void main()
{ 
	int a[10][10];	/* bidimensional array with a no. maximum of 10 lines 
						and 10 columns can be stored*/
	int m,n;	/* real number (to read) rows respectively 
						columns of the array */
	int i,j;
	//introduction of array size
	printf("Enter number of lines: ");
	scanf("%d", &m);
	printf("Enter number of columns: ");
	scanf("%d", &n);
	//introduction of array elements
	printf("Enter array the elements: \n");
	for(i=0; i<m; i++)
		for(j=0; j<n; j++)
		{
			printf("\tElem [%d,%d]= ", i+1, j+1);
			scanf("%d", &a[i][j]);
		}
	//display matrix elements
		for(i=0; i<m; i++)
		{
			for(j=0; j<n; j++)
				printf("%5d", a[i][j]);
			printf("\n");
		}
	getchar();
	int k;
	scanf("%d", k);
}
3) Will write a routine that will read a two-dimensional arrays with integer components, after which it will display and then calculate the array elements sum.
#include<stdio.h>
void main()
{ 
	int a[10][10];	/* bidimensional array with a no. maximum of 10 lines 
						and 10 columns can be stored*/
	int m,n;       /* real number (to read) rows respectively 
						columns of the array  */
	int i,j;
	//introduction of array size
	printf("Enter the number of lines: ");
	scanf("%d", &m);
	printf("Enter the number of columns: ");
	scanf("%d", &n);
	//introduction of array elements
	printf("Enter array the elements: \n");
	for(i=0; i<m; i++)
		for(j=0; j<n; j++)
		{
			printf("\tElem [%d,%d]= ", i+1, j+1);
			scanf("%d", &a[i][j]);
		} 
	//display matrix elements
		for(i=0; i<m; i++)
		{
			for(j=0; j<n; j++)
				printf("%5d", a[i][j]);
			printf("\n");
		}
	//calculation of the matrix elements
	int s=0;
	for(i=0; i<m; i++)
		for(j=0; j<n; j++)
			s=s+a[i][j];
	printf("\nSum of the elements is: %d", s);
 
	getchar();
	int k;
	scanf("%d", k);
}
4) Make a program that loads the numbers 1 to 12 in a two-dimensional array and displays them line by line, by performing merges between its lines. It also will get first or last element from an array.
//type macros constant for the number of rows, respectively columns 
#define lin 3
#define col 4
#include<stdio.h>

//define a array with a specified number of rows and columns
typedef int array[lin][col];
 
//first attribution function values from 1 to 12 in a two-dimensional array
void attribution1(array a)
{
	int k=1,i,j;
	for(i=0;i<lin;i++)
	for(j=0;j<col;j++)
	{
		a[i][j]=k;
		k++;
	}
}
//another attribution function values from 1 to 12 in a two-dimensional array
void attribution2(array a)
{
	int i,j;
	for(i=0;i<lin;i++)
	for(j=0;j<col;j++)
	a[i][j]=(i*col)+j+1;
}
//display the first, respectively, the last element from an array
void afis_prm_ult(array a)
{
	printf("\nThe first element is: %d",a[0][0]);
	printf("\nThe last element is: %d\n",a[lin-1][col-1]);
}
//display a two-dimensional array as an array
void display(array a, char c)
{
	int i,j;
	printf("\n");
	printf("\nArray %c este:\n",c);
	for(i=0;i<lin;i++)
	{
		for(j=0;j<col;j++)
		printf("%4d",a[i][j]);
		printf("\n");
	}
}
//merging of two read lines into an array
void merge(array a)
{
	int x,y,j,aux;
	printf("\nEnter the line that needs to be merged : ");
	scanf("%d",&x);
	printf("\nEnter the line that needs to be merged : ");
	scanf("%d",&y);
 
	/* merge between the content line x to y is element
		element by covering the entire column. */
	for(j=0;j<col;j++)
	{
		aux=a[x-1][j];
		a[x-1][j]=a[y-1][j];
		a[y-1][j]=aux;
	}
}
//main function to run
void main()
{
	array a,b;
	attribution1(a);
	attribution2(b);
	afis_prm_ult(a);
	display(a,'A');
	display(b,'B');
	merge(a);
	display(a,'A');
 
	getchar();
	int k;
	scanf("%d", k);
}
5) Will write a program for achieving the main summation between elements of two-dimensional arrays.
#include<stdio.h>
//define a array with a specified number of rows and columns
typedef int arrays[10][10];
 
void read(arrays a, int *n, char c)
{
	int i,j;
	printf("Enter dates in arrays %c:\n",c);
 
	//introduction of the arrays size
	printf("\tnumber of rows (columns): ");
	scanf("%d",n); 
 
	//introduction of arrays elements
	printf("\tarrays elements:\n");
	for(i=0;i<*n;i++)
		for(j=0;j<*n;j++)
		{
			printf("\t\t%c[%d][%d]= ",c,i+1,j+1);
			scanf("%d",&a[i][j]);
		}
}
void display(arrays a, int n, char c)
{
	int i,j;
	printf("The arrays %c is:\n",c);
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			printf("%5d",a[i][j]);
			printf("\n");
	}
} 
//sum of all arrays elements
void sum1(arrays a, int n)
{
	int s=0,i,j;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			s+=a[i][j];
	printf("\nsum of all elements is: %d", s);
}
//main diagonal elements sum
void sum2(arrays a, int n)
{
	int s=0,i;
	for(i=0;i<n;i++)
		s+=a[i][i];
	printf("\n\nmain diagonal elements sum is: %d", s);
}
//sum above the main diagonal elements
void sum3(arrays a,int n)
{
	int s=0,i,j;
	for(i=0;i<n-1;i++)
		for(j=i+1;j<n;j++)
			s+=a[i][j];
	printf("\nsum elements above the main diagonal is: %d", s);
}
//sum elements under the main diagonal
void sum4(arrays a,int n)
{
	int s=0,i,j;
	for(i=1;i<n;i++)
		for(j=0;j<=i-1;j++)
			s+=a[i][j];
	printf("\nsum elements under the main diagonal is: %d", s);
}
//sum of the diagonal elements of secondary
void sum5(arrays a,int n)
{
	int s=0,i;
	for(i=n-1;i>=0;i--)
		s+=a[i][n-i-1];
	printf("\n\nsum of the diagonal elements of secondary is: %d", s);
} 
//sum elements above the diagonal secondary
void sum6(arrays a,int n)
{
	int s=0,i,j;
	for(i=0;i<n-1;i++)
		for(j=0;j<=n-2-i;j++)
			s+=a[i][j];
	printf("\nsum elements above the diagonal secondary is: %d",s);
}
//sum of diagonal elements under secondary
void sum7(arrays a,int n)
{
	int s=0,i,j;
	for(i=n-1;i>=1;i--)
		for(j=n-i;j<n;j++)
			s+=a[i][j];
	printf("\nsum of diagonal elements under the secondary is: %d",s);
}
//sum on a certain line elements
void sum8(arrays a,int n,int l)
{
	int s=0,j;
	for(j=0;j<n;j++)
		s+=a[l-1][j];
	printf("sum on a certain line elements is: %d",s);
}
void main()
{
	arrays a,b,c;
	int n,l;
	read(a,&n,'A');
	display(a,n,'A');
	sum1(a,n);
	sum2(a,n);
	sum3(a,n);
	sum4(a,n);
	sum5(a,n);
	sum6(a,n);
	sum7(a,n);
 
	printf("\n\nEnter the desired line to calculate the sum: ");
	scanf("%d",&l);
 
	sum8(a,n,l);
 
	getchar();
	int k;
	scanf("%d", k);
}
6) Write a program to achieve the main summation between elements of a two-dimensional array, so functions will provide the results not only display them inside their body.
#include<stdio.h>
//define a array with a specified number of rows and columns
typedef int arrays[10][10];
void read(arrays a, int *n, char c)
{
	int i,j;
	printf("Enter data the arrays %c:\n",c); 
	//introduction of the arrays size
	printf("\tnumber of rows (columns): ");
	scanf("%d",n); 
 	//introduction of arrays elements
	printf("\tthe arrays elements:\n");
	for(i=0;i<*n;i++)
		for(j=0;j<*n;j++)
		{
			printf("\t\t%c[%d][%d]= ",c,i+1,j+1);
			scanf("%d",&a[i][j]);
		}
} 
void display(arrays a, int n, char c)
{
	int i,j;
	printf("Arrays %c is:\n",c);
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			printf("%5d",a[i][j]);
			printf("\n");
	}
}
//sum of all arrays elements
int sum1(arrays a, int n)
{
	int s=0,i,j;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			s+=a[i][j];
	return s;
}
//sum of the elements on the main diagonal
int sum2(arrays a, int n)
{
	int s=0,i;
	for(i=0;i<n;i++)
		s+=a[i][i];
	return s;
}
//sum of the elements above the main diagonal
int sum3(arrays a,int n)
{
	int s=0,i,j;
	for(i=0;i<n-1;i++)
		for(j=i+1;j<n;j++)
			s+=a[i][j];
	return s;
}
//sum of the elements below the main diagonal
int sum4(arrays a,int n)
{
	int s=0,i,j;
	for(i=1;i<n;i++)
		for(j=0;j<=i-1;j++)
			s+=a[i][j];
	return s;
}
//sum of secondary diagonal elements
int sum5(arrays a,int n)
{
	int s=0,i;
	for(i=n-1;i>=0;i--)
		s+=a[i][n-i-1];
	return s;
}
//sum of the elements above the diagonal secondary
int sum6(arrays a,int n)
{
	int s=0,i,j;
	for(i=0;i<n-1;i++)
		for(j=0;j<=n-2-i;j++)
			s+=a[i][j];
	return s;
}
//sum of diagonal elements under secondary
int sum7(arrays a,int n)
{
	int s=0,i,j;
	for(i=n-1;i>=1;i--)
		for(j=n-i;j<n;j++)
			s+=a[i][j];
	return s;
}
//amount on a certain line elements
int sum8(arrays a,int n,int l)
{
	int s=0,j;
	for(j=0;j<n;j++)
		s+=a[l-1][j];
	return s;
}
void main()
{
arrays a,b,c;
int n,l;
read(a,&n,'A');
display(a,n,'A');
printf("\nThe sum of all elements is: %d", sum1(a,n));
printf("\nSum of the elements on the main diagonal is: %d", sum2(a,n));
printf("\nSum of the elements above the main diagonal is: %d", sum3(a,n));
printf("\nSum of the elements under the main diagonal is: %d", sum4(a,n));
printf("\nSum of secondary diagonal elements is: %d", sum5(a,n));
printf("\nSum of the elements above the diagonal secondary is: %d", sum6(a,n));
printf("\nSum of diagonal elements under the secondary is: %d", sum7(a,n));
 
printf("\n\nEnter the desired line to calculate: ");
scanf("%d",&l);
printf("\nSum of the elements in a given line is: %d", sum8(a,n,l));
	
getchar();
int k;
scanf("%d", k);
}
7) Write a routine that will read a two-dimensional arrays with integer components, after which it will display, then there will be an own function that returns the sum of secondary diagonal matrix elements.
#include<stdio.h>
//define an arrays with a specified number of rows and columns
typedef int arrays[10][10];
//sum of secondary diagonal elements
int sum(arrays a, int n)
{
	int s=0,i;
	for(i=n-1;i>=0;i--)
		s+=a[i][n-i-1];
	return s;
}
void main()
{ 
	arrays a;	/* two-dimensional matrix with a no. maximum of 10 lines 
					and 10 columns can be stored */
	int n;		/* real number (reading) of rows / columns of the the arrays */
	int i,j; 
	//introduction of the arrays size
	printf("Enter the number of lines: ");
	scanf("%d", &n); 
	//introduction of arrays elements
	printf("Enter the the array elements: \n");
	for(i=0; i<n; i++)
		for(j=0; j<n; j++)
		{
			printf("\tElem [%d,%d]= ", i+1, j+1);
			scanf("%d", &a[i][j]);
		}	
	//display arrays elements
		printf("The arrays is: \n");
		for(i=0; i<n; i++)
		{
			for(j=0; j<n; j++)
				printf("%5d", a[i][j]);
			printf("\n");
		} 
	//displaying the secondary diagonal elements sum
	printf("\nSum of secondary diagonal elements is: %d", sum(a,n)); 
	getchar();
	int k;
	scanf("%d", &k);
}
8) Sum of two arrays.
#include<stdio.h>
//define an arrays with a specified number of rows and columns
typedef int arrays[10][10];
void read(arrays x, int n) // n - no. lines / columns
{
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
		{
			printf("\tElem [%d,%d]= ", i+1, j+1);
			scanf("%d", &x[i][j]);
		}
}
//display a two-dimensional arrays
void display(arrays x, int n, char* s)
{
	printf("\nThe arrays %s is:\n",s);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			printf("\t%d",x[i][j]);
				printf("\n");
	}
}
//sum of two arrays
void sum(arrays x, arrays y, arrays z, int n) /* In this case we return through type 
		of the result matrix, for the matrix is a pointer (an address) */
{
 
	for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	z[i][j] = x[i][j] + y[i][j];
}
void main()
{
	arrays A,B,C;
	int n;
 
	printf("Enter the dimensions of arrays\n");
 
	printf("\tno. lines / columns: ");
	scanf("%d",&n);
 
	printf("Introduce the array elements of A.\n");
	read(A,n);
	display(A,n,"A");
 
	printf("Introduce the array elements of B.\n");
	read(B,n);
	display(B,n,"B");
 
	sum(A,B,C,n);
	display(C,n,"A+B");
 
getchar();
int k;
scanf("%d", &k);
}
9) Multiplication of two square arrays.
#include<stdio.h>
//define an arrays with a specified number of rows and columns
typedef int arrays[10][10];
void read(arrays x, int n) // n - no. lines / columns
{
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
		{
			printf("\tElem [%d,%d]= ", i+1, j+1);
			scanf("%d", &x[i][j]);
		}
}
//display a two-dimensional arrays
void display(arrays x, int n, char* s)
{
	printf("\nThe arrays %s is:\n",s);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
			printf("\t%d",x[i][j]);
				printf("\n");
	}
}
//multiplication of two arrays
void multiplication(arrays x, arrays y, arrays z, int n) /* In this case we return through type of the result
							 matrix, for the matrix is a pointer (an address) */
{
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
       	{
			z[i][j]=0;
				for(int k=0;k<n;k++)
        			z[i][j]+=x[i][k]*y[k][j];
      	}
}
void main()
{
	arrays A,B,C;
	int n;
 
	printf("Enter the dimensions of arrays\n");
 
	printf("\tno. lines / columns: ");
	scanf("%d",&n);
 
	printf("Introduce elements of the arrays A.\n");
	read(A,n);
	display(A,n,"A");
 
	printf("Introduce elements of the arrays B.\n");
	read(B,n);
	display(B,n,"B");
 
	multiplication(A,B,C,n);
	display(C,n,"A*B");
 
getchar();
int k;
scanf("%d", &k);
}
10) Write a program that determines the maximum and minimum elements from entire the quadratic arrays.
#include<stdio.h>
void main()
{
	int a[10][10];	/* two-dimensional arrays with a no. maximum of 10 lines 
						and 10 columns can be stored */
	int n;	/* real number (reading) of rows / columns of the arrays */
	int i,j;
 
	//introduction of the arrays size
	printf("Enter the number of lines / col: ");
	scanf("%d", &n);
 
	//introduction of arrays elements
	printf("Enter the arrays elements: \n");
	for(i=0; i<n; i++)
		for(j=0; j<n; j++)
		{
			printf("\tElem [%d,%d]= ", i+1, j+1);
			scanf("%d", &a[i][j]);
		}
	
	//display arrays elements
		for(i=0; i<n; i++)
		{
			for(j=0; j<n; j++)
				printf("%5d", a[i][j]);
			printf("\n");
		}
 
	//The maximum and minimum across the arrays
	int max=a[0][0];
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
		{
			if(a[i][j]>max)
				max=a[i][j];
		}
 
	int min=a[0][0];
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
		{
			if(a[i][j]<min)
				min=a[i][j];
		}
 
	printf("The maximum and minimum of the arrays: %d si %d",max,min);
 
	getchar();
	int k;
	scanf("%d", k);
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.