0% found this document useful (0 votes)
20 views4 pages

MatrixOps in C

Random Matrix Generation: Creates two random 4x4 matrices with integer values from 0 to 9. Matrix Multiplication: Computes the product of the two matrices. Trace Calculation: Calculates the sum of the diagonal elements of the resulting matrix. Matrix Transposition: Computes the transpose of the resulting matrix. Matrix Display: Prints the matrices and results in a readable format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

MatrixOps in C

Random Matrix Generation: Creates two random 4x4 matrices with integer values from 0 to 9. Matrix Multiplication: Computes the product of the two matrices. Trace Calculation: Calculates the sum of the diagonal elements of the resulting matrix. Matrix Transposition: Computes the transpose of the resulting matrix. Matrix Display: Prints the matrices and results in a readable format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>

#include <stdlib.h>

#include <time.h>

#define N 4

// Function to generate a random NxN matrix

void generateMatrix(int matrix[N][N]) {

srand(time(0));

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

matrix[i][j] = rand() % 10;

// Function to print a matrix

void printMatrix(int matrix[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

}
// Function to multiply two matrices

void multiplyMatrices(int mat1[N][N], int mat2[N][N], int result[N][N]) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

result[i][j] = 0;

for (int k = 0; k < N; k++) {

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

// Function to find the trace of a matrix

int trace(int matrix[N][N]) {

int trace = 0;

for (int i = 0; i < N; i++) {

trace += matrix[i][i];

return trace;

// Function to find the transpose of a matrix

void transposeMatrix(int matrix[N][N], int transpose[N][N]) {

for (int i = 0; i < N; i++) {


for (int j = 0; j < N; j++) {

transpose[i][j] = matrix[j][i];

int main() {

int mat1[N][N], mat2[N][N], result[N][N], transposed[N][N];

// Generate two random matrices

generateMatrix(mat1);

generateMatrix(mat2);

printf("Matrix 1:\n");

printMatrix(mat1);

printf("Matrix 2:\n");

printMatrix(mat2);

// Multiply the two matrices

multiplyMatrices(mat1, mat2, result);

printf("Product of Matrix 1 and Matrix 2:\n");

printMatrix(result);

// Calculate the trace of the resulting matrix


printf("Trace of the result matrix: %d\n", trace(result));

// Transpose the resulting matrix

transposeMatrix(result, transposed);

printf("Transpose of the result matrix:\n");

printMatrix(transposed);

return 0;

You might also like