Advertisement
Shailrshah

Matrix Operations

Apr 19th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <stdio.h>
  2. int r1, c1, r2, c2, i, j, k, sum = 0;
  3. int *ptf, *pts, *ptr;
  4. int first[10][10], second[10][10], result[10][10];
  5. void add()
  6. {
  7.   for(i=0;i<r1;i++)
  8.     for(j=0;j<c2;j++)
  9.       *(ptr+(i*10 +j)) = *(ptf+(i*10+j)) + *(ptf+(i*10+j));
  10. }
  11. void sub()
  12. {
  13.     for(i=0;i<r1;i++)
  14.       for(j=0;j<c2;j++)
  15.         *(ptr+(i*10 +j)) = *(ptf+(i*10+j)) - *(ptf+(i*10+j));
  16. }
  17. void mul()
  18. {
  19.  
  20.   for ( i = 0 ; i < r1 ; i++ )
  21.   {
  22.     for ( j = 0 ; j < c2 ; j++ )
  23.     {
  24.       *(ptr + (i * 10+j)) = 0;
  25.       for ( k = 0 ; k < r2 ; k++ )
  26.        *(ptr  + (i * 10+j)) += *(ptf + (i*10 + k)) * *(pts + (k*10 + j));
  27.     }
  28.   }
  29. }
  30.  
  31. int main()
  32. {
  33.   int choice;
  34.  
  35.   printf("Enter the number of rows and columns of first matrix\n");
  36.   scanf("%d%d", &r1, &c1);
  37.   printf("Enter the number of rows and columns of second matrix\n");
  38.   scanf("%d%d", &r2, &c2);
  39.  
  40.  
  41.   printf("Enter the elements of first matrix\n");
  42.   for (  i = 0 ; i < r1 ; i++ )
  43.   {
  44.     for ( j = 0 ; j < c1 ; j++ )
  45.       scanf("%d", &first[i][j]);
  46.     printf("\n");
  47.   }
  48.   printf("Enter the elements of second matrix\n");
  49.   for ( i = 0 ; i < r2 ; i++ )
  50.   {
  51.     for ( j = 0 ; j < c2 ; j++ )
  52.       scanf("%d", &second[i][j]);
  53.     printf("\n");
  54.   }
  55.  
  56.   printf("First matrix is:-\n");
  57.   for ( i = 0 ; i < r1 ; i++ )
  58.   {
  59.     for ( j = 0 ; j < c1 ; j++ )
  60.       printf("%d\t", first[i][j]);
  61.  
  62.     printf("\n");
  63.   }
  64.   printf("Second Matrix is:-\n");
  65.   for ( i = 0 ; i < r2 ; i++ )
  66.   {
  67.     for ( j = 0 ; j < c2 ; j++ )
  68.       printf("%d\t", second[i][j]);
  69.  
  70.     printf("\n");
  71.   }
  72.  
  73.   ptf = &first[0][0];
  74.   pts = &second[0][0];
  75.   ptr = &result[0][0];
  76.  
  77.   printf("1. Add 2. Subtract 3. Multiply: ");
  78.   scanf("%i",&choice);
  79.   printf("The result is:-\n");
  80.  
  81.   switch (choice)
  82.   {
  83.     case 1: add(); break;
  84.     case 2: sub(); break;
  85.     case 3:if ( c1 != r2 )
  86.             {
  87.               printf("Number of columns of first matrix and rows of second matrix must be same.\n");
  88.               return (0);
  89.             }
  90.             mul(); break;
  91.   }
  92.  
  93.   for ( i = 0 ; i < r1 ; i++ )
  94.   {
  95.     for ( j = 0 ; j < c2 ; j++ )
  96.       printf("%d\t", result[i][j]);
  97.  
  98.     printf("\n");
  99.   }
  100.   return 0;
  101. }
  102. //Output
  103. //Enter the elements of second matrix
  104. //1
  105. //2
  106. //3
  107.  
  108. //4
  109. //5
  110. //6
  111.  
  112. //7
  113. //8
  114. //9
  115.  
  116. //First matrix is:-
  117. //1       2       3
  118. //4       5       6
  119. //7       8       9
  120. //Second Matrix is:-
  121. //1       2       3
  122. //4       5       6
  123. //7       8       9
  124. //1. Add 2. Subtract 3. Multiply: 1
  125. ///The result is:-
  126. //2       4       6
  127. //8       10      12
  128. //14      16      18
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement