Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

26/02/2019

Find the transpose of a matrix in C-Programming:

 First of all we have to know what is the definition of the transpose of a matrix. For a matrix A, we define the transpose of A by AT interchanging the elements of corresponding row and column of A. More easily we have to swap the 1st row with the 1st column and 2nd row with the 2nd column and so on. So here we find the transpose of a matrix in C-Programming.
Find the transpose of a matrix in C-Programming
Transpose of Matrix

Important:

1. for-loop
2. use of array

You can also see:

Representation of a matrix 
Sum of two matrix


Code:

Find the transpose of a matrix in C-Programming
Find the transpose of a matrix in C-Programming

#include<stdio.h>
int main()
{
  int n,m,i,j,a[20][20];
  printf("Enter the order of the matrix:");
  scanf("%d%d",&n,&m);
  printf("Enter the element of the matrix row wise:");
  for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
  scanf("%d",&a[i][j]);
  }
  printf("The given matrix is:\n");
  for(i=0;i<n;i++)
{
  for(j=0;j<m;j++)
  {
  printf("%d \t",a[i][j]);
  }
  printf("\n");
}
  printf("The transpose of the given matrix is:\n");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    printf("%d \t",a[j][i]);
  }
  printf("\n");
}
}

Output:

The output is:
Find the transpose of a matrix in C-Programming

Explanation:


1. First we used the header package <stdio.h>. Visit Header package to know more.
2. We include the integer variables n,m,i,j and a[20][20]. First we used n for store how many row and m for how many column. i and j have been used for for-loop. a[20][20] has been used to store the element of the matrix. Here we use int a[20][20] for store only the integer element in the matrix, you can use float a[20][20] for store the real number.
3. Then we store the order of the matrix in n and m with scanf.
4. Then we store all the elements of the matrix using for-loop row wise.
5. After that we display the matrix using for-loop by printf syntax.
6. Next we display the transpose of the matrix.


Lets see with an example how the first for-loop works-
The loops is like this:
for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
  scanf("%d",&a[i][j]);
  }
Suppose that user want to type a 3x2 matrix. So n=3 and m=2, now 1st for-loop start from i=0 which less than 3 so it will go into 2nd for loop and start with j=0 which is less than 2 so it will enter in the bracket of 2nd for-loop and scan the number and store it in 00 position. Now it will leave from the bracket and increased by 1 so j will be now 1 and which is less than 2 so it again enter into the 2nd for-loop and doing the same process and it will store the number in the 01 position and exit and increasing j with 1. Now j will be 2 which is not less that 2 therefore it will not entered in the 2nd for-loop.  It will again enter 1st for-loop for i=1 and doing the same process and exit and will increase the value of i that is, it will be 2 which is less than 3 and it will enter in the 1st for-loop and doing the same process. It will running till the condition is satisfied.

20/02/2019

Sum Of Two Matrix in C-Programming:

 We will add two matrix in C-Programming. First we have to know that how to add two matrix. So two matrix can be added if the order of this two matrix are same. Now how the addition is defined? - the matrix addition is defined as in the given picture.
Sum Of Two Matrix in C-Programming

Important Tools:

1. array
2. for-loop
3. if-else

Code:

Sum Of Two Matrix in C-Programming

#include<stdio.h>
#include<conio.h>
int main()
{
int m,n,m1,n1,a[50][50],i,j,b[50][50],c[50][50];
printf("Enter the order of the first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter the order of the second matrix: ");
scanf("%d%d",&m1,&n1);
//Condition for addition
if(m!=m1||n!=n1)
printf("The sum is not possible");
//The first matrix scan and print
else
{
printf("Enter the first matrix row wise: ");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    scanf("%d",&a[i][j]);
  }
}
printf("The 1st matrix is:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
  printf("%d\t",a[i][j]);
  }
printf("\n");
}
//the second matrix scan and print
printf("Enter the 2nd matrix row wise:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    scanf("%d",&b[i][j]);
  }
}
printf("The 2nd matrix is:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
  printf("%d\t",b[i][j]);
  }
printf("\n");
}
//addition of two matrix print
printf("The sum of the matrix\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    c[i][j]=a[i][j]+b[i][j];
  printf("%d\t",c[i][j]);
  }
  printf("\n");
}
}
}

Output:

The output is given below
Sum Of Two Matrix in C-Programming

Explanation:

1. We use <stdio.h> and <conio.h> two header package.
2. first of all we check the condition for the addition of two matrix. So we use if-else condition for that. If the order of two matrix is equal then it can be added if not it is impossible.
3. We use  m,n,m1,n1,a[50][50],i,j,b[50][50] and c[50][50] variable here. m and n are used for the order of the 1st matrix and n1 and  m1 are used for order of 2nd matrix. a[50][50] and b[50][50] are used for store the given two matrices and we used c[50][50] to store and show the sum of two given matrices. And i and j are used in the loop.
4. First we scan and store two matrices in the variable a[50][50] and b[50][50] respectively using for-loop for know more see my post. After that we add the two matrices and store it in the variable c[50][50]. Therefore we print the c[50][50] variable using for-loop.

Print a MATRIX in C-Programming using array: 

Here we print a matrix with the help of array; so what is a matrix? - Matrix is a collection of numbers arranged into a fixed number of rows and columns. The numbers can be real or complex, but we use real numbers here.
Print a MATRIX in C-Programming using array

Code:

Print a MATRIX in C-Programming using array

#include<stdio.h>
#include<conio.h>
int main()
{
  int m,n,a[100][100],i,j;
  printf("Enter the order of the matrix:\n");
  scanf("%d%d",&m,&n);
  printf("Give the %d*%d matrix",m,n);
  printf("\n");
  for(i=0;i<n;i++)
  {
    printf("Enter the %d row\n",i+1);
    for(j=0;j<m;j++)
    scanf("%d",&a[i][j]);
  }
  printf("The given matrix is:\n");
  for (i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
    printf("%d\t",a[i][j]);
    printf("\n");
  }
}

Output: 

The output is given below-
Print a MATRIX in C-Programming using array

Explanation: 

1. First we use header package <stdio.h> and <conio.h>. To know more visit package page.
2. We use 5 integer variables. we use m and n for taking the order of the matrix. Then we use a[100][100] variable which called array its means that a variable has 100x100 box space, easily you can think that it would give a 10000 box space. You can change the array size 100 by changing it with any number. We use i and j variable for the use of for-loop.
3. First we take the order of the matrix by users and store it in m and n.
4. After that we store the numbers in variable a[100][100] with the help of for-loop. we scan the numbers and store all the numbers in a[100][100] variable. Now we have to print all the numbers that we stored it in the variable a[100][100], for that we use for-loop again for to print those numbers.

If you have any doubts then comment below.