C Online Compiler
Example: Check Specific Column for All 0s in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Check Specific Column for All 0s #include <stdio.h> #include <stdbool.h> #define ROWS 3 #define COLS 4 // Function to check if a specific column has all 0s bool checkColForAllZeros(int matrix[ROWS][COLS], int col_index) { if (col_index < 0 || col_index >= COLS) { printf("Error: Invalid column index.\n"); return false; } for (int i = 0; i < ROWS; i++) { // Iterate through rows for a fixed column if (matrix[i][col_index] == 1) { return false; // Found a 1, so not all 0s } } return true; // All elements were 0s } int main() { // Step 1: Define a sample boolean matrix int booleanMatrix[ROWS][COLS] = { {1, 0, 1, 0}, {0, 0, 0, 0}, {1, 0, 1, 0} }; // 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: Check a specific column (e.g., column 1) int targetCol = 1; if (checkColForAllZeros(booleanMatrix, targetCol)) { printf("Column %d has all 0s.\n", targetCol); } else { printf("Column %d does not have all 0s.\n", targetCol); } // Step 4: Check another column (e.g., column 2) targetCol = 2; if (checkColForAllZeros(booleanMatrix, targetCol)) { printf("Column %d has all 0s.\n", targetCol); } else { printf("Column %d does not have all 0s.\n", targetCol); } return 0; }
Output
Clear
ADVERTISEMENTS