Hi, there !...

I just found some reference here (http://www.daniweb.com/forums/thread160974.html) how to used malloc on multidimentional array.

I tried it to apply on my case using Microsoft Visual C++.
Basically my code is to read an array of numbers in txt file.
The rows and cols of array dimension are dynamically changed depend
on numbers provide in txt file.

But a lot of error generate when i tried to implement my code.
Here my is my code:

#include <stdio.h>
#include <stdlib.h>


main() 
{
       FILE *file, *file1;
       int i,j,maxi,maxj;
       
       
       printf("Enter number of rows = ");
       scanf("%i", &maxi);

       printf("Enter number of cols= ");
       scanf("%i", &maxj); 
       
       //from http://www.daniweb.com/forums /thread160974.html
       float **arr = malloc( maxi * sizeof *arr );
       for ( i = 0 ; i < maxi ; i++ ) {
       arr[i] = malloc( maxj * sizeof *arr[i] );
       }
       
       file = fopen("inputfile.txt", "r");
       file1 = fopen("outputfile.txt", "w");
       
           for (i = 0, j = 0 ; i<maxi; i++)
           {
           while(!feof(file))
           {fscanf(file, "%f", &arr[i][j]);
           j++;}
         j=maxj;
           }
       
       for(i=0 ; i<maxi ; i++) 
       {
            for(j=0 ; j<maxj ; j++) 
            {  
            //printf ("arr[%i][%i] = %f \n",i,j,arr[i][j]);
            fprintf(file1,"arr[%i][%i] = %f \n",i,j,arr[i][j]);
            }
       }
      fclose(file);
      fclose(file1);
      system("pause");
      return 0;
}

Here sample my array in inputfile.txt :
0.112473 0.646742 0.511758 0.668315
0.054922 0.745220 0.558939 0.685408
0.039145 0.778385 0.571827 0.685408
0.028496 0.810535 0.580570 0.688868
0.027084 0.843699 0.581700 0.688868
0.023846 0.874756 0.584338 0.688868
0.023337 0.906906 0.584790 0.683218
0.020540 0.940070 0.587052 0.691694
0.000000 0.972142 0.603934 0.684560
0.998899 0.286695 0.181866 0.206173
0.983896 0.308233 0.194151 0.228987
0.954266 0.330550 0.218420 0.247846
0.922824 0.353648 0.244197 0.285775
0.876231 0.375966 0.282333 0.293756
0.832624 0.398986 0.318058 0.334652
0.766315 0.421303 0.372324 0.394971
0.712212 0.444401 0.416642 0.474785
0.634415 0.467499 0.480404 0.532067
0.574816 0.489739 0.529243 0.585888
0.488278 0.512837 0.600090 0.629821

Could you guys help me with this problems???

line 18: C language requires all variables have to be declared at the top of the block. So you need to move the declaration of arr up to about line 8.

arr = malloc( maxi * sizeof(float*) );
       for ( i = 0 ; i < maxi ; i++ ) {
       arr[i] = malloc( maxj * sizeof(float) );

There are other ways to do the above, but this is the one I find most convenient.

Thank you very much Ancient Dragon the compiling was done.

But seem new problems arise. The result of outputfile.txt are strange.
here the output:
arr[0][0] = 0.112473
arr[0][1] = 0.646742
arr[0][2] = 0.511758
arr[0][3] = 0.668315
arr[1][0] = -431602080.000000
arr[1][1] = -431602080.000000
arr[1][2] = -431602080.000000
arr[1][3] = -431602080.000000
......same...
......until...
arr[19][0] = -431602080.000000
arr[19][1] = -431602080.000000
arr[19][2] = -431602080.000000
arr[19][3] = -431602080.000000

What's wrong with the code. i just suspect on this part

for (i = 0, j = 0 ; i<maxi; i++)
           {
           while(!feof(file))
           {fscanf(file, "%f", &arr[i][j]);
           j++;}
         j=maxj;
           }

it this because of increment or using !feof(file)????

thank's in advanced

>while(!feof(file))

I glanced very quickly to your last post and saw that.
feof(file) would never give you the expected result as a loop control.
See here for some explanation.

Furthermore that for loop will continue regardless if fscanf() can read a conversion or not. It should be able to stop when fscanf() fails.

Also, when does the value of j ever get reset to 0? j needs to be incremented from 0 to maxj and then reset back to 0. In the meantime the value of i should not change. Something like this:

for(i = 0; i < maxi; i++)
{
    for(j = 0; j < maxj; j++)
    {
         fscanf(...);
    }
}

Another way to do it is to use fscanf() as the loop controller

int i = j = 0;
while( fscanf("%f", &arr[i][j]) > 0)
{
    j++;
    if( j == maxj)
    {
        j = 0;
        i++;
    }
}

Great..it is solved..thanks a lot
to : Ancient Dragon & Aia

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.