I 've to do the following exercise:
Write a function that given two integers (x and k ) and a M X N matrix, returns TRUE if there is an element that occurs at least k times in at least x columns of the matrix.
this is my solution but there is something wrong:
#include <iostream>
using namespace std;
const int DIM = 3;
bool check ( int matrix[DIM][DIM], int element, int k, int x )
{
bool occur = false;
int i,j = 0;
for ( i=0; i<DIM; i++ )
{
for ( j=0; j<DIM; j++)
{
while ( i<k && occur)
{
matrix[i][j] == element;
i++;
}
}
}
return occur;
}
int main ()
{
int matrix[DIM][DIM] = {{1,2,3},
{4,1,6},
{7,8,9}};
int x = 2;
int k = 1;
int elemento = 1;
if ( check (matrix, element, k , x))
{
cout << "l'elemento "<< elemento <<" ricorre "<< k <<" volta/e in "<< x <<" colonna/e";
}
return 0;
}
occur = truei < kandmatrix[i][j] == element;seems incorrect. Also what mert said.