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
and use the spell ckeck button to correct your spelling mistakes when you post