C Online Compiler
Example: Check Specific Row for All 1s in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Check Specific Row for All 1s #include <stdio.h> #include <stdbool.h> // For boolean data type #define ROWS 3 #define COLS 3 // Function to check if a specific row has all 1s bool checkRowForAllOnes(int matrix[ROWS][COLS], int row_index) { if (row_index < 0 || row_index >= ROWS) { printf("Error: Invalid row index.\n"); return false; // Or handle error appropriately } for (int j = 0; j < COLS; j++) { if (matrix[row_index][j] == 0) { return false; // Found a 0, so not all 1s } } return true; // All elements were 1s } int main() { // Step 1: Define a sample boolean matrix int booleanMatrix[ROWS][COLS] = { {1, 1, 1}, {0, 1, 0}, {1, 0, 1} }; // Step 2: Print the matrix for verification 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: Check a specific row (e.g., row 0) int targetRow = 0; if (checkRowForAllOnes(booleanMatrix, targetRow)) { printf("Row %d has all 1s.\n", targetRow); } else { printf("Row %d does not have all 1s.\n", targetRow); } // Step 4: Check another row (e.g., row 1) targetRow = 1; if (checkRowForAllOnes(booleanMatrix, targetRow)) { printf("Row %d has all 1s.\n", targetRow); } else { printf("Row %d does not have all 1s.\n", targetRow); } return 0; }
Output
Clear
ADVERTISEMENTS