Question

In: Computer Science

Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...

Implement function matmul() that embodies multiplication of n*n matrix in c language?(code)

Can you let me know?

Solutions

Expert Solution

Please find the matmul() implemented function below:

// C program to multiply two matrices. 

#include <stdio.h> 

const int MAX = 100; 
// Function to print Matrix 
void printMatrix(int M[][MAX], int n) 
{ 
    for (int i = 0; i < n; i++) { 
        for (int j = 0; j < n; j++) 
            printf("%d ", M[i][j]); 
  
        printf("\n"); 
    } 
} 
// Function to multiply two matrices A[][] and B[][] 
void matmul(int A[][MAX], int B[][MAX], int n) 
{ 
        int i, j, k; 

        // Matrix to store the result 
        int C[MAX][MAX]; 


        // Multiply the two 
        for (i = 0; i < n; i++) { 
                for (j = 0; j < n; j++) { 
                        C[i][j] = 0; 
                        for (k = 0; k < n; k++) 
                                C[i][j] += A[i][k] * B[k][j]; 
                } 
        } 

        // Print the result 
        printf("\nResultant Matrix: \n"); 
        printMatrix(C, n); 
} 

// Main function 
int main() 
{ 
        int n, i, j; 
        int A[MAX][MAX], B[MAX][MAX]; 

        // Read size of Matrix A from user 
        printf("Enter the value of n: "); 
        scanf("%d", &n); 

        // Read the elements of Matrix A from user 
        printf("\nEnter the elements of First Matrix: "); 
        for (i = 0; i < n; i++) { 
                for (j = 0; j < n; j++) { 
                        printf("\nA[%d][%d]: ", i, j); 
                        scanf("%d", &A[i][j]); 
                        printf("%d", A[i][j]); 
                } 
        }  

        // Read the elements of Matrix B from user 
        printf("\nEnter the elements of First Matrix: "); 
        for (i = 0; i < n; i++) { 
                for (j = 0; j < n; j++) { 
                        printf("\nB[%d][%d]: ", i, j); 
                        scanf("%d", &B[i][j]); 
                        printf("%d", B[i][j]); 
                } 
        } 

        // Print the Matrix A 
        printf("\n\nFirst Matrix: \n"); 
        printMatrix(A, n); 

        // Print the Matrix B 
        printf("\nSecond Matrix: \n"); 
        printMatrix(B, n); 

        // Find the product of the 2 matrices 
        matmul(A, B, n); 

        return 0; 
} 

OUTPUT:

First Matrix: 
1 2 3 
4 5 6 
7 8 9 

Second Matrix: 
1 2 3 
4 5 6 
7 8 9 

Resultant Matrix: 
30 36 42 
66 81 96 
102 126 150
 

Related Solutions

Can you give me a turing machine for the language c* n b*2n a* n+2 for...
Can you give me a turing machine for the language c* n b*2n a* n+2 for n>=0 . Please give the entire diagram with states, transition function, alphabet. Can use either one-tape or two-tape (both infinite). Describe the logic used to build the machine. Run the TM that accepts for any string of length > 1. Also run for string cbba.
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without thread AND with thread. [12:18, 10/2/2020] Mohan Reddy: ou are to implement (M by N matrix) times (N by 1 vector) operation and see how multiple threads can speed up the computation. The resulting vector will be (M by 1 vector). See the following steps/requirements. 1. Accept M and N as keyboard input. 2. Generate a random (M by N matrix) and a random...
Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
Can you please code in C the Lagrangian function. If you have any questions please let...
Can you please code in C the Lagrangian function. If you have any questions please let me know.
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from...
Let A be a diagonalizable n × n matrix and let P be an invertible n...
Let A be a diagonalizable n × n matrix and let P be an invertible n × n matrix such that B = P−1AP is the diagonal form of A. Prove that Ak = PBkP−1, where k is a positive integer. Use the result above to find the indicated power of A. A = 6 0 −4 7 −1 −4 6 0 −4 , A5 A5 =
Let A be a diagonalizable n × n matrix and let P be an invertible n...
Let A be a diagonalizable n × n matrix and let P be an invertible n × n matrix such that B = P−1AP is the diagonal form of A. Prove that Ak = PBkP−1, where k is a positive integer. Use the result above to find A5 A = 4 0 −4 5 −1 −4 6 0 −6
Let “ ·n” be multiplication modulo n, and consider the set Un = { [a] ∈...
Let “ ·n” be multiplication modulo n, and consider the set Un = { [a] ∈ Zn | there is a [b] ∈ Zn with [a] ·n [b] = [1]} (a) Show that (Un, ·n ) is a group. (b) Write down the Cayley table for U5. Hint: |U5| = 4. (c) Write down the Cayley table for U12. Hint: |U12| = 4.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT