Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int r1, c1, r2, c2, i, j, k, sum = 0;
- int *ptf, *pts, *ptr;
- int first[10][10], second[10][10], result[10][10];
- void add()
- {
- for(i=0;i<r1;i++)
- for(j=0;j<c2;j++)
- *(ptr+(i*10 +j)) = *(ptf+(i*10+j)) + *(ptf+(i*10+j));
- }
- void sub()
- {
- for(i=0;i<r1;i++)
- for(j=0;j<c2;j++)
- *(ptr+(i*10 +j)) = *(ptf+(i*10+j)) - *(ptf+(i*10+j));
- }
- void mul()
- {
- for ( i = 0 ; i < r1 ; i++ )
- {
- for ( j = 0 ; j < c2 ; j++ )
- {
- *(ptr + (i * 10+j)) = 0;
- for ( k = 0 ; k < r2 ; k++ )
- *(ptr + (i * 10+j)) += *(ptf + (i*10 + k)) * *(pts + (k*10 + j));
- }
- }
- }
- int main()
- {
- int choice;
- printf("Enter the number of rows and columns of first matrix\n");
- scanf("%d%d", &r1, &c1);
- printf("Enter the number of rows and columns of second matrix\n");
- scanf("%d%d", &r2, &c2);
- printf("Enter the elements of first matrix\n");
- for ( i = 0 ; i < r1 ; i++ )
- {
- for ( j = 0 ; j < c1 ; j++ )
- scanf("%d", &first[i][j]);
- printf("\n");
- }
- printf("Enter the elements of second matrix\n");
- for ( i = 0 ; i < r2 ; i++ )
- {
- for ( j = 0 ; j < c2 ; j++ )
- scanf("%d", &second[i][j]);
- printf("\n");
- }
- printf("First matrix is:-\n");
- for ( i = 0 ; i < r1 ; i++ )
- {
- for ( j = 0 ; j < c1 ; j++ )
- printf("%d\t", first[i][j]);
- printf("\n");
- }
- printf("Second Matrix is:-\n");
- for ( i = 0 ; i < r2 ; i++ )
- {
- for ( j = 0 ; j < c2 ; j++ )
- printf("%d\t", second[i][j]);
- printf("\n");
- }
- ptf = &first[0][0];
- pts = &second[0][0];
- ptr = &result[0][0];
- printf("1. Add 2. Subtract 3. Multiply: ");
- scanf("%i",&choice);
- printf("The result is:-\n");
- switch (choice)
- {
- case 1: add(); break;
- case 2: sub(); break;
- case 3:if ( c1 != r2 )
- {
- printf("Number of columns of first matrix and rows of second matrix must be same.\n");
- return (0);
- }
- mul(); break;
- }
- for ( i = 0 ; i < r1 ; i++ )
- {
- for ( j = 0 ; j < c2 ; j++ )
- printf("%d\t", result[i][j]);
- printf("\n");
- }
- return 0;
- }
- //Output
- //Enter the elements of second matrix
- //1
- //2
- //3
- //4
- //5
- //6
- //7
- //8
- //9
- //First matrix is:-
- //1 2 3
- //4 5 6
- //7 8 9
- //Second Matrix is:-
- //1 2 3
- //4 5 6
- //7 8 9
- //1. Add 2. Subtract 3. Multiply: 1
- ///The result is:-
- //2 4 6
- //8 10 12
- //14 16 18
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement