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???