I am facing a question in which I am required to create a function that gets a 2D array and it's size and it should return another 2D array which is basically the same one but half rows size and half columns size and each group of arrays depending on the size of the original matrix will be pasted next to each other, example:
https://i.stack.imgur.com/CtLre.jpg
image of the faulty output i am getting : https://i.stack.imgur.com/qp03n.jpg
it keeps giving me trash value after the second matrix paste for some reason and i dont know why :/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
long long Power(long long C1, long long C2)
{
int Digit = 0, i = 0;
long long Flag, DigitCount=0, Multiplier = 1;
Flag = C2;
while (Flag != 0)
{
Digit = Flag % 10;
DigitCount++;
Flag = Flag / 10;
}
while (i < DigitCount)
{
Multiplier = Multiplier * 10;
i++;
}
long long Final = 0;
Final = (Multiplier * C1) + C2;
return Final;
}
long long** shrink(long long** Matrix, int size, int* pSize)
{
if (size % 2 != 0)
{
return 0;
}
*pSize = size / 2;
long long A, B, C;
long long **New_Matrix = 0;
New_Matrix = (long long**)malloc(*pSize * sizeof(long long*));
for(int i=0; i<*pSize; i++)
{
New_Matrix[i] = (long long*)malloc(*pSize * sizeof(long long));
for (int j = 0; j < *pSize; j++)
{
A = Power(Matrix[2 * i][2 * j], Matrix[2 * i][2 * j + 1]);
B = Power(A, Matrix[2 * i + 1][2 * j]);
C = Power(B, Matrix[2 * i + 1][2 * j + 1]);
}
}
return New_Matrix;
}
int main()
{
long long** Matrix = 0;
int size;
int *pSize;
long long** result=0;
printf("Size Insertion : \n");
scanf("%d", &size);
Matrix = (long long**)malloc(size * sizeof(long long*));
printf("Matrix Insertion : \n");
for (int i = 0; i < size; i++)
{
Matrix[i] = (long long*)malloc(size * sizeof(long long));
for (int j = 0; j < size; j++)
{
scanf("%lld", &Matrix[i][j]);
}
}
printf("Matrix Display : \n");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%lld ", Matrix[i][j]);
}
printf("\n");
}
result = shrink(Matrix, size, &pSize);
for (int i = 0; i < pSize; i++)
{
for (int j = 0; j < pSize; j++)
{
printf("%lld ", result[i][j]);
}
printf(" \n");
}
free(result, Matrix);
return 0;
}