|
1 |
| -/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2, |
2 |
| -where m and n are the dimensions of the matrix.*/ |
| 1 | +/** @file |
| 2 | + * A sparse matrix is a matrix which has number of zeroes greater than |
| 3 | + * \f$\frac{m*n}{2}\f$, where m and n are the dimensions of the matrix. |
| 4 | + */ |
| 5 | + |
3 | 6 | #include <iostream>
|
4 |
| -using namespace std; |
5 | 7 |
|
6 |
| -int main() |
7 |
| -{ |
8 |
| - int m, n; |
9 |
| - int counterZeros = 0; |
10 |
| - cout << "Enter dimensions of matrix (seperated with space): "; |
11 |
| - cin >> m >> n; |
12 |
| - int a[m][n]; |
13 |
| - cout << "Enter matrix elements:"; |
14 |
| - cout << "\n"; |
| 8 | +int main() { |
| 9 | + int m, n; |
| 10 | + int counterZeros = 0; |
| 11 | + |
| 12 | + std::cout << "Enter dimensions of matrix (seperated with space): "; |
| 13 | + std::cin >> m; |
| 14 | + std::cin >> n; |
| 15 | + |
| 16 | + int a[m][n]; |
| 17 | + std::cout << "Enter matrix elements:"; |
| 18 | + std::cout << "\n"; |
15 | 19 |
|
16 |
| - // reads the matrix from stdin |
17 |
| - for (int i = 0; i < m; i++) |
18 |
| - { |
19 |
| - for (int j = 0; j < n; j++) |
20 |
| - { |
21 |
| - cout << "element? "; |
22 |
| - cin >> a[i][j]; |
| 20 | + // reads the matrix from stdin |
| 21 | + for (int i = 0; i < m; i++) { |
| 22 | + for (int j = 0; j < n; j++) { |
| 23 | + std::cout << "element? "; |
| 24 | + std::cin >> a[i][j]; |
| 25 | + } |
23 | 26 | }
|
24 |
| - } |
25 | 27 |
|
26 |
| - // counts the zero's |
27 |
| - for (int i = 0; i < m; i++) |
28 |
| - { |
29 |
| - for (int j = 0; j < n; j++) |
30 |
| - { |
31 |
| - if (a[i][j] == 0) |
32 |
| - counterZeros++; //Counting number of zeroes |
| 28 | + // counts the zero's |
| 29 | + for (int i = 0; i < m; i++) { |
| 30 | + for (int j = 0; j < n; j++) { |
| 31 | + if (a[i][j] == 0) counterZeros++; // Counting number of zeroes |
| 32 | + } |
33 | 33 | }
|
34 |
| - } |
35 | 34 |
|
36 |
| - // makes sure the matrix is a sparse matrix |
37 |
| - if (counterZeros > ((m * n) / 2)) //Checking for sparse matrix |
38 |
| - cout << "Sparse matrix"; |
39 |
| - else |
40 |
| - cout << "Not a sparse matrix"; |
| 35 | + // makes sure the matrix is a sparse matrix |
| 36 | + if (counterZeros > ((m * n) / 2)) // Checking for sparse matrix |
| 37 | + std::cout << "Sparse matrix"; |
| 38 | + else |
| 39 | + std::cout << "Not a sparse matrix"; |
41 | 40 | }
|
0 commit comments