1

I'm trying to dynamically allocate two dimensional array N+1xN with all elements double. Then I want to put value (1.6) for each element of an array. The last step is to print this array. My function

void inic(double **t)
{
    for(int y=0;y<N;y++)
    {
        for(int x=0;x<N+1;x++)
        {
            t[x][y]=1.5;
        }
    }
}

isn't working as it should and I don't know why.

For example the (for n=3) result is:

[0][0][0][0] 
[0][0][0][0] 
[0][0][0][0] 

And I expected:

[1.6][1.6][1.6][1.6] 
[1.6][1.6][1.6][1.6] 
[1.6][1.6][1.6][1.6] 
[1.6][1.6][1.6][1.6] 

Here is my code:

#include <stdio.h>
#include <stdlib.h>
int N;

void inic(double **t)
{
    for(int y=0;y<N;y++)
    {
        for(int x=0;x<N+1;x++)
        {

            t[x][y]=1.5;

        }
    }
}

void print(double **t)
{
    for(int y=0;y<N;y++)
    {
        for(int x=0;x<N+1;x++)
        {
            printf("%i ",t[x][y]);
        }
        printf("\n");
    }
}




int main()
{
    double **t;
    N=3;
    t=malloc((N+1)*sizeof(double*));
    for(int i=0;i<N+1;i++)
    {
        t[i]=malloc(N*sizeof(double));
    }

    inic(t);
    print(t);
    printf("\n");
}
7
  • double ** is not a 2D array and cannot point to one. You can allocate a 2D array dynamically without problem, why not use it? And "my code is not working" is not a specific problem description. Commented Apr 17, 2017 at 17:25
  • In the first place, your array has (N+1).N elements, not N+1. Commented Apr 17, 2017 at 17:26
  • How do you expect it to have 1.6 if you put everywhere 1.5???? :) Commented Apr 17, 2017 at 17:27
  • geeksforgeeks.org/dynamically-allocate-2d-array-c Commented Apr 17, 2017 at 17:29
  • 3) Using pointer to a pointer Commented Apr 17, 2017 at 17:29

2 Answers 2

1

You are using incorrect conversion specifier. Try the following

printf("%lf ",t[x][y]);

Also it would be more natural to use another order of the indices that is when this statement

t=malloc((N)*sizeof(double*));

allocates N "rows" and then the N + 1 "columns" are allocated in the loop

for(int i=0;i<N;i++)
{
    t[i]=malloc( ( N + 1 )*sizeof(double));
}

In this case other functions will look like

void inic(double **t)
{
    for(int x = 0; x < N; x++)
    {
        for( int y = 0;  y < N + 1; y++ )
        {

            t[x][y]=1.5;

        }
    }
}

void print(double **t)
{
    for( int x = 0; x < N; x++ )
    {
        for( int y = 0; y < N + 1; y++)
        {
            printf("%lf ",t[x][y]);
        }
        printf("\n");
    }
}

Also you should free all the allocated memory at the end of the program.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked however I had to change: "printf("%lf ",t[x][y]);" for "printf("%f ",t[x][y]);". :D
1

try to replace t[x][y] to t[y][x]

1 Comment

also replace printf("%i ",t[y][x]); to printf("%f ",t[y][x]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.