I need a function to find out the Kaprekar numbers inside this 2d array, I searched in web but none of the results worked for 2D array.
This is the array that I made:
int **matrix;
int row, column;
long s, k;
srand(time(NULL));
printf("Number of rows: ");
scanf("%d", &row);
printf("Number of column: ");
scanf("%d", &column);
matrix = (int **) calloc(row, sizeof(int*));
for(i = 0; i < row; i++)
matrix[i] = (int *) calloc(column, sizeof(int));
for(s = 0; s < row; s++)
{
for(k = 0; k < column; k++)
{
matrix[s][k]=(rand()%1000) * (rand()%1000);
}
}
any help or suggestions to convert this code to be able to work for 2D array?
bool iskaprekar(int n)
{
if (n == 1)
return true;
int sq_n = n * n;
int count_digits = 0;
while (sq_n)
{
count_digits++;
sq_n /= 10;
}
sq_n = n*n;
for (int r_digits=1; r_digits<count_digits; r_digits++)
{
int eq_parts = pow(10, r_digits);
if (eq_parts == n)
continue;
int sum = sq_n/eq_parts + sq_n % eq_parts;
if (sum == n)
return true;
}
return false;
}