0

guys :) I have a 2d dynamic array and I need to find the biggest and the smallest number in every column. I have to insert 2 new lines (for max and min) in my array but it seems that my realloc is not working fine. Please, tell me what I am doing wrong and how I should continue.

int **in(int l,int c);
void out(int l, int c, int **a);
int max_colonne(int l, int c, int **a,int *max);
int minimal_colone(int l, int c, int **a,int *minimal);

int main()
{
    int **x,*max,*minimal,l,c, i, j;
    printf("nombre de lignes: ");
    scanf("%d",&l);
    printf("nombre de colonnes: ");
    scanf("%d",&c);
    x=in(l,c);
    printf("La matrice cree.\n");
    out(l,c,x);

    for(i=0;i<c;i++){

        x[i]=(int *)realloc(x, (l+2)*sizeof(int)); }


    free(x) ;
    free(max) ;
}

int **in(int l,int c)
{
    int **t,i,j;
    t=(int **)malloc(l*sizeof(int *));

    for(i=0;i<l;i++)
        t[i]=(int *)malloc(c*sizeof(int));

    for(i=0;i<l;i++)
        for(j=0;j<c;j++)
        {         printf("el[%d][%d]:",i,j);
            scanf("%d",(t[i]+j));
        }
    return t;
}

void out(int l, int c, int **a)
{
    int i,j;
    for(i=0;i<l;i++)
    {  for(j=0;j<c;j++)
        printf("%3d",*(a[i]+j));
        printf("\n");
    }
}

int max_colonne(int l, int c, int **a,int *max)
{
    int i,j;
    for(j=0;j<c;j++,max++)
    {           *max=*a[j];
        for(i=0;i<l;i++)
            if(*(a[j]+i) > *max) *max=*(a[j]+i);
    } return *max;
}

int minimal_colone(int l, int c, int **a,int *minimal)
{
    int i,j;
    for(j=0;j<c;j++,minimal++)
    {           *minimal=*(a[0]+j);
        for(i=0;i<c;i++)
            if(*(a[i]+j) < *minimal) *minimal=*(a[i]+j);
    } return *minimal;
}
dfeuer
  • 48,079
  • 5
  • 63
  • 167
  • 2
    What do you mean by not working fine? You get an error? Wrong result? Something else? – Reti43 Jan 11 '15 at 12:09
  • You are looping over `x[]` using the `c`, but you created it using `l`. Have a break ... ;-) – alk Jan 11 '15 at 12:19
  • 1
    And it might also help cleaning up your code in terms of indenting it properly: Less noise, better awareness ... – alk Jan 11 '15 at 12:21
  • @alk and please more whitespace, why are people afraid of using whitespaces if the compiler ignores them anyway and the make the code so more readable. – Iharob Al Asimi Jan 11 '15 at 12:23
  • @iharob: I simply press `Ctrl-Shift-F` and it looks nice. – alk Jan 11 '15 at 12:24

1 Answers1

1

Your realloc is not working because it's wrong, instead of

x[i]=(int *)realloc(x, (l+2)*sizeof(int)); 

it should be

x[i] = realloc(x[i], (l + 2) * sizeof(int)); 

and also, you don't need to cast to int *.

You should always check for the return value of malloc/realloc/calloc functions, on failure they return NULL, so to safely use realloc you should use a temporary variable, to prevent loosing the original pointer, since if you do this

x[i] = realloc(x[i], newSize);

and realloc returns NULL then you lost the rerference to the original pointer x[i] so I would recommend

void *tmp;

tmp = realloc(x[i], newSize);
if (tmp == NULL)
    handleMallocFailureErrorProbablyRetryOrExitTheProgram();
x[0] = tmp;

so for example, you can free(x[i]) in handleMallocFailureErrorProbablyRetryOrExitTheProgram().

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97