access file and read the data into two dimensional array, C

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4073 Views - Last Post: 14 January 2011 - 11:06 AM Rate Topic: -----

#1 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

access file and read the data into two dimensional array, C

Posted 12 January 2011 - 04:52 AM

Hi, I am trying a program in C. I'm a newbie for C. Here I am trying to read on file that contains numerical data and I am required to find the total of each row and the average by using two dimensional array. Hope anyone of you could help me on this. Below is the following code that I have tried.

                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;


Is This A Good Question/Topic? 0
  • +

Replies To: access file and read the data into two dimensional array, C

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 05:13 AM

Please give us some more details of your problem.
( 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?
Was This Post Helpful? 0
  • +
  • -

#3 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 05:17 AM

First, arr's calues are not initialized. So what do you expect to get when you do:
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.
Was This Post Helpful? 0
  • +
  • -

#4 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 07:02 AM

( a ) Yes,it compile but the program keep on running.
( 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

Was This Post Helpful? 0
  • +
  • -

#5 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 07:15 AM

Instead of just running a lot of text together in a incomprehensible mangle take some time to lay out your question clearly.

What is your problem, exactly and in detail, with examples.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 07:32 AM

man sscanf.
Was This Post Helpful? 0
  • +
  • -

#7 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 07:37 AM

Given a text file with values that consist of 1200 rows and 4 columns.
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

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 07:55 AM

With the following code:

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
Was This Post Helpful? 0
  • +
  • -

#9 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 08:12 AM

Well, I don't really get on what you have said since sscanf functions to read data from string but over here I have values. Can you please show me on it works?
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 08:31 AM

You have number values in your file but your program has a c-string containing 4 values. You read this c-string into your char array (char buf[1000];) with this line.

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
Was This Post Helpful? 0
  • +
  • -

#11 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 10:54 AM

I have tried this but it does not solve my problem...
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);

}



Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: access file and read the data into two dimensional array, C

Posted 12 January 2011 - 11:10 AM

As I said in my previous post your for loop solution is wrong.

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
Was This Post Helpful? 0
  • +
  • -

#13 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

Re: access file and read the data into two dimensional array, C

Posted 14 January 2011 - 09:22 AM

Hi jimblumberg, I still cant get on this. I just don't get on what is wrong with it?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: access file and read the data into two dimensional array, C

Posted 14 January 2011 - 10:18 AM

Have you tried to replace the variables named first, second, third, fourth with variables from your array?


Jim
Was This Post Helpful? 0
  • +
  • -

#15 lijeeva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-January 11

Re: access file and read the data into two dimensional array, C

Posted 14 January 2011 - 10:29 AM

Hi Jim, this is what i have tried.

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);
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2