Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with variable's name 1

Status
Not open for further replies.

j0zn

Programmer
Jul 19, 2013
36
BR
Hi guys! I have a weird problem about the following program:

when I use "i_square"(as a integer) and choose "-3" and "7" the output is:
**************************************************************
What is the first numbe which square is asked?
-3
How many square you want?
7
The first 0 square from -3 are: 9 4 1 0 1 4 9
****************************************************************

and when I use "square"(as a integer) and choose "-3" and "7" the output is:
###################################################################################
What is the first numbe which square is asked?
-3
How many square you want?
7

The first 7 square from
-3 are: 9 4 1 0 1 4 9
###################################################################################


So, the question is: how changing a variable name could affect the program's output? and why is it happen?
and how to solve it?
Thank you in advance


PROGRAM square_list
IMPLICIT none
INTEGER, PARAMETER :: max_allowed = 20
INTEGER :: i_square(max_allowed)
INTEGER :: num, quant_num
INTEGER :: first_square, last_square

! Write the fisrt square
PRINT *, "What is the first numbe which square is asked?"
READ *, first_square

IF ( first_square > max_allowed ) THEN
PRINT *
PRINT *, "It is not possible"
ELSE
! Find how many squere are necessary
PRINT *, "How many square you want?"
READ *, quant_num

last_square=first_square+quant_num-1
! Verify if it is inside of limite allowed
max: IF ( last_square > max_allowed ) THEN
PRINT *
PRINT *, "It is beyond ..., the max square is&
&permitido eh de ", max_allowed
ELSE max
min: IF ( quant_num < 1) THEN
PRINT *
PRINT *,quant_num,"it is not a valid number"
ELSE min
! Quantity of numbers is ok then store the square

DO num = first_square, last_square
i_square(num) = num**2

END DO

! Print the list of squares
PRINT *
so_um: IF (quant_num==1) THEN
PRINT *, "The first square is: ", i_square&
(1)
ELSE so_um

PRINT *, "The first ", quant_num , &
" square from ", &
first_square, " are: ",&
(i_square(num), num=&
first_square, last_square)
END IF so_um

END IF min
END IF max
END IF
END PROGRAM square_list
 
Your problem is in the following
Code:
INTEGER, PARAMETER :: max_allowed = 20
INTEGER :: i_square(max_allowed)
...
DO num = first_square, last_square
i_square(num) = num**2
END DO
...
PRINT *, "The first ", quant_num , &
" square from ", &
first_square, " are: ",&
(i_square(num), num=&
first_square, last_square)
first_square goes from -3 to 3 but your array is declared 1 to 20. When you specify -3, it could overwrite some of the other variables. To get it to work correctly, change to something like. Just lucky I suppose: it could have crashed
Code:
DO num = first_square, last_square
    i_square(num - first_square + 1) = num**2
END DO
...
PRINT *, "The first ", quant_num , &
" square from ", &
first_square, " are: ",&
(i_square(num - first_square + 1), num=&
first_square, last_square)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top