Home > Archive > Fortran > January 2008 > finding average, pls help me
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
finding average, pls help me
|
|
| rajkrai@gmail.com 2008-01-18, 7:15 pm |
| Program
do m = 1, 4
do i = 1, 3
do j = 1, 3
read (11, 'F') dat(m,i,j)
enddo
enddo
enddo
reads the following 36 seq data.
3.0
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
4.0
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
I want to get the avarage ie (3.0+4.0+1.0+2.0)/4.0=2.5;
(3.1+4.1+1.1+2.1)/4.0 = 2.6 and so on so that I can get 9 average.
I did in the following way.
do i = 1, 3
do j = 1, 3
summ = 0
do m = 1, 4
summ = dat(m,i,j)+summ
enddo
average = summ/4
write (12,"F3.2") average
enddo
enddo
end
whats the problem with it? how can i get the avarage? I appreciate
your help. Thanks in advance.
Raj
| |
| Richard Maine 2008-01-18, 7:15 pm |
| <rajkrai@gmail.com> wrote:
[much elided]
You need to show the *COMPLETE* program. That includes declarations.
Declarations are absolutely critical. The code you showed could not
plausibly have run without at least some.
You also need to show what happened when you ran your code. You give no
hint as to what the problem was.
That being said.
> read (11, 'F') dat(m,i,j)
That shouldn't have worked. The F isn't a valid edit descriptor, though
some compilers might accept it as an extension. Also, you need parens.
You probably wanted something more like '(F3.2)', though I might argue
for using list-directed (a * for the format).
> write (12,"F3.2") average
Again, you need parens.
> whats the problem with it?
Other than that, it looks more or less correct. You need to tell us what
the problem is rather than making us guess.
This isn't the way I'd do it. I'd use array slices and the SUM
intrinsic, but as this looks like a homework assignment, I don't think
I'll show exact code, but just that general suggestion.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Gordon Sande 2008-01-18, 7:15 pm |
| On 2008-01-18 14:54:39 -0400, rajkrai@gmail.com said:
> Program
>
> do m = 1, 4
> do i = 1, 3
> do j = 1, 3
> read (11, 'F') dat(m,i,j)
> enddo
> enddo
> enddo
>
> reads the following 36 seq data.
>
> 3.0
> 3.1
> 3.2
> 3.3
> 3.4
> 3.5
> 3.6
> 3.7
> 3.8
> 4.0
> 4.1
> 4.2
> 4.3
> 4.4
> 4.5
> 4.6
> 4.7
> 4.8
> 1.0
> 1.1
> 1.2
> 1.3
> 1.4
> 1.5
> 1.6
> 1.7
> 1.8
> 2.0
> 2.1
> 2.2
> 2.3
> 2.4
> 2.5
> 2.6
> 2.7
> 2.8
>
> I want to get the avarage ie (3.0+4.0+1.0+2.0)/4.0=2.5;
> (3.1+4.1+1.1+2.1)/4.0 = 2.6 and so on so that I can get 9 average.
>
> I did in the following way.
>
> do i = 1, 3
> do j = 1, 3
> summ = 0
> do m = 1, 4
> summ = dat(m,i,j)+summ
> enddo
> average = summ/4
> write (12,"F3.2") average
> enddo
> enddo
> end
>
> whats the problem with it? how can i get the avarage? I appreciate
> your help. Thanks in advance.
>
> Raj
Perhaps you could help with a few more hints.
Like:
1. Is there a complete program with declrtations of more variables?
2. Did you try running this to get some compiler diagnostics? What
where they (if there were some)? What compiler? What system?
3. What were you expecting out that you did not get?
"It does not work" just does not cut it in this newsgroup. You will
get grilled for a real question! And for some indication that you
have tried something constructive.
And yes there should have been diagnostics fromm what you have shown.
Sorry but I will not tell you what I expect until you show some effort.
Once you do that you will get several sets of good responses.
| |
| e p chandler 2008-01-18, 7:15 pm |
| On Jan 18, 2:07=A0pm, nos...@see.signature (Richard Maine) wrote:
> <rajk...@gmail.com> wrote:
>
> [much elided]
>
> You need to show the *COMPLETE* program. That includes declarations.
> Declarations are absolutely critical. The code you showed could not
> plausibly have run without at least some.
>
> Richard Maine =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| Good judgement come=
s from experience;
I'm going to be a pest and suggest that the original program is not
Fortran at all but something that looks like Fortran. Not that I know
what language it actually is. :-(.
-- e
| |
| Gib Bogle 2008-01-18, 7:15 pm |
| rajkrai@gmail.com wrote:
> Program
>
> do m = 1, 4
> do i = 1, 3
> do j = 1, 3
> read (11, 'F') dat(m,i,j)
> enddo
> enddo
> enddo
>
> reads the following 36 seq data.
>
> 3.0
> 3.1
> 3.2
> 3.3
> 3.4
> 3.5
> 3.6
> 3.7
> 3.8
> 4.0
> 4.1
> 4.2
> 4.3
> 4.4
> 4.5
> 4.6
> 4.7
> 4.8
> 1.0
> 1.1
> 1.2
> 1.3
> 1.4
> 1.5
> 1.6
> 1.7
> 1.8
> 2.0
> 2.1
> 2.2
> 2.3
> 2.4
> 2.5
> 2.6
> 2.7
> 2.8
>
> I want to get the avarage ie (3.0+4.0+1.0+2.0)/4.0=2.5;
> (3.1+4.1+1.1+2.1)/4.0 = 2.6 and so on so that I can get 9 average.
>
> I did in the following way.
>
> do i = 1, 3
> do j = 1, 3
> summ = 0
> do m = 1, 4
> summ = dat(m,i,j)+summ
> enddo
> average = summ/4
> write (12,"F3.2") average
> enddo
> enddo
> end
>
> whats the problem with it? how can i get the avarage? I appreciate
> your help. Thanks in advance.
>
> Raj
A format of F3.2 is not very useful for displaying a number like 2.6,
since the total field width is 3 and 2 spaces are reserved for the
numbers after the decimal point, i.e. it can display numbers like .26
Try
write(12,'(f7.4)') average
This gives space for a sign followed by a number like 1.2345
| |
| Terence 2008-01-18, 7:15 pm |
| You won't get the correct answers if you divide by 4 instead of 4.0.
The input values are floating point numbers, not integers, and so the
divisor in this case must also be a floating point constant.
| |
| Gib Bogle 2008-01-18, 7:15 pm |
| Terence wrote:
> You won't get the correct answers if you divide by 4 instead of 4.0.
>
> The input values are floating point numbers, not integers, and so the
> divisor in this case must also be a floating point constant.
Really?
| |
| James Giles 2008-01-18, 7:15 pm |
| Terence wrote:
> You won't get the correct answers if you divide by 4 instead of 4.0.
>
> The input values are floating point numbers, not integers, and so the
> divisor in this case must also be a floating point constant.
The mixed mode rules in Fortran are well defined and have been
since before the first standard. There's nothing wrong with dividing
REALs by INTEGERs.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
| |
| Richard Maine 2008-01-18, 10:14 pm |
| Gib Bogle <bogle@ihug.too.much.spam.co.nz> wrote:
> Terence wrote:
>
> Really?
No.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Terence 2008-01-19, 4:25 am |
| He's learning Fortran.
We don't know how he defined his variables.
He DID give the example of using 4.0 to divide with, with did not CODE
that.
I don't want to confuse him with the rules of what gets converted to
what in the hiearchy of operations.
If his individual variables (dat, summ and average) are floating
point, he'll get the right answer; but if any of them is an integer
type, he won't.
| |
| Richard Maine 2008-01-19, 4:25 am |
| Terence <tbwright@cantv.net> wrote:
> If his individual variables (dat, summ and average) are floating
> point, he'll get the right answer; but if any of them is an integer
> type, he won't.
In which case, dividing by 4.0 won't fix the problem either, because the
sum will already be wrong before dividing.
Besides which, that would be a quite unlikely error for the OP to have
made. The variable in question (summ) would be real if implicitly
declared, so the OP would have had to explicitly declare it integer (or
have messed with the implicit typing rules). While that is theoretically
possible... well... let me just say that your debugging instincts and
techniques must not have much in common with mine. My instincts say that
this is all likely to be just a confusing and irrelevant diversion.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Gib Bogle 2008-01-19, 4:25 am |
| Richard Maine wrote:
> Gib Bogle <bogle@ihug.too.much.spam.co.nz> wrote:
>
>
> No.
>
;-)
| |
| Richard L Walker 2008-01-19, 7:12 pm |
| I'd get the total of whatever numbers you want to average and then
divide by however many numbers you are averaging. I used to start
with a total of 0 and then increase that total by each number as I
got to it. I use the variable count to keep track of how many
numbers I'm averaging.
This isn't FORTRAN but it shows the procedure.
total = 0
count=0
input a number until there isn't another number
total=total + number
count=count+1
go back to the input statement
exit the loop here
average = total/count
|
|
|
|
|