0

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;
}
Dark
  • 29
  • 5
  • so what is the question exactly? – Andrey Markeev Jan 01 '21 at 20:31
  • when i try to run it it works untill the second matrix is about to be printed it prints it out as a trash value – Dark Jan 01 '21 at 20:36
  • if there is something unclear i would be happy to explain it , i am still new to this and trying my best ! – Dark Jan 01 '21 at 20:41
  • 1
    I am suspicious of your double pointer - I would try printing the matrix to the screen at the beginning of the `shrink` routine to see if it is behaving the way that you want. – tom Jan 01 '21 at 20:46
  • 1
    *I am required to create a function that gets a 2D array ...* No, your function is getting a **one-dimensional** array of pointers, each one pointing to another, completely separate, **one-dimensional** array. See [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) People actually ***teaching*** C should know better. – Andrew Henle Jan 01 '21 at 20:49

1 Answers1

0
 result = shrink(Matrix, size, &pSize)

Here you're passing the address of the pSize pointer to the function, which when dereferenced, returns the underlying pointer. So the line *pSize = size / 2; assigns the value to the underlying pointer which is simply wrong. Pointers only hold memory addresses. You may probably have meant to have written this: **pSize = size / 2; You should dereference once to get the underlying pointer, twice to get to the variable it points to.

Also, you need to dereference pSize in the for loops as well to get the size value set by the function

for (int i = 0; i < *pSize; i++)
    {
        for (int j = 0; j < *pSize; j++)
        {
            printf("%lld ", result[i][j]);
        }
        printf(" \n");
    }

Also, pSize is an empty pointer, it points to an undefined memory address. Make sure that's not the case:

int * pSize = (int *) malloc(sizeof (int));
// rest of code
free(pSize);
Vahan
  • 166
  • 13