×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Students Click Here

Problem with variable's name

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

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close