Computer Applications
Write a program to accept a two-dimensional integer array of order 4 x 5 as input from the user. Check if it is a Sparse Matrix or not. A matrix is considered to be a sparse, if the total number of zero elements is greater than the total number of non-zero elements. Print appropriate messages.
Example:
| 4 | 3 | 0 | 1 | 0 |
| 1 | 0 | 0 | 2 | 0 |
| 1 | 0 | 1 | 0 | 0 |
| 0 | 3 | 2 | 0 | 0 |
Number of zero elements = 11
Number of non zero elements = 9
Matrix is a Sparse Matrix
Java
Java Arrays
2 Likes
Answer
import java.util.Scanner;
class SparseMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[4][5];
int i, j, zero = 0, nonzero = 0;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 5; j++)
{
System.out.print("Enter element: ");
a[i][j] = sc.nextInt();
}
}
for(i = 0; i < 4; i++)
{
for(j = 0; j < 5; j++)
{
if(a[i][j] == 0)
zero++;
else
nonzero++;
}
}
System.out.println("Number of zero elements = " + zero);
System.out.println("Number of non zero elements = " + nonzero);
if(zero > nonzero)
System.out.println("Matrix is a Sparse Matrix");
else
System.out.println("Matrix is not a Sparse Matrix");
}
}Output

Answered By
1 Like
Related Questions
Define a class named StepTracker with the following specifications:
Member Variables:
- String name – stores the user's name
- int sw – stores the total number of steps walked by the user.
- double cb – stores the estimated calories burned by the user.
- double km – stores the estimated distance walked in kilometers.
Member Methods:
- void accept() – to input the name and the steps walked using Scanner class methods only.
- void calculate() – calculates calories burned and distance in km based on steps walked using the following estimation table:
Metric Calculation Formula Calories Burned steps walked x 0.04 (e.g., 1 step burns 0.04 calories) Distance (Km) steps walked / 1300 (e.g., 1300 steps is approx. 1 km) - void display() – Display the calories burned, distance in km and the user's name.
Write a main method to create an object of the class and invoke the methods.
Write a program to accept the designations of 100 employees in a single dimensional array. Accept the designation from the user and print the total number of employees with the designation given by the user as input.
Example:
Trainee Manager Chef Manager Director Manager Input: Manager Output: 3
Write a program to accept a number and check if it is a Mark number or not. A number is said to be Mark when sum of the squares of each digit is an even number as well as the last digit of the sum and the last digit of the number given is same.
Example: n = 246
sum = 2 x 2 + 4 x 4 + 6 x 6 = 56
56 is an even number as well as last digit is 6 for both sum as well as the number.Define a class to overload the method format as follows:
void format(): To print the following pattern using Nested for loops only.
1 2 3 4 5 2 3 4 5 3 4 5 4 5 5int format(String s): To calculate and return the sum of ASCII codes of each character of the String.
Example: CAB
Output: 67 + 65 + 66
198void format(int n): To calculate and display the sum of natural numbers up to n given by the formula.