Skip to main content

Search This Blog

matrix multiplication in c using functions and pointers

 #include <stdio.h> #define ROWS 3 #define COLS 3 void matrixMultiply(int *mat1, int *mat2, int *result, int rows1, int cols1, int cols2) {     int i, j, k;     // Multiplying matrices     for (i = 0; i < rows1; i++) {         for (j = 0; j < cols2; j++) {             *(result + i * cols2 + j) = 0;             for (k = 0; k < cols1; k++) {                 *(result + i * cols2 + j) += *(mat1 + i * cols1 + k) * *(mat2 + k * cols2 + j);             }         }     } } void displayMatrix(int *mat, int rows, int cols) {     int i, j;     // Displaying matrix     for (i = 0; i < rows; i++) {         for (j = 0; j < cols; j++) {             printf("%d\t", *(mat + i * cols + j)); ...

multiply two matrix using function

 #include <stdio.h>


#define ROWS 3

#define COLS 3


void matrixMultiply(int mat1[ROWS][COLS], int mat2[ROWS][COLS], int result[ROWS][COLS]) {

    int i, j, k;


    // Multiplying matrices

    for (i = 0; i < ROWS; i++) {

        for (j = 0; j < COLS; j++) {

            result[i][j] = 0;

            for (k = 0; k < ROWS; k++) {

                result[i][j] += mat1[i][k] * mat2[k][j];

            }

        }

    }

}


void displayMatrix(int mat[ROWS][COLS]) {

    int i, j;


    // Displaying matrix

    for (i = 0; i < ROWS; i++) {

        for (j = 0; j < COLS; j++) {

            printf("%d\t", mat[i][j]);

        }

        printf("\n");

    }

}


int main() {

    int mat1[ROWS][COLS] = {{1, 2, 3},

                            {4, 5, 6},

                            {7, 8, 9}};

    int mat2[ROWS][COLS] = {{9, 8, 7},

                            {6, 5, 4},

                            {3, 2, 1}};

    int result[ROWS][COLS];


    // Multiplying matrices

    matrixMultiply(mat1, mat2, result);


    // Displaying matrices

    printf("Matrix 1:\n");

    displayMatrix(mat1);

    printf("\nMatrix 2:\n");

    displayMatrix(mat2);

    printf("\nResultant Matrix:\n");

    displayMatrix(result);


    return 0;

}

  1. Define Matrix Sizes: The matrices are defined with a constant number of rows and columns. In this example, both matrices are 3x3, but you can adjust the ROWS and COLS macros to change their sizes.

  2. Matrix Multiplication Function (matrixMultiply): This function takes three parameters: two matrices (mat1 and mat2) to be multiplied and a third matrix (result) where the product will be stored. It uses nested loops to iterate over each element of the result matrix and calculate its value by performing the dot product of the corresponding row in mat1 and column in mat2 Display Matrix Function (displayMatrix): This function takes a matrix as input and displays its contents row by row, separated by tabs.

  3. Main Function (main): In the main function, two matrices (mat1 and mat2) are initialized with sample values. Then, the matrixMultiply function is called to compute the product of these matrices, and the result is stored in the result matrix. Finally, the contents of all three matrices are displayed using the displayMatrix function.

Comments

Popular posts from this blog

Write and explain the following types of functions with the help of an example program for each (i) Function with no arguments and no return value. (ii) Function with arguments and no return value

hello Write and explain the following types of functions with the help of an example program for each  (i) Function with no arguments and no return value. (ii) Function with arguments and no return value. ANS-    1:  Function with no arguments and no return value: OUTPUT               In this type the function has no arguments, it doesn't receive any data from the calling function. Similarly it doesn't return any value, the calling function doesn't receive any data from called function.  So there's no digital communication between calling function and called function. #include<stdio.h> #include<conio.h> void sum(); void main() {        clrscr();     sum();     getch(); } void sum() {     int a,b;     printf(”Enter any two numbers:”);     scanf("%d%d",&a,&b);     printf(”sum= %d”,a+b); } In this function no return and no argumen...

Write a C program (use a switch statement for selection) to add or subtract 2 matrices having order 3 x 3, depending upon the choice made by the user

Write a C program (use a switch statement for selection) to add or subtract 2 matrices having order 3 x 3, depending upon the choice made by the user. MCS-011 : PROBLEM SOLVING AND   PROGRAMMING (June, 2017) c program for matrix operations using switch case, c program for addition and subtraction of two matrices, algorithm for matrix addition in c program #include <stdio.h> #include<conio.h> void main() {     int first_matrix[3][3], second_matrix[3][3], sub_of_two_matrix[3][3],sum_of_two_matrix[3][3], i, j;      int oprater;      clrscr(); //Enter First Matrix      printf("\nEnter elements of 1st matrix:\n");     for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) {     printf("Enter element a%d%d: ", i + 1, j + 1);     scanf("%d", &first_matrix[i][j]); } //Enter Second Matrix      printf("Enter elements of 2nd matrix:\n");     for (i = 0;...

Here you get a leave from the office to have a physical relationship, know why the government has made this unique rule

Here you get a leave from the office to have a physical relationship, know why the government has made this unique rule   There is an official holiday for having a physical relationship, are you surprised to read the headline? But this is true. There is a country where people get leave to have physical relationship. Let us tell about that country and what is the reason behind it. Relationship Desk . The meeting of two hearts or the establishment of a physical relationship between them is a moment. Why would a leave be given for this? But you will find it strange to know that there is a country where the government has made a provision of leave for this. People get leave from office to have sex. The name of that country is Denmark. Yes, this facility has been given to the people in this country. Actually, the decreasing population of Denmark has been a matter of concern for the government there. From time to time, the government there runs such campaigns. Due to which people get the...

Contact Form

Name

Email *

Message *