Why I can't get only the first occurrence of the element in the matrix?

Viewed 73

I'm new to the C language, and I'm trying to make a program to perform some tasks using a matrix, so I'm working on a task to search an element of the matrix and give the position (row, column) of the element's first occurrence. Below are the functions:

   void Do_Search(Matrix M, int row, int col, int elem_search)
   {
   int i, j, aux = 0;

   for(i = 0; i < row; i++)
   {
       for(j = 0; j < col; j++)
       {
           if(M[i][j] == elem_search)
           {
           printf("\nElement first found in [%d][%d]", i, j);
           break;
           }
         }
       }
    }

    void Search_Element(Matrix M, int row, int col){
    int i, j, num_elem, elem_search, elements = row * col;

    while(num_elem > elements || num_elem < 1)
    {
    printf("\nEnter the number of elements to search >> ", element );
    scanf("%d", &num_elem);
    }

    for(i = 0; i < num_elem; i++)
    {
        printf("\n\nEnter the element to search >> ");
        scanf("%d", &elem_search);
        Do_Search(M, row, col, elem_search);
    }
}

In

           if(M[i][j] == elem_search)
           {
               printf("\nElement first found in [%d][%d]", i, j);
               break;
           }

I would like just to get the first occurrence, but the program gives one position when there are only one occurrence and two-position despite the number of elements being higher than 2.

3 Answers

The break; statement only breaks out of the inner for-loop. So, the outer (row) for-loop continues to run, and thus if the element exists in multiple rows, your function will find all of them.

You probably want to return; once you find the first element, in order to exit the function after finding a single occurrence.

 if(M[i][j] == elem_search)
 {
   printf("\nElement first found in [%d][%d]", i, j);
   return;
 }

You have nested loops. You are breaking out of only the inner loop.

So, you need a "stop" variable (e.g. found) to tell the outer loop to stop on a match:

void
Do_Search(Matrix M, int row, int col, int elem_search)
{
    int i, j, aux = 0;

    int found = 0;
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            found = (M[i][j] == elem_search);
            if (found) {
                printf("\nElement first found in [%d][%d]", i, j);
                break;
            }
        }
        if (found)
            break;
    }
}

I use a booleen variable & I initialized it to false ,if the element exist in the matrix the booleen variable initialized at true and he make Exit from the loop (while(i<row) & while(j<col))

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

void Do_Search(int row, int col,int M[row][col], int elem_search);

int main(void)
{
int rows, cols;

do
{
      printf("Give me the number of rows :");
      scanf("%d",&rows);
}while(rows<1);

do
{
      printf("Give me the number of cols :");
      scanf("%d",&cols);
}while(cols<1);

int arr[rows][cols];

printf("\nThe filling of the Matrix :\n");
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        scanf("%d", &arr[i][j]);
    }
}

printf("\nDisplay of the Matrix :\n");
for (int i = 0; i < rows; i++)
{printf("\n");
    for (int j = 0; j < cols; j++)
    {
        printf("[%d]",arr[i][j]);
    }
}

printf("\n");
Do_Search(rows,cols,arr,5);
printf("\n");

}

void Do_Search(int row, int col,int M[row][col], int elem_search)
{
int i=0, j=0, aux = 0;
bool found = false;
while(i<row&&found==false)
{j=0;
     while(j<col&&found==false)
     {
          if(M[i][j] == elem_search)
          {
                printf("\nElement first found in [%d][%d]", i, j);
                found=true;
          }
          j++;
     }
     i++;
}

}
Related