C Online Compiler
Example: Find All Rows with All 1s in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find All Rows with All 1s #include <stdio.h> #include <stdbool.h> #define ROWS 4 #define COLS 3 // Helper function to check if a row has all 1s bool isRowAllOnes(int matrix[ROWS][COLS], int row_index, int num_cols) { for (int j = 0; j < num_cols; j++) { if (matrix[row_index][j] == 0) { return false; } } return true; } int main() { // Step 1: Define a sample boolean matrix int booleanMatrix[ROWS][COLS] = { {1, 1, 1}, {0, 1, 0}, {1, 1, 1}, {1, 0, 1} }; // Step 2: Print the matrix printf("Matrix:\n"); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { printf("%d ", booleanMatrix[i][j]); } printf("\n"); } // Step 3: Iterate through all rows and check the property printf("\nRows with all 1s:\n"); bool found_any_row = false; for (int i = 0; i < ROWS; i++) { if (isRowAllOnes(booleanMatrix, i, COLS)) { printf(" Row %d\n", i); found_any_row = true; } } // Step 4: Report if no such row was found if (!found_any_row) { printf(" No rows found with all 1s.\n"); } return 0; }
Output
Clear
ADVERTISEMENTS