KEMBAR78
Sparse 3 | PDF | Computer Programming | Linear Algebra
0% found this document useful (0 votes)
14 views1 page

Sparse 3

Uploaded by

dhanushk20044002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Sparse 3

Uploaded by

dhanushk20044002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Preview Cheat Sheet

HIDE
Cheat Sheet Editor #include <stdio.h>
#define MAX 10
void convertToSparse(int matrix[MAX][MAX], int row, int col) {
int sparse[MAX * MAX][3];

Online Cheating notes editor for exams int k = 0;


// Traverse the matrix and find non-zero elements
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] != 0) {

Start writing your exam cheat sheet in the editor below sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];

Pick the font size and the line height. Letter 5 is recommended for }
}
k++;

exams }
// Handle case where all elements are zero
Resize the exam cheat sheet by grabbing and moving the bottom right if (k == 0) {
printf("\nSparse matrix is empty (no non-zero elements).\n");

holder of the cheat sheet preview at the right side


return;
}

Add more cheat sheets by pressing (+) // Print the sparse matrix
printf("\nSparse Matrix (Row Col Value):\n");
for (int i = 0; i < k; i++) {
printf("%d %d %d\n", sparse[i][0], sparse[i][1], sparse[i][2]);
}
}
int main() {
int matrix[MAX][MAX];
int row, col;
// Get matrix size from user
printf("Enter rows and columns (max %d x %d): ", MAX, MAX);
#include <stdio.h> scanf("%d %d", &row, &col);
// Validate matrix size
#define MAX 10 if (row <= 0 || col <= 0 || row > MAX || col > MAX) {
printf("Error: Invalid matrix size. Please enter values between 1 and %d.\n", MAX);
return 1;
}
// Get matrix values
void convertToSparse(int matrix[MAX][MAX], int row, int col) { printf("Enter matrix values:\n");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int sparse[MAX * MAX][3]; if (scanf("%d", &matrix[i][j]) != 1) {
printf("Invalid input. Please enter integers only.\n");
return 1;
int k = 0; }
}
}
// Convert and print sparse matrix
convertToSparse(matrix, row, col);
// Traverse the matrix and find non-zero elements return 0;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] != 0) {
sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];
k++;
}
}
}

// Handle case where all elements are zero


if (k == 0) {
printf("\nSparse matrix is empty (no non-zero elements).\n");
return;
}

// Print the sparse matrix


printf("\nSparse Matrix (Row Col Value):\n");
for (int i = 0; i < k; i++) {
printf("%d %d %d\n", sparse[i][0], sparse[i][1], sparse[i][2]);
}
}

int main() {
int matrix[MAX][MAX];
int row, col;

// Get matrix size from user


printf("Enter rows and columns (max %d x %d): ", MAX, MAX);
scanf("%d %d", &row, &col);

// Validate matrix size


if (row <= 0 || col <= 0 || row > MAX || col > MAX) {
printf("Error: Invalid matrix size. Please enter values between 1 and %d.\n", MAX);
return 1;
}

// Get matrix values


printf("Enter matrix values:\n");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (scanf("%d", &matrix[i][j]) != 1) {
printf("Invalid input. Please enter integers only.\n");
return 1;
}
}
}

// Convert and print sparse matrix


convertToSparse(matrix, row, col);

return 0;
}

Print Cheat Sheet

You might also like