0

When I run the code for Celsius only I am getting the result below the code:

    program temperature
! F C temperature conversion
implicit none
real :: celcius=0.0, fahrenheit=0.0

integer:: t,n
print *,'enter the number of lines'
read*, n
do n=1,n
print*,'enter the  value of t: one per line',n
read*, t
celcius=5/9*(t-32)

enddo
do n=1,n
print*, t, celcius
enddo
end program

result

    enter the number of lines
3
 enter the  value of t: one per line           1
50
 enter the  value of t: one per line           2
20
 enter the  value of t: one per line           3
10
          10   0.00000000E+00
          10   0.00000000E+00
          10   0.00000000E+00
          10   0.00000000E+00

It's clear that compiler is not picking the value of t in the calculation.

1
  • Only the last value entered in the first loop provides the value for t outside the loop. If you want to use multiple values you should use an array, or restructure the rest around the first loop. Commented Feb 21, 2019 at 14:09

2 Answers 2

1

You have several options.

  1. Store the input in an array and process the array.

    program temperature
      ! F C temperature conversion
      implicit none
    
      real, allocatable :: fahrenheit(:)
      integer:: n
    
      print *,'enter the number of lines'
      read*, n
      allocate(fahrenheit(n))
      print*,'enter the  value of t: all items on one line'
      read*, fahrenheit
      print *, 'F: ', fahrenheit
      print *, 'C:', fahrenheit_to_celcius(fahrenheit)
    
    contains
    
      pure elemental function fahrenheit_to_celcius(t_f) result(t_c)
        real, intent(in) :: t_f
        real :: t_c
    
        t_c = 5.*(t_f-32.)/9.
    
      end function fahrenheit_to_celcius
    
    end program
    
  2. Process the inputs one at a time

    program temperature
      ! F C temperature conversion
      implicit none
    
      real :: fahrenheit
      integer:: i, n
    
      print *,'enter the number of lines'
      read*, n
    
      do i = 1, n
         print*,'enter the  value of t: one per line',n
         read*, fahrenheit
         print *, 'F: ', fahrenheit, 'C:', fahrenheit_to_celcius(fahrenheit)
      enddo
    
    contains
    
      pure elemental function fahrenheit_to_celcius(t_f) result(t_c)
        real, intent(in) :: t_f
        real :: t_c
    
        t_c = 5.*(t_f-32.)/9.
    
      end function fahrenheit_to_celcius
    
    end program
    

Note that I have used the elemental keyword for the function. It means that you can pass a scalar as well as an array. This is a nice solution for direct computations such as the one here: the routine fahrenheit_to_celcius is identical in both cases.

I fixed the 5/9 (that returns 0) and the mixed up variables as well.

Sign up to request clarification or add additional context in comments.

2 Comments

hi thanks for the help....i had managed to get the results as you suggested in the 2nd method but couldn't get the out put in the table form which i presume is possible with arrays as of now since i haven't got as far as the last block in your code with function
By "in table form", do you mean subsequent lines with first the temperature in C then in F? If so, just loop over the output array in the first example as follows: do i = 1, n print *, fahrenheit(i), fahrenheit_to_celcius(fahrenheit(i)) end do. Put these in subsequent lines and do not forget to declare the variable i as integer :: i.
1

You have at least three problems:

  1. The expression 5/9*(t-32) is evaluated left-to-right, so the 5/9 part is an integer (truncating) division, always producing 0. The product of zero with anything finite is zero. There are several ways you could address that, but one of the simpler would be to rewrite the expressions as 5 * (t - 32) / 9.

  2. Your variables t and celcius are scalars. They hold only one number at a time each. In your first loop you assign multiple values to each of them them in sequence. When you later perform a second loop to print the results, only the last value assigned to each variable will be accessible. If you must defer output until after reading all the input then one way to handle it would be to make t and celcius arrays of sufficient size, and store your values in different elements. (Also note: the correct spelling of the English word is "celsius".)

  3. Per @albert in comments, after an indexed do loop finishes, the value of the iteration variable is the one it would have had in the next iteration of the loop, if there were one. Therefore, by using variable n both as your iteration variable and your upper bound, you are causing its value to be different after each loop than it was before. There are several ways you could address this, but I urge you to simply avoid reusing n as your iteration variable. There is no efficiency to be gained by avoiding a for-purpose iteration variable.

7 Comments

so without using array here i wont be able to view result in a table form? i am sorry i know its a silly question but i am trying understand how array and loops work
I think there is even a more serious problem: do n=1,n, i.e. redefining the value of n
That's an interesting observation, @albert, mainly because it is surprisingly wrong. In a conforming Fortran implementation, the number of iterations of an indexed do loop is computed (as if) before entry into the loop. Therefore, if the upper bound is given by a variable and that variable is modified during execution of the loop, including if it is used as the iteration variable, the number of iterations is not affected. Indeed, you can see the effect of that in the output reported by the OP.
You can especially see that in the first loop the initial n has the value 3 and in the second loop the initial n has the value 4 (not 100% sure but I think that actually the value of a loop counter at the end of a loop is implementation dependent).
Yes, @albert, I was just writing an addendum about that. I disagree that it constitutes a "more serious" problem, but it does need to be addressed, and I will shortly update this answer to talk about that. But there is no uncertainty about the value of the iteration variable after the loop; see, for example, stackoverflow.com/a/43211598/2402272.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.