write a program to create a sparse matrix using dynamic memory
allocation.



write a program to create a sparse matrix using dynamic memory allocation...

Answer / Ajit Kumar

In C, you can create a sparse matrix using dynamic memory allocation by defining a struct for the matrix elements and dynamically allocating memory for the non-zero elements. Here's an example:nn```cn#include <stdio.h> n#include <stdlib.h>nnstruct Matrix {n int row;n int col;n int value;n struct Matrix *next;n};nnvoid create_matrix(struct Matrix **head, int rows, int cols) {n *head = (struct Matrix *)malloc(sizeof(struct Matrix));n if (*head == NULL) {n printf("Memory error
");n return;n }n (*head)->row = 0;n (*head)->col = 0;n (*head)->value = 0;n (*head)->next = NULL;nn int i, j;n for (i = 1; i <= rows; ++i) {n for (j = 1; j <= cols; ++j) {n struct Matrix *new_node = (struct Matrix *)malloc(sizeof(struct Matrix));n if (new_node == NULL) {n printf("Memory error
");n return;n }n scanf("%d %d %d", &(new_node->row), &(new_node->col), &(new_node->value));n new_node->next = *head;n *head = new_node;n }n }n}nnvoid display(struct Matrix *head) {n struct Matrix *temp = head;n while (temp != NULL) {n printf("Row: %d, Column: %d, Value: %d
", temp->row, temp->col, temp->value);n temp = temp->next;n }n}nnint main() {n int rows, cols;n printf("Enter the number of rows and columns: ");n scanf("%d %d", &rows, &cols);n struct Matrix *head = NULL;n create_matrix(&head, rows, cols);n display(head);n return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

How does memset() work in C?

2 Answers  


When is a null pointer used?

1 Answers  


b) 4 c) 6 d) 7 32. Any C program a) must contain at least one function b) need not contain ant function c) needs input data d) none of the above 33. Using goto inside for loop is equivalent to using a) continue b) break c) return d)none of the above 34. The program fragment int a=5, b=2; printf(“%d”,a+++++b); a) prints 7 b)prints 8 c) prints 9 d)none of the above 35. printf(“ab” , “cd”,”ef”); prints a) ab abcdef c) abcdef, followed by garbage value d) none of the above 36. Consider the following program segment. i=6720; j=4; while((i%j)==0) { i=i/j; j=j+1; } On termination j will have the value a) 4 b) 8 c) 9 d) 6720

1 Answers   HCL,


#include<stdio.h> void main() { int =1; printf("%d%d%d",a++,++a,++a); }

3 Answers   VB,


Explain two-dimensional array.

1 Answers  


Is c procedural or object oriented?

1 Answers  


to print the salary of an employee according to follwing calculation: Allowances:HRA-20% of BASIC,DA-45% of BASIC,TA-10%. Deductions:EPF-8% of BASIC,LIC-Rs.200/-Prof.Tax:Rs.200/- create c language program?

1 Answers  


What would happen to X in this expression: X += 15; (assuming the value of X is 5)

1 Answers  


How can I sort a linked list?

1 Answers  


What is a function in c?

3 Answers  


Implement a function that returns the 5th element from the end in a singly linked list of integers in one pass.

11 Answers   Microsoft,


What is a program flowchart?

1 Answers  


Categories