C#: Determine whether a matrix is a sparse matrix
C# Sharp Array: Exercise-29 with Solution
Write a C# Sharp program to accept a matrix and determine whether it is a sparse matrix.
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise29
{
public static void Main()
{
// Declaration of variables for matrix manipulation
int[,] arr1 = new int[10, 10]; // Declare a 10x10 integer matrix
int i, j, r, c; // Variables for iteration and matrix dimensions
int ctr = 0; // Counter for the number of zero elements in the matrix
// User input for matrix dimensions
Console.Write("\n\nDetermine whether a matrix is a sparse matrix :\n ");
Console.Write("----------------------------------------------------\n");
Console.Write("Input the number of rows of the matrix : ");
r = Convert.ToInt32(Console.ReadLine()); // Read number of rows from user
Console.Write("Input the number of columns of the matrix : ");
c = Convert.ToInt32(Console.ReadLine()); // Read number of columns from user
Console.Write("Input elements in the matrix :\n");
// User input for matrix elements and checking for zeros
for (i = 0; i < r; i++) // Loop through rows
{
for (j = 0; j < c; j++) // Loop through columns
{
// Prompt user to enter matrix element
Console.Write("element - [{0}],[{1}] : ", i, j);
arr1[i, j] = Convert.ToInt32(Console.ReadLine()); // Read user input and store in the matrix
// Check if the entered element is zero
if (arr1[i, j] == 0)
{
++ctr; // Increment the counter if the element is zero
}
}
}
// Check if the number of zeros is greater than half the total elements in the matrix
if (ctr > ((r * c) / 2))
{
Console.Write("The given matrix is a sparse matrix. \n"); // Display message if matrix is sparse
}
else
{
Console.Write("The given matrix is not a sparse matrix.\n"); // Display message if matrix is not sparse
}
// Display the number of zeros in the matrix
Console.Write("There are {0} number of zeros in the matrix.\n\n", ctr);
}
}
Sample Output:
Determine whether a matrix is a sparse matrix : ---------------------------------------------------- Input the number of rows of the matrix : 2 Input the number of columns of the matrix : 2 Input elements in the first matrix : element - [0],[0] : 0 element - [0],[1] : 1 element - [1],[0] : 0 element - [1],[1] : 0 The given matrix is sparse matrix. There are 3 number of zeros in the matrix.
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to calculate determinant of a 3 x 3 matrix.
Next: Write a program in C# Sharp to accept two matrices and check whether they are equal.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/array/csharp-array-exercise-29.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics