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)); ...

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));

        }

        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((int *)mat1, (int *)mat2, (int *)result, ROWS, COLS, COLS);


    // Displaying matrices

    printf("Matrix 1:\n");

    displayMatrix((int *)mat1, ROWS, COLS);

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

    displayMatrix((int *)mat2, ROWS, COLS);

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

    displayMatrix((int *)result, ROWS, COLS);


    return 0;

}

  1. Matrix Multiplication Function (matrixMultiply): This function takes pointers to the two matrices (mat1 and mat2) to be multiplied and a pointer to the result matrix (result). Additionally, it takes the dimensions of the matrices (rows1, cols1, and cols2). Inside the function, the matrices are accessed using pointer arithmetic, and matrix multiplication is performed similarly to the previous example.

  2. Display Matrix Function (displayMatrix): This function takes a pointer to a matrix (mat), along with its dimensions (rows and cols). It uses pointer arithmetic to access the elements of the matrix and prints them row by row.

  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.

This code demonstrates how to multiply two matrices in C using functions and pointers for passing matrices to functions.

Comments

Popular posts from this blog

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;...

how to multiple number enter and sort the Ascending Order

how to multiple number enter and sort the Ascending Order #include<Stdio.h> #include<conio.h> int main(void){ int a[10],b,c,n,i,j; printf("Enter number of elements :"); scanf("%d",&n); for(i=1; i<=n; i++){ printf("Enter any number :"); scanf("%d",&a[i]); } for(i=1; i<=n; i++){ for(j=i+1; j<=n; j++){ if(a[i]>a[j]){ b=a[i]; a[i]=a[j]; a[j]=b; } } } printf("Ascending Order :\n"); for(i=1; i<=n; i++){ printf("%d\t",a[i]); } getch(); }

Contact Form

Name

Email *

Message *