Addition of each Column and row in C (using array)
Shishu Ranjan
Posted on July 22, 2023
#include <stdio.h>
int main()
{
//matrix of 2X3
int a[2][3], i, j, sum;
printf("Enter the element of first row \n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
if (i == 1)
{
printf("Enter the Elements of second row \n");
}
scanf("%d", &a[i][j]);
}
}
printf("Elements are\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
// logic for addition of rows.
for (i = 0; i < 1; i++)
{
for (j = 0, sum = 0; j < 3; j++)
{
sum = sum + a[i][j];
}
}
printf("Adddition of First Row is: %d\n", sum);
for (i = 1; i < 2; i++)
{
for (j = 0, sum = 0; j < 3; j++)
{
sum = sum + a[i][j];
}
}
printf("Adddition of second Row is: %d\n", sum);
// logic for addition of column.
for (i = 0; i < 1; i++)
{
for (j = 0, sum = 0; j < 2; j++)
{
sum = sum + a[j][i];
}
}
printf("Sum of first column is : %d\n", sum);
// logic for addition of second column
for (i = 1; i < 2; i++)
{
for (j = 0, sum = 0; j < 2; j++)
{
sum = sum + a[j][i];
}
}
printf("Sum of second column is : %d\n", sum);
// logic for addition of third column
for (i = 2; i < 3; i++)
{
for (j = 0, sum = 0; j < 2; j++)
{
sum = sum + a[j][i];
}
}
printf("Sum of third column is : %d\n", sum);
}
Here
int a[2][3], i, j, sum;
initialise the 2D array of variable name 'a' as 2 row and 3 column in the memory along with the variable i, j, sum.
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
if (i == 1)
{
printf("Enter the Elements of second row \n");
}
scanf("%d", &a[i][j]);
}
}
this block of code is used to take input in the 2D array.
printf("Elements are\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
the above block of code is used to print the input taken in the 2D array for conformation that all entered element is correctly placed as a matrix and we are good to go for further process of calculation.
// logic for addition of rows.
for (i = 0; i < 1; i++)
{
for (j = 0, sum = 0; j < 3; j++)
{
sum = sum + a[i][j];
}
}
printf("Adddition of First Row is: %d\n", sum);
for (i = 1; i < 2; i++)
{
for (j = 0, sum = 0; j < 3; j++)
{
sum = sum + a[i][j];
}
}
printf("Addition of second Row is: %d\n", sum);
the above code is the logic for addition of element of each row and hence printing the total sum after the execution of the the for loop.
// logic for addition of column.
for (i = 0; i < 1; i++)
{
for (j = 0, sum = 0; j < 2; j++)
{
sum = sum + a[j][i];
}
}
printf("Sum of first column is : %d\n", sum);
// logic for addition of second column
for (i = 1; i < 2; i++)
{
for (j = 0, sum = 0; j < 2; j++)
{
sum = sum + a[j][i];
}
}
printf("Sum of second column is : %d\n", sum);
// logic for addition of third column
for (i = 2; i < 3; i++)
{
for (j = 0, sum = 0; j < 2; j++)
{
sum = sum + a[j][i];
}
}
printf("Sum of third column is : %d\n", sum);
}
The above code prints the sum of each individual column of the matrix.
Posted on July 22, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024
November 25, 2024