hello everyone... i ve got this txt file and i want to scan it and insert the 2nd row digits at profit[ncustomer] matrix, where ncustomer is the 1st digit which readed from file.
After that i want to add all the other digits in a 2 dimensional matrix dis[ncustomer][ncustomer] ...
this is the code i wrote but there is a problem with the dis[0][0] . All the other are OK
I heard that this is easier with pointer and ??malloc ????? function but i dont know these yet... if anyone can help me??
I ll be grateful...
thanks in advance!!
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE*fp=fopen("txt.txt","r+");
int i,j,k;
int ncustomer;
float data;
fscanf(fp,"%d",&ncustomer);
printf("\nncustomer=%d\n",ncustomer);
float profit[ncustomer];
float dis[ncustomer][ncustomer];
rewind(fp);
//=================================
// read this file and import the data in profit[ncustomer] and dis[ncostumer][ncostumer]
//=================================
i=1;
while((fscanf(fp,"%f",&data))!=EOF)
{
if(i==0)
{
continue ;
}
else if (i==1)
{
for(j=0;j<ncustomer;j++)
{
fscanf(fp,"%f",&profit[j]);
}
}
else
{
for(k=0;k<ncustomer;k++)
{
for(j=0;j<ncustomer;j++)
{
fscanf(fp,"%f",&dis[k][j]);
}
}
}
i++;
}
fclose(fp);
//=================================
// matrixes print
printf("\nprofit=\n");
for(j=0;j<ncustomer;j++)
{
printf("%.1f\t",profit[j]);
}
printf("\ndis=\n");
for(i=0;i<ncustomer;i++)
{
for(j=0;j<ncustomer;j++)
{
printf("%.1f\t",dis[i][j-1]);
}
printf("\n");
}
//=================================
getchar();
}