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.