0

I have a problem with printing 2d array. After allocating memory for matrix and filling it with 0's it cannot be printed. Here is my code. I had done this before and worked well but now I cannot see the problem.

int _tmain(int argc, _TCHAR* argv[])
{
int m, n, num;
float **tab1 = NULL;
for (;;)
{

    printf("\n1. Nowy budynek");
    printf("\n2. Wyswietl moc pobierana w pomieszczeniach");
    printf("\n3. Wlacz swiatlo");
    printf("\n4. Wylacz swiatlo");
    printf("\n0. Exit\n");
    scanf_s("%d", &num);

    if (num == 1)
    {
        do{
            printf("Podaj liczbe kondygnacji: ");
            scanf_s("%d", &m);
        } while (m < 0);
        do{
            printf("Podaj liczbe pomieszczen: ");
            scanf_s("%d", &n);
        } while (n < 0);
        tworzenie(m, n);
    }
    for (int i = m - 1; i >= 0; i--)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%10.2f", tab1[i][j]);
        }
        printf("\n");
    }
    if (num == 2)
    {
        if (tab1 == NULL)
        {
            printf("\n Brak budynku! Stworz nowy.\n");
        }
        else
        wyswietlanie(tab1, m, n); <- it crashes here.
    }
    if (num == 3)
    {
        if (tab1 == NULL)
        {
            printf("\n Brak budynku! Stworz nowy.\n");
        }
        else
            wlaczanie(tab1, m, n);
    }
    if (num == 4)
    {
        if (tab1 == NULL)
        {
            printf("\n Brak budynku! Stwórz nowy.");
        }
        //else
        //wylaczanie(tab1,m, n);
    }
    if (num == 0)
    {
        //      exit(tab1,m, n);
    }
}


return 0;

}

Here is creating the table:

float** tworzenie(int m, int n)
{
    float **tab1;

    tab1 = (float**)malloc(m * sizeof(float*));
    for (int i = 0; i < n; i++)
    {
        tab1[i] = (float*)malloc(n * sizeof(float));
    }


    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            tab1[i][j] = 0;
        }
    }
    return tab1;
}

and here, printing(upside down):

void wyswietlanie(float **tab1, int m, int n)
{
for (int i = m - 1; i >= 0; i--)
{
    for (int j = 0; j < n; j++)
    {
        printf("%10.2f", tab1[i][j]);
    }
    printf("\n");
}
 }
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
balerofon
  • 1
  • 1

1 Answers1

2

This is the main problem

if (num == 1)
{
    do{
        printf("Podaj liczbe kondygnacji: ");
        scanf_s("%d", &m);
    } while (m < 0);
    do{
        printf("Podaj liczbe pomieszczen: ");
        scanf_s("%d", &n);
    } while (n < 0);
    /* tworzenie(m, n); this is wrong, must be */
    tab1 = tworzenie(m, n);
}

Also,

tab1 = (float**)malloc(m * sizeof(float*));
for (int i = 0; i < n; i++)
{
    tab1[i] = (float*)malloc(n * sizeof(float));
}

you malloced m float * poitners, and iterate through n, change it to

tab1 = (float**)malloc(m * sizeof(float*));
for (int i = 0; i < m; i++)
{
    tab1[i] = (float*)malloc(n * sizeof(float));
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97