-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_1s_Analyzer.cpp
64 lines (54 loc) · 1.65 KB
/
Matrix_1s_Analyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cstdlib> // For random number generation
#include <ctime> // For seeding the random number generator
using namespace std;
int main() {
// Seed the random number generator (improves randomness)
srand(time(0));
// Define the matrix size
const int rows = 4;
const int cols = 4;
// Declare the matrix
int matrix[rows][cols];
// Fill the matrix with random 0s and 1s
cout << "Matrix:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = rand() % 2; // Generate 0 or 1 randomly
cout << matrix[i][j] << " ";
}
cout << endl;
}
// Find the row with the most 1s
int maxRowOnes = 0, maxRowIndex = 0;
for (int i = 0; i < rows; i++) {
int rowOnes = 0;
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 1) {
rowOnes++;
}
}
if (rowOnes > maxRowOnes) {
maxRowOnes = rowOnes;
maxRowIndex = i;
}
}
// Find the column with the most 1s
int maxColOnes = 0, maxColIndex = 0;
for (int j = 0; j < cols; j++) {
int colOnes = 0;
for (int i = 0; i < rows; i++) {
if (matrix[i][j] == 1) {
colOnes++;
}
}
if (colOnes > maxColOnes) {
maxColOnes = colOnes;
maxColIndex = j;
}
}
// Print the results
cout << "The largest row index: " << maxRowIndex << endl;
cout << "The largest column index: " << maxColIndex << endl;
return 0;
}