finding a Kaprekar numbers inside a 2D array in C - c

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;
}

You have 2D array, you have also the function that verifies the number is Kaprekar or not. The simple way is using the for loop to check all number in 2D array as you did when you initialize the values of array using rand() function.
for(s = 0; s < row; s++) {
for(k = 0; k < column; k++) {
if(iskaprekar(matrix[s][k])) {
printf("%d ", matrix[s][k]);
}
}
}
if you want to store all Kaprekar numbers, you can use pointer then reallocate for it after each time you meet one Kaprekar number. Then remove the duplicated numbers if you want.
int * numbers = 0;
if(!numbers) {return -1;}
int count = 0;
for(s = 0; s < row; s++) {
for(k = 0; k < column; k++) {
if(iskaprekar(matrix[s][k])) {
numbers = realloc(numbers, sizeof(int) (count+1));
if(!numbers) {return -1;}
numbers[count] = matrix[s][k];
count++;
}
}
}
// Remove the duplicated numbers here, if you want

Related

Sorting a matrix

Im trying to sort a matrix by the sum of its row's digits, from highest to lowest. I dont know if i explained that correctly so here's some photos explaining it.
This is what my code outputs. Basically, it asks you for m and n, which are the dimensions of the matrix. In this example it's a 3x4, 3 rows and 4 columns. Then, the matrix should be sorted by rows, by the sum of row's digits. Which means, instead of what's being outputted in the picture above, the correct result should be this:
I have no idea how to sort this from highest to lowest, i have been trying for hours to no avail.
Here's my code:
#include <stdio.h>
#define N 30
void main(){
double a[N][N], s[N], p;
int i, j, m, n, max;
while(1){
printf("\nm, n? ");
scanf("%d%d", &m, &n);
if(m <= 0 || m > N || n <=0 || n > N)
break;
for(i = 0; i < m; i++){
printf("%2d. row? ", i+1);
for(j = 0; j < n; scanf("%lf", &a[i][j++]));
}
for(i = 0; i < m; i++)
for(s[i] = j = 0; j < n; s[i] += a[i][j++]);
for(j = 0; j < n - 1; j++){
for(max = i, j = i+1; j < n; j++)
if(s[j] > s[max])
max = i;
if(max != j){
p = s[j];
s[j] = s[max];
s[max] = p;
for(j = 0; j < m; j++){
p = a[j][i];
a[j][i] = a[j][max];
a[j][max] = p;
}
}
}
printf("New matrix: \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for(j = 0; j < m; j++)
printf("-------------");
printf("\n");
for(j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
}
You can sort the rows of the matrix from highest to lowest, using a simple bubble sort algorithm.Your code modified below:
int main() {
double a[N][N], s[N], p;
int i, j, m, n, max;
while (1) {
printf("\nm, n? ");
scanf("%d%d", & m, & n);
if (m <= 0 || m > N || n <= 0 || n > N)
break;
for (i = 0; i < m; i++) {
printf("%2d. row? ", i + 1);
for (j = 0; j < n; scanf("%lf", & a[i][j++]));
}
for (i = 0; i < m; i++)
for (s[i] = j = 0; j < n; s[i] += a[i][j++]);
for (i = 0; i < m - 1; i++) { // modified here
for (j = i + 1; j < m; j++) { // modified here
if (s[j] > s[i]) { // modified here
p = s[i];
s[i] = s[j];
s[j] = p;
for (int k = 0; k < n; k++) {
p = a[i][k];
a[i][k] = a[j][k];
a[j][k] = p;
}
}
}
}
printf("New matrix: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for (j = 0; j < m; j++)
printf("-------------");
printf("\n");
for (j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
return 0;
}
Here's how i modified your code to achieve that:
Initialize a loop variable i to 0.
In the outer loop, run the inner loop j from i+1 to m-1.
In the inner loop, compare the sum of the row i with the sum of row
j. If the sum of row j is greater than the sum of row i, swap the
rows using a temporary variable.
After the inner loop finishes, increment the value of i by 1. Repeat
the outer loop until i becomes equal to m-1.
Output:
You can just use qsort to let it handle the sorting and item swapping. Then you only need to write the code for comparing two rows with each other.
Given something like this:
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
You'd call qsort as:
qsort(matrix, 3, sizeof(int[4]), compare);
The only complexity is implementing the comparison callback function. There's two things to consider there:
We've told qsort that we have an array of 3 items, each of type int[4]. So the void pointers it passes along to us will actually be pointers to type int[4]. That is: int(*)[4].
qsort sorts in ascending order by default, where the item considered "less" ends up first. So we need to tweak that to get the largest item first.
Example:
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
sum1 < sum2 would have placed the smallest row first.
Full example:
#include <stdio.h>
#include <stdlib.h>
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
void print_matrix(size_t col, size_t row, int matrix[col][row])
{
for(size_t i=0; i<col; i++)
{
for(size_t j=0; j<row; j++)
{
printf("%d,", matrix[i][j]);
}
puts("");
}
}
int main (void)
{
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
print_matrix(3,4,matrix);
puts("");
qsort(matrix, 3, sizeof(int[4]), compare);
print_matrix(3,4,matrix);
}

Arranging columns in a matrix lexicographically

I've been trying to sort columns in a matrix (the dimensions are m,n <= 10) via the lexicographical order (if the columns share the same element, then we compare the elements in the row beneath etc.) with some additional conditions. I need to use functions to print the matrix, input random integers up to 5 as its elements and finally arrange the matrix. I think I got the printing and random inputs correctly but I can't figure out the sorting. Plus I can't use global variables which I have no idea how to do, since I haven't been shown. An example matrix would be :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
int mat[10][10];
void print_matrix()
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (mat[i][j] < mat[k][j])
{
a = mat[i][j];
mat[i][j] = mat[k][j];
mat[k][j] = a;
}
}
}
}
}
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int(mat[m][n]);
print_matrix(mat[m][n]);
arrange(mat[m][n]);
print_matrix(mat[m][n]);
return 0;
}
Try this solution(will work for input 0-8 only), also used global variables:
There have multiple solutions. but is the easiest one.
I have converted each of the columns as an integer value. then bubble sorted the integers. After sorting. I have then converted the integer value to digits. (You have to know how to convert individual digits to multiple digit integer and multiple digit integers to single-digit.
Note I have added one(1) with each digit. Because the input can be zero(0). if you convert 0 0 2 1 to an integer will be only 21. the first two digits lost. So I have added 1. so 0 0 2 1 will be converted to 1132. I have done (added 1) for each input(deducted 1 after sorting). so it will not affect other inputs. Be careful input have to be from(0 to 8)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int m, n;
int mat[10][10];
int generatedNumber[10];
void print_matrix()
{
printf("The matrix is:\n");
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
int number = 0;
for (i = 0; i < m; ++i)
{
number = number * 10 + mat[i][j] + 1;///Adding one for avoiding zero(0)
}
generatedNumber[j] = number;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( generatedNumber[j] > generatedNumber[j+1])
{
// swap the elements
int temp = generatedNumber[j];
generatedNumber[j] = generatedNumber[j+1];
generatedNumber[j+1] = temp;
}
}
}
for(i = 0; i < n; i++)///columwise
{
int generatedColumnvalue = generatedNumber[i];
for(j = m -1; j>= 0; j--)///row wise and fro last vaue to first
{
mat[j][i] = (generatedColumnvalue%10)-1;///removing extra added 1
generatedColumnvalue/=10;
}
}
}
int main()
{
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int();
print_matrix();
arrange();
print_matrix();
return 0;
}

Bubble sort 2D array in C

I need a function to bubble sort this randomly generated 2D array.
Also with rand() method I wanted it to generate numbers between (1, 1000000) but it doesnt give the required range, any suggestion to find out a solution?
int **matrix()
{
int **matrix;
int row, column;
long s, k;
int i,j,f,swap;
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()%10000000;
}
}
for(s = 0; s < row; s++)
{
for(k = 0; k < column; k++)
printf("%4d \t\t", matrix[s][k]);
printf("\n");
}
for(i = 0; i < row; i++)
free((void *) matrix[i]);
free((void *) matrix);
return **matrix;
}
Sorting a 2D array with bubble sort is a little different that sorting a single dimensional array. This example may help with that part of your question.
Other issues:
Based on this section:
for(i = 0; i < row; i++)
matrix[i] = (int *) calloc(column, sizeof(int));
The line in the preceding section:
matrix = (int **) calloc(row, sizeof(int));
^
Should be allocating memory for int *
matrix = calloc(row, sizeof(int *));
^
(Note the cast for calloc() has also been removed. In C casting the return of [c][m][re]alloc is not recommended.)
Also with rand() method I wanted it to generate numbers between (1, 1000000) but it doesnt give the required range, any suggestion to find out a solution?
( With credit to this answer )
rand() expanded to provide a pseudo random distribution of 1000000 unique values:
unsigned long rand_ex(void);
int main(void){
srand(clock());
for(int i=0;i<100;i++)
{
printf("%10d: %10lu\n", i, rand_ex());
}
return 0;
}
unsigned long rand_ex(void)
{
unsigned long x;
x = rand();
x <<= 15;
x ^= rand();
x %= 1000001;
return x;
}

Finding duplicate value in a row (2D array)

I would like to know how to find duplicate values in the 1st row of my 2d array.
I thought that by setting array[0][0] == array[i][j], it would check if the array[0][0] equals to the number of array[0][rest of the column]. But my code is just popping up my try again message whenever I put my first value.
Here's what I've tried so far.
void main(void)
{
int array[2][5];
int i, j, l, k;
printf("\Please enter 10 values\n");
for (j = 0; j < 5; j++)
{
for (i = 0; i < 2; i++)
{
scanf("%i", &array[i][j]);
for (k = 0; k < 2; k++)
{
for (l = 0; l < 5; l++)
{
while (array[0][0] == array[i][j])
{
printf("You entered 2 identical numbers in the first row, try again:\n");
scanf("%i", &array[i][j]);
}
}
}
}
}
}
// this isn't the fastest algorithm but it works because of the small length
int check_duplicates(int arr[], int len) {
// iterate through the array
for (int i = 0; i < len; i++) {
// only need to check to the right
// since the left elements have been checked previously
for (int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
// there's a duplicate, return
return 1;
}
}
}
// no duplicates found
return 0;
}
int main(void) {
int array[2][5];
int i, j, l, k;
printf("Please enter 10 values\n");
for (j = 0; j < 5; j++) {
for (i = 0; i < 2; i++) {
scanf("%i", &array[i][j]);
// a duplicate has been found
if (check_duplicates(array[0], j + 1)) {
printf("You entered a duplicate, try again.\n");
// undo one loop to read back into that position
i --;
}
}
}
return 0;
}

Product of numbers, not printing right number

I have a 2d array (matrix) where I am trying to calculate the biggest product of adjacent numbers. It's very similar to Project Euler Problem 11 except the user enters how many adjacent numbers they want in the calculation. I have it all right I think. The problem is if i use integer's to calculate the product of say 5 integers of 99 (e.g. 99*99*99*99*99) it will not display right. The maximum number of adjacent numbers which can be checked is 99. I have tried changing to long doubles (as you can see in the code) but it prints ridiculous numbers, e.g. with 3 adjacent numbers I entered 99 in all matrix positions and should get back 970299 (which I do when the maxProduct is an int), but instead I get:
-497917511184158537131936752181264370659584929560826523880745083032965215342755650440802286656251727430041200624372430370294634536699364412350122489510814753628581807006780156992324264734484592980976635224618682514265787653963930812412392499329499188301075222828863209569131692032
I feel like it's something obvious but I just can't see it.
#include <stdio.h>
#include <stdlib.h>
long double calcProduct(int n, int m, int ** matrix)
{
int i, x, y; //Loop counters
long double maxProduct; //Used to hold the maximum product of numbers found so far
long double temp; //Used to hold the current product of numbers
//Searching left to right
for(y = 0; y < n; y++)
{
for(x = 0; x <= n - m; x++)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x + i][y];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
//Searching top down
for(x = 0; x < n; x++)
{
for(y = 0; y <= n - m; y++)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x][y + i];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
temp = 1;
//Searching diagonal down right
for(x = 0; x < n - m; x++)
{
for(y = 0; y <= n - m; y++)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x + i][y + i];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
temp = 1;
//Searching diagonal up right
for(x = 0; x < n - m; x++)
{
for(y = n - 1; y >= m - 1; y--)
{
for(i = 0; i < m; i++)
{
temp *= matrix[x + i][y - i];
}
if(temp > maxProduct)
{
maxProduct = temp;
}
temp = 1;
}
}
return maxProduct;
}
main()
{
int ** matrix; //2D array to hold the matrix items
int n, m; //Used to hold the size of the matrix (n) and the number of adjacent numbers to include in the calculation (m)
int i, j; //Loop counters
//Taking input of n (for size of grid) and m (number of adjacent numbers to include in calculation)
scanf("%d %d", &n, &m);
//Assign the array 'matrix' with the size of int multiplied by the number of items to hold (n)
matrix = (int **)malloc(sizeof(int*)*n);
//If the matrix is null then exit the program
if (matrix == NULL)
{
exit (0);
}
for(i = 0; i < n; i++)
{
matrix[i] = (int *)malloc(sizeof(int)*n);
if(matrix[i] == NULL) exit (0);
}
//Getting the numbers which are held in the matrix
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &matrix[i][j]);
}
}
//Prints the highest product by calling the method calcProduct, giving it n, m and the array matrix
printf("%.0Lf", calcProduct(n, m, matrix));
}
temp is not initialized in calcProduct, and neither is maxProduct. They will contain random, garbage values the first time through the loop, which is corrupting your maxProduct result.
'matrix is of type 'int'-Array, but your scanf() 'doubles' into it

Resources