LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2004, 09:55 AM   #16
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15

thanks both of you. thanks for your concerne. I undrstand your points

Finally I solved the fork() problem
but I need your help in this not your code as you said, just the idea and I will write

1 3
2 4
3 5
4 6
5 2
6 1
7 0
8 6
9 9
9 1

I have these coulm in a file how can I calculat thier average (each colum alone), in clearer way how can I read each colum in the file


thanks again for your honsty
 
Old 11-19-2004, 10:45 AM   #17
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
1 3
2 4
3 5
4 6
5 2
6 1
7 0
8 6
9 9
9 1

I have these coulm in a file how can I calculat thier average (each colum alone), in clearer way how can I read each colum in the file
Actually your file will look like this:
<number chars><space chars><number chars><new line char><number char><space char><nuber chars><new line>...

SO you cannot directly see any collums.

What u may do is use fscanf,

ex.
for(*********){
fscanf(fp,"%d",&a);
fscanf(fp,"%d",&b);
}
will get the first int and put it in a and the second one and put it in b......then the third and forth .....

make sure you check the returning value of fscanf, for errors.

if you define to arrays
int col1[100],col2[100], u can put the values in there, but if you only want the aver you dont need that.

you can simply do


Code:
i= Sum1=Sum2=0;

while((fscanf(fp,"%d %d",&a,&b))!=EOF){
Sum1 = Sum1 + a;
Sum2 = Sum2 + b;
i++;
}
aver1 = Sum1 / (float) i;
aver2 = Sum2 / (float) i;
the float thing you need it because when u use integers in C
3/2 = 1 <- an integer

3.0/2.0 = 1.5 <- float
or 3/2.0 = 1.5 <- float
or 3.0/2 = 1.5 <- float

so you must define aver1,aver2 as floats
and cast one of the integers in the calculation to float, so that the calculation is done with floats

also see
Code:
man fscanf
and use the spell ckeck button to correct your spelling mistakes when you post

Last edited by perfect_circle; 11-19-2004 at 10:49 AM.
 
Old 11-19-2004, 11:23 AM   #18
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Also consider buying "The C Programming Language (Second Edition)"
by Bian W. Kernighan & Dennis M. Ritchie.

It's not very expensive and it is the best C programming book
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing output to file Hockeyfan Linux - General 3 10-24-2005 08:02 PM
PHP file writing benrose111488 Programming 6 07-01-2005 03:57 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM
c++ writing to a .txt file xemous Programming 8 09-01-2004 10:57 PM
writing a batch file ???? Micro Linux - General 3 01-09-2003 11:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration