Showing posts with label Practice. Show all posts
Showing posts with label Practice. Show all posts

Wednesday, April 3, 2024

Matrix multiplication in C

 


Matrix multiplication in C: We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from the user for row number, column number, first matrix elements and second matrix elements. Then we are performing multiplication on the matrices entered by the user.

In matrix multiplication first matrix one row element is multiplied by second matrix all column elements.

Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by the figure given below:

matrix multiplication program in c

Let's see the program of matrix multiplication in C.

  1. #include<stdio.h>    
  2. #include<stdlib.h>  
  3. int main(){  
  4. int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    
  5. system("cls");  
  6. printf("enter the number of row=");    
  7. scanf("%d",&r);    
  8. printf("enter the number of column=");    
  9. scanf("%d",&c);    
  10. printf("enter the first matrix element=\n");    
  11. for(i=0;i<r;i++)    
  12. {    
  13. for(j=0;j<c;j++)    
  14. {    
  15. scanf("%d",&a[i][j]);    
  16. }    
  17. }    
  18. printf("enter the second matrix element=\n");    
  19. for(i=0;i<r;i++)    
  20. {    
  21. for(j=0;j<c;j++)    
  22. {    
  23. scanf("%d",&b[i][j]);    
  24. }    
  25. }    
  26.     
  27. printf("multiply of the matrix=\n");    
  28. for(i=0;i<r;i++)    
  29. {    
  30. for(j=0;j<c;j++)    
  31. {    
  32. mul[i][j]=0;    
  33. for(k=0;k<c;k++)    
  34. {    
  35. mul[i][j]+=a[i][k]*b[k][j];    
  36. }    
  37. }    
  38. }    
  39. //for printing result    
  40. for(i=0;i<r;i++)    
  41. {    
  42. for(j=0;j<c;j++)    
  43. {    
  44. printf("%d\t",mul[i][j]);    
  45. }    
  46. printf("\n");    
  47. }    
  48. return 0;  
  49. }  

Output:

enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure given below:

matrix multiplication in c

---------------------------------------------------




2×2 Matrix Multiplication

Let’s consider a simple 2 × 2 matrix multiplication

=[3749]  =[6258]

Now each of the elements of product matrix AB can be calculated as follows:

  • AB11 = 3 × 6 + 7 ×5 = 53
  • AB12 = 3 × 2 + 7 × 8 = 62
  • AB21 = 4 × 6 + 9 × 5 = 69
  • AB22 = 4 × 2 + 9 × 8 = 80

Therefore,

=[53626980]

3×3 Matrix Multiplication

To understand the multiplication of two 3 × 3 matrices, let us consider two 3 × 3 matrices A and B.

=[1284317149810], =[519361597816]

Each element of the Product matrix AB can be calculated as follows:

  • AB11 = 12×5 + 8×6 + 4×7 = 136
  • AB12 = 12×19 + 8×15 + 4×8 = 380
  • AB13 = 12×3 + 8×9+4×16 = 172
  • AB21 = 3×5 + 17×6 + 14×7 = 215
  • AB22 = 3×19 + 17×15 + 14×8 = 424
  • AB23 = 3×3 + 17×9 + 14×16 = 386
  • AB31 = 9×5 + 8×6 + 10×7 = 163
  • AB32 = 9×19 + 8×15 + 10×8 = 371
  • AB33 = 9×3 + 8×9 + 10×16 = 259

Therefore,


//C program to check whether a number is divisible by 2 and 9 or not


#include <stdio.h>

int main()

{

    int num;   

    printf("Enter any number: ");

    scanf("%d", &num);


//if(!(num % 2) && !(num % 9))

    if((num % 2 == 0) && (num % 9 == 0))

    {

        printf("Number is divisible by 2 and 9");

    }

    else

    {

        printf("Number is not divisible by 2 and 9");

    }

    return 0;

}


//Enter any number: 18

//Number is divisible by 2 and 9


C program to sort a string in alphabetic order & Sorting of a Set of Strings in Ascending alphabetical order

 

C program to sort a string in alphabetic order

C program to sort a string in alphabetic order: For example, if a user inputs a string "programming," then the output will be "aggimmnoprr", so output string will contain characters in alphabetical order. We assume input string contains only lower case alphabets. We count how many times characters 'a' to 'z' appear in the input string and then create another string that contains characters 'a' to 'z' as many times as they appear in the input string.

C program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char ch, input[100], output[100];
  int no[26] = {0}, n, c, t, x;

  printf("Enter some text\n");
  scanf("%s", input);

  n = strlen(input);

  /** Storing how many times characters (a to z)
    appears in input string in an array */

  for (= 0; c < n; c++)
  {
    ch = input[c] - 'a';
    no[ch]++;
  }

  t = 0;

  /** Insert characters 'a' to 'z' in output string as many times
    as they appear in the input string */

  for (ch = 'a'; ch <= 'z'; ch++)
  {
    x = ch - 'a';

    for (= 0; c < no[x]; c++)
    {
      output[t] = ch;
      t++;
    }
  }
  output[t] = '\0';

  printf("%s\n", output);

  return 0;
}

Output of program:
Sort a string program output


C Program – Sorting of a Set of Strings in Ascending alphabetical order

/* This program would sort the input strings in
 * an ascending order and would display the same
 */
#include<stdio.h>
#include<string.h>
int main(){
   int i,j,count;
   char str[25][25],temp[25];
   puts("How many strings u are going to enter?: ");
   scanf("%d",&count);

   puts("Enter Strings one by one: ");
   for(i=0;i<=count;i++)
      gets(str[i]);
   for(i=0;i<=count;i++)
      for(j=i+1;j<=count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   printf("Order of Sorted Strings:");
   for(i=0;i<=count;i++)
      puts(str[i]);
   
   return 0;
}

Output:
sorted_strings_ascending

As you can observe in the above screenshot of output that we have entered 5 strings and the program then sorted them in ascending order. We got a sorted set of strings as output.

C Program to Compute the Sum of Diagonals of a Matrix

 

C Program to Compute the Sum of Diagonals of a Matrix

Here, we will compute the sum of diagonals of a Matrix using the following 3 methods:

  1. Using Conditional statements
  2. Taking Custom Input from the user whilst using Conditional Statements
  3. Using Functions

We will keep the same input in all the mentioned approaches and get an output accordingly.

Input: 

The matrix is 
1 2 3
4 5 6
7 8 9

Output:

Main diagonal elements sum is = 15
Off-diagonal elements sum is = 15 

Explanation: The main diagonals are 1, 5, and 9. So, the sum of the main diagonals is 1+5+9=15. The off diagonals are 3, 5, and 7. So, the sum of the main diagonals is 3+5+7=15

Taking input matrix from the user 

// C Program to Demonstrate the Sum of Diagonals
// of a Matrix by taking input from the user
#include <stdio.h>
  
int main()
{
  
    int i, j, m = 3, n = 3, a = 0, sum = 0;
    int matrix[10][10];
    
    // if both rows and columns are equal then it is
    // possible to calculate diagonal sum
    if (m == n) {
        
        // entering the coefficients of the matrix
        for (i = 0; i < m; ++i) {
            
            for (j = 0; j < n; ++j) {
                
                scanf("%d", &matrix[i][j]);
            }
        }
        // printing the input matrix
        printf("The matrix is \n");
        
        // iterates number of rows
        for (i = 0; i < m; ++i) {
            
            // iterates number of columns
            for (j = 0; j < n; ++j) {
                printf(" %d", matrix[i][j]);
            }
            printf("\n");
        }
        for (i = 0; i < m; ++i) {
            
            // calculating the main diagonal sum
            sum = sum + matrix[i][i];
            
            // calculating the off diagonal sum
            a = a + matrix[i][m - i - 1];
        }
        // printing the result
        printf("\nMain diagonal elements sum is = %d\n",sum);
        printf("Off-diagonal elements sum is = %d\n", a);
    }
    else
        // if both rows and columns are not equal then it is
        // not possible to calculate the sum
        printf("not a square matrix\n");
    return 0;
}

Output: 

The matrix is 
1 2 3
4 5 6
7 8 9
Main diagonal elements sum is = 15
Off-diagonal elements sum is = 15

Programming in C

  PDF Download Links University Question Papers Notes Laboratory Manual Record Important Questions and Question Bank Important Questions Pro...