Skip to content

Commit 2def9ab

Browse files
committed
cin accepts only one variable at a time & fixed cpplint
1 parent 139964d commit 2def9ab

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

others/sparse_matrix.cpp

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
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+
36
#include <iostream>
4-
using namespace std;
57

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";
1519

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+
}
2326
}
24-
}
2527

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+
}
3333
}
34-
}
3534

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";
4140
}

0 commit comments

Comments
 (0)