C Online Compiler
Example: Brute Force Matrix Search in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Brute Force Matrix Search #include <stdio.h> #include <stdbool.h> // For boolean type // Function to search for target in matrix using brute force bool searchMatrixBruteForce(int matrix[5][5], int rows, int cols, int target) { // Step 1: Iterate through each row for (int i = 0; i < rows; i++) { // Step 2: Iterate through each column in the current row for (int j = 0; j < cols; j++) { // Step 3: Compare current element with the target if (matrix[i][j] == target) { return true; // Target found } } } return false; // Target not found after checking all elements } int main() { int matrix[5][5] = { {1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30} }; int rows = 5; int cols = 5; int target1 = 5; int target2 = 20; // Test with target1 if (searchMatrixBruteForce(matrix, rows, cols, target1)) { printf("Target %d found in the matrix (Brute Force).\n", target1); } else { printf("Target %d not found in the matrix (Brute Force).\n", target1); } // Test with target2 if (searchMatrixBruteForce(matrix, rows, cols, target2)) { printf("Target %d found in the matrix (Brute Force).\n", target2); } else { printf("Target %d not found in the matrix (Brute Force).\n", target2); } return 0; }
Output
Clear
ADVERTISEMENTS