Ganesh Deshmukh
Ganesh Deshmukh

Reputation: 21

Why am I getting Output Element Not Found Even If Element is Present in Matrix?

Following Code gives output Found if element is present in first row. When I tried to search element which is present in remaining rows, the output is Not Found. Please help me to solve this problem.

Session

Enter size
rows:2

columns:2
1 2 3 4

Enter Target:3
1       2
3       4

Not Found

Expected Output

Found

Here is the code:

#include <stdio.h>

void Search(int n, int m, int matrix[][n], int target) {
    int i, j;
    while (i < m) {
        j = 0;
        while (j < n) {
            if (target == matrix[i][j]) {
                printf("Found");
                return;
            }
            j++;    
        }
        i++;
    }
    printf("\nNot Found");
}

void main() {
    int matrix[5][5];
    int rows, col, target;
    printf("Enter size\nrows:");
    scanf("%d", &rows);
    printf("\ncolumns:");
    scanf("%d", &col);
    
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < col; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    
    printf("\nEnter Target:");
    scanf("%d", &target);
    
    Search(col, rows, matrix, target);
}

Upvotes: 1

Views: 70

Answers (1)

chqrlie
chqrlie

Reputation: 144740

There are multiple problems in the code:

  • the index variable i must be initialized to 0 before the start of the outer loop of the Search function. It is less error prone to use for loops for this instead of while loops.

  • The output posted for the session is not consistent with the posted code. The matrix contents is shown but there is no code to produce this output.

  • The prototype for main should be int main(void) or int main(int argc, char *argv[]) or equivalent. void is not a proper return type for this function.

  • The matrix passed to Search has a size of 5 rows and 5 columns, but you tell the function a different size: 2 rows and 2 columns, passed as arguments and the C99 prototype tells the compiler that the number of columns is n with a value of 2. This is causing the problem. You should either define Search as void Search(int n, int m, int matrix[][5], int target) or define matrix with the proper sizes.

Here is a modified version:

#include <stdio.h>

void Search(int n, int m, int matrix[][n], int target) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (target == matrix[i][j]) {
                printf("Found\n");
                return;
            }
        }
    }
    printf("Not Found\n");
}

int main(void) {
    int rows, cols, target;

    printf("Enter size\nrows: ");
    if (scanf("%d", &rows) != 1 || rows <= 0)
        return 1;

    printf("\ncolumns: ");
    if (scanf("%d", &cols) != 1 || cols <= 0)
        return 1;

    int matrix[rows][cols];
    
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (scanf("%d", &matrix[i][j]) != 1)
                return 1;
        }
    }
    
    printf("\nEnter Target:");
    if (scanf("%d", &target) != 1)
        return 1;

    Search(cols, rows, matrix, target);
    return 0;
}

Upvotes: 2

Related Questions