×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Problem with variable's name

## Problem with variable's name

(OP)
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?

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?"

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?"

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

### RE: Problem with variable's name

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) 

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!