a c code by using memory allocation for add ,multiply of sprase matrixes



a c code by using memory allocation for add ,multiply of sprase matrixes..

Answer / Rajeev Kumar Malhavtra

Here is a simple C code that uses dynamic memory allocation to perform addition and multiplication of sparse matrices. Note that this is just an example and may not be optimized or error-free.nn```cn#include <stdio.h>n#include <stdlib.h>nn#define MAX_ROWS 100n#define MAX_COLS 100nn// Structure for sparse matrix elementsnstruct Matrix {n int row, col;n double value;n};nn// Function to allocate memory for a matrixnMatrix* createMatrix(int rows) {n Matrix *matrix = (Matrix*)malloc(rows * sizeof(Matrix));n return matrix;n}nn// Function to free memory for a matrixnvoid freeMatrix(Matrix *matrix, int rows) {n for (int i = 0; i < rows; ++i)n free(matrix + i);n free(matrix);n}nn// Function to perform addition of sparse matricesnvoid addMatrices(Matrix A, Matrix B, int rows, int cols, Matrix *result) {n for (int i = 0; i < rows; ++i)n for (int j = 0; j < cols; ++j)n result[i][j].value += A[i][j].value + B[i][j].value;n}nn// Function to perform multiplication of sparse matricesnvoid multiplyMatrices(Matrix A, Matrix B, int rowsA, int colsA, int rowsB, int colsB, Matrix *result) {n for (int i = 0; i < rowsA; ++i)n for (int j = 0; j < colsB; ++j)n result[i][j].value = 0;nn for (int k = 0; k < colsA; ++k)n for (int i2 = 0; i2 < rowsA; ++i2)n for (int j2 = 0; j2 < colsB; ++j2)n result[i][j].value += A[i2][k].value * B[k][j].value;n}nnint main() {n int rowsA = 3, colsA = 4, rowsB = 4, colsB = 5;n Matrix A = createMatrix(rowsA);n Matrix B = createMatrix(rowsB);n Matrix result = createMatrix(rowsA);nn // Fill matrices with values herenn addMatrices(A, B, rowsA, colsA, rowsB, colsB, &result);n multiplyMatrices(A, B, rowsA, colsA, rowsB, colsB, &result);nn // Print results herenn freeMatrix(A, rowsA);n freeMatrix(B, rowsB);n freeMatrix(result, rowsA);n return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

What is the difference between if else and switchstatement

1 Answers  


How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance

8 Answers  


7-Given an index k, return the kth row of the Pascal's triangle. For example, when k = 3, the row is [1,3,3,1]. For reference look at the following standard pascal’s triangle.

1 Answers  


State two uses of pointers in C?

1 Answers   Aspire, Infogain,


ATM machine and railway reservation class/object diagram

0 Answers   Zycus Infotech,


What are the different types of control structures in programming?

1 Answers  


we need to calculating INCOME TAX for the person. The INCOME TAX is as follows:- First $10000/- of income : 4% tax Next $10000/- of income : 8% tax Next $10000/- of income : 11.5% tax above $10, 00,00/- : 15% tax What is the Solution of this Question ?

1 Answers  


Taking an example,differentiate b/w loader and linker ?

1 Answers  


What are volatile variables?

1 Answers   Mind Tree,


can u give me the good and very optimised code for a car racing game?

0 Answers  


What is the difference between constant pointer and pointer to a constant. Give examples.

4 Answers   TCS,


how to exchnage bits in a byte b7<-->b0 b6<-->b1 b5<-->b2 b4<-->b3 please mail me the code if any one know to rajeshmb4u@gmail.com

3 Answers   Honeywell, Huawei,


Categories