Problem searching for a value in a matrix

Viewed 35

I wanted to make a function that checks if a value exists in a matrix or not by giving (the matrice + the value I want to check + the line) as parameters and I'm getting an error on the transtype from an integer to a pointer. Please I need a help as soon as possible, thank you:)

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

bool Search(int *Mat[4][4], int val, int numLigne) {
    int i;

    for (i = 0; i <= 4; ++i) {
        if (Mat[numLigne][i] == val)
            return 1;
        else
            return 0;
    }
}

int main() {
    int Mat[4][4] = {
        { 1, 2, 3, 4 },
        { 5, 6, 7, 8 },
        { 9, 10, 11, 12 },
        { 13, 14, 15, 16 }
    };
    int p;
    p = Search(Mat[4][4], 5, 1);
    printf("The number is: %d", p);

    return 0;
}
2 Answers

Plenty problems

  1. int *Mat[4][4] it is the array of pointers to int. Not the pointer to array. You want int Mat[4][4] here

  2. Search(Mat[4][4], you do not pass the pointer to the function only the integer value which is taken outside the array bounds. 2 UBs in one call. You want Search(Mat, here

  3. for( i=0; i<=4; ++i ) it is wrong as indexes in your array are from 0 to 3, and you integrate to 4. You want for( i=0; i<4; ++i )

  4. This code

    for( i=0; i<=4; ++i )
    {
        if(Mat[numLigne][i] == val  )
            return 1;
        else
            return 0;
    }

is equivalent to:

        if(Mat[numLigne][0] == val  )
            return 1;
        else
            return 0;

You will stop on the index 0 as you return on both conditions.

I think you need to read the pointer and array chapters of your favourite C book. You need to read about iterations, ifs and functions.

There are multiple problems in your code:

  • the prototype for Search should be bool Search(int Mat[4][4], int value, int row); or simply bool Search(int Mat[][4], int value, int row);. as posted you declare Mat to be a matrix of pointers to int.
  • the loop for (i = 0; i <= 4; ++i) should stop when i == 4, hence the test should be i < 4.
  • if the value if not found in the first column, you immediately return 0 without testing the other columns.
  • passing the matrix to the function is written Search(Mat, 5, 1). As posted, you attempt to pass the value of a matrix element outside the matrix.
  • Search returns a boolean indicating if the row contains the value, but the column number is not returned. It would be better to return the column number and return -1 if the value is not found.

Here is a modified version:

#include <stdio.h>

int Search(int Mat[4][4], int val, int row) {
    for (int i = 0; i < 4; ++i) {
        if (Mat[row][i] == val)
            return i;
    }
    return -1;  // not found
}

int main() {
    int Mat[4][4] = {
        { 1, 2, 3, 4 },
        { 5, 6, 7, 8 },
        { 9, 10, 11, 12 },
        { 13, 14, 15, 16 }
    };
    int col = Search(Mat, 5, 1);
    if (col >= 0)
        printf("The value %d is at position Mat[%d][%d]\n", 5, 1, col);
    else
        printf("The value %d is not present in row %d of Mat\n", 5, 1);

    return 0;
}
Related