FILE *ptr_file;
char buf[1000];
float arr[1200][4];
int i,j;
float total2,ave;
ptr_file =fopen("data.txt","r");
if (!ptr_file)
{
printf("Error opening file!!!!");
getch();
return 1;
}
while (fgets(buf,1000, ptr_file)!=NULL)
{
for(i=0;i<1200;i++)
{
total2=0.0;
for(j=0;j<4;j++)
total2=total2+arr[j][i];
ave=total2/4;
printf("%f\n",total2);
printf("%f\n",ave);
}
}
fclose(ptr_file);
getch();
return 0;
15 Replies - 2339 Views - Last Post: 14 January 2011 - 11:06 AM
#1
access file and read the data into two dimensional array, C
Posted 12 January 2011 - 04:52 AM
Replies To: access file and read the data into two dimensional array, C
#2
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 05:13 AM
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?
#3
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 05:17 AM
total2=total2+arr[j][i];
You first have to fill arr with values.
what you do is just read a line to buf, but you don't pass it's values to arr.
You first have to parse buf, convert it into float values, and then assign it's values to arr.
Only then you can iterate arr and add the values to total2.
#4
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 07:02 AM
( b ) There are 1200 rows and 4 columns.
For example:
For the first row :
Input: 23.454 33.123 43.678 56.87
Therefore the output must show the total for each row follows by the average as
(total) 23.454 33.123 43.678 56.87 = 157.13
(average) 157.125/4 = 39.28
Output:
157.13 39.28
This post has been edited by lijeeva: 12 January 2011 - 07:10 AM
#5
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 07:15 AM
What is your problem, exactly and in detail, with examples.
#6
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 07:32 AM
#7
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 07:37 AM
Here I am required to create a 2d array to calculate the sum and the average of each row.
Example of text files input:
23.454 33.123 43.678 56.87
23.44 233.123 4.78 156.7
Output:
Total Average
157.13 39.28
418.04 104.51
This post has been edited by lijeeva: 12 January 2011 - 07:41 AM
#8
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 07:55 AM
while (fgets(buf,1000, ptr_file)!=NULL)
You have read a single line into your character buffer. You must now convert this line into your four numbers. As others have hinted look the function sscanf. Once you convert the buffer into numbers then you can calculate the sum and average.
Jim
#9
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 08:12 AM
#10
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 08:31 AM
while (fgets(buf,1000, ptr_file)!=NULL)
All of the rest of the code inside your while loop is incorrect and will cause errors.
Now you need to convert the c-string contained in buf into the individual numbers. This is where sscanf() comes into play.
Replace your while loop with the following code snippet it will demonstrate that the variable buf contains 4 numbers.
while (fgets(buf,1000, ptr_file)!=NULL)
{
printf("%s\n",buf);
}
Now after you see that you are indeed reading the file correctly you can proceed with trying to use sscanf() to place the individual numbers into your array.
Jim
#11
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 10:54 AM
What I should do?
for(i=0;i<1200;i++)
{
total2=0.0;
for(j=0;j<4;j++)
total2=total2+arr[j][i];
ave=total2/4;
sscanf(buf,"%s %f",arr,&total2,&ave);
printf ("%s %f\n",arr,total2,ave);
}
#12
Re: access file and read the data into two dimensional array, C
Posted 12 January 2011 - 11:10 AM
The sample below will place the numbers from the file into the doubles named first, second, third, fourth. It will then total them and produce the average.
double first, second, third, fourth;
double total, average;
while (fgets(buf,1000, ptr_file)!=NULL)
{
sscanf(buf,"%lf %lf %lf %lf", &first, &second, &third, &fourth);
total = first + second + third + fourth;
average = total / 4.0;
printf("Total = %f, Average = %f\n", total, average);
}
I will leave it to you to change the above code to work with your array.
Jim
#13
Re: access file and read the data into two dimensional array, C
Posted 14 January 2011 - 09:22 AM
#14
Re: access file and read the data into two dimensional array, C
Posted 14 January 2011 - 10:18 AM
Jim
#15
Re: access file and read the data into two dimensional array, C
Posted 14 January 2011 - 10:29 AM
for(int i=0;i<1200;i++)
{
for(int j=0;j<4;j++)
{
fscanf(buf,"%f %f %f %f", &first, &second, &third, &fourth);
total2=0;
total = first + second + third + fourth;
average = total / 4.0;
}
printf("Total = %f, Average = %f\n", total, average);
}
|
|

New Topic/Question
Reply




MultiQuote





|