Wednesday 29 August 2018

C Program For Printing Matrix & Sum of the Values in Each Rows Using The Concept of 2D / Two dimensional array and FOR Loop | C Program for Printing Values in Matrix Form & Sum of the Values in Each Rows By Using Two Dimensional Array and FOR Loop

C Program For Printing Matrix & Sum of the Values in Each Rows Using The Concept of 2D / Two-dimensional array and FOR Loop 

C Program for Printing Values in Matrix Form & Sum of the Values in Each Rows By Using Two Dimensional Array and FOR Loop 

2D / Two-dimensional array :- 2D array has a type such as int[][] or String[][], withtwo pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns.

An array of arrays is known as a 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. Before we discuss more about two Dimensional array lets have a look at the following C program

 The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows − Type arrayName [x][y]

Where type can be any valid C data type and arrayName will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns. A two-dimensional array a, which contains three rows and four columns can be shown as follows −

Thus, every element in the array a is identified by an element name of the form a[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.

C Program For Printing Matrix & Sum of the Values in Each Rows of that Matrix Using The Concept of 2D / Two-dimensional array and FOR Loop - 

Type 1 ->>



#include <stdio.h>

main()
{
int a,b,i,j,sum; //Some integer type variable declared
printf("\nEnter Rows and Coloumn:\n");
scanf("%d %d",&a,&b); //Enter the values for number of rows and number of columns
printf("\nRows = %d \n and \n Coloumn = %d\n",a,b); 
int k[a][b]; //2D Array declared
printf("\nEnter A Values\n");
for(i=0;i<a;i++) //For loop for Rows
{
for(j=0;j<b;j++) //For loop for column
{
scanf("%d",&k[i][j]); //Values of the matrix given here
printf("\n %6d (%d %d)\n",k[i][j],i+1,j+1); //Printing the given value with the same number like row number and column number For visualisation whole concept
}
}
printf("\nYour Matrex \n\n\n");

printf("\n                             Result\n");

for(i=0;i<a;i++) //For loop for Rows

{
sum=0; //
for(j=0;j<b;j++) //For Columns
{
printf("%6d",k[i][j]); //Printing the whole matrix
sum+=k[i][j]; //Addition of values in each row
}

printf("%15d",sum); //Printing the all or total value of addition
       }
}

Type 2 ->>

#include <stdio.h>
main()
{
int a,b,i,j,sum; 
//Some integer type variable declared
printf("\nEnter Rows and Coloumn:\n");
scanf("%d %d",&a,&b);
 //Enter the values for number of rows and number of columns
printf("\nRows = %d \n and \n Coloumn = %d\n",a,b);
int k[a][b];
 //2D Array declared
printf("\nEnter A Values\n");
for(i=0;i<a;i++)
//For loop for Rows
{
for(j=0;j<b;j++)
 //For loop for column
{
scanf("%d",&k[i][j]); 
 //Values of the matrix given here
printf("\n %6d (%d %d)\n",k[i][j],i+1,j+1); 
//Printing the given value with the same number like row number and column number For visualisation whole concept
}
}
printf("\nYour Matrex \n\n\n");
for(i=0;i<a;i++)
//For loop for Rows
{
sum=0;
for(j=0;j<b;j++)
 //For loop for column
{
printf("%6d",k[i][j]);
//Printing the whole matrix
sum+=k[i][j];
//Addition of values in each row
}
printf("  Result = %6d",sum); //Printing the all or total value of addition
printf("\n\n");
}
}