Initialize an array in an if statement
Author |
Message |
vansh03
|
Posted: Thu Jan 04, 2018 2:45 pm Post subject: Initialize an array in an if statement |
|
|
What is it you are trying to achieve?
I am trying to declare an array variable in an if statement.
What is the problem you are having?
*variable name* has not been declared
Describe what you have tried to solve this problem
I have tried declaring the variable at the top and only changing its value in the if statement but an array can only be initialized once. I understand that each index of an array can be set separately but i have many arrays in my actual program which are changed very often. Setting each one separately could easily take thousands of lines.
Post any relevant code
Underneath is code for a simplified version of what I'm trying to do, as my actual program is too long and complicated.
Turing: |
var c : int := 1
if c = 1 then
var x : array 1..2 of int := init(5, 6)
else
var x : array 1..2 of int := init(7, 8)
end if
put x(2)
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Thu Jan 04, 2018 5:19 pm Post subject: RE:Initialize an array in an if statement |
|
|
If you declare a variable inside an if statement, then it only exists inside that if statement. The same applies to loops, functions, procedures, etc. You need to declare it before the if, then initialize it inside the if. |
|
|
|
|
|
vansh03
|
Posted: Thu Jan 04, 2018 7:17 pm Post subject: Re: RE:Initialize an array in an if statement |
|
|
Insectoid @ Thu Jan 04, 2018 5:19 pm wrote: If you declare a variable inside an if statement, then it only exists inside that if statement. The same applies to loops, functions, procedures, etc. You need to declare it before the if, then initialize it inside the if.
How would I do that with an array? |
|
|
|
|
|
lordroba
|
Posted: Thu Jan 04, 2018 11:07 pm Post subject: Re: Initialize an array in an if statement |
|
|
You either need to have it like this...
Turing: |
var c : int := 1
if c = 1 then
var x : array 1..2 of int := init(5, 6)
put x(2)
else
var x : array 1..2 of int := init(7, 8)
put x(2)
end if
|
or declare the x variable outside the if statement like Insectoid said.
In the case that c does not equal 1, the program won't know what x is, that's why it's giving you an error. |
|
|
|
|
|
vansh03
|
Posted: Thu Jan 04, 2018 11:18 pm Post subject: Re: Initialize an array in an if statement |
|
|
lordroba @ Thu Jan 04, 2018 11:07 pm wrote: You either need to have it like this...
Turing: |
var c : int := 1
if c = 1 then
var x : array 1..2 of int := init(5, 6)
put x(2)
else
var x : array 1..2 of int := init(7, 8)
put x(2)
end if
|
or declare the x variable outside the if statement like Insectoid said.
In the case that c does not equal 1, the program won't know what x is, that's why it's giving you an error.
In my actual program, I need to put x outside of the if statement as there needs to be code in between, and I can't seem to declare an array then initialize after the fact. Turing won't let me. |
|
|
|
|
|
TokenHerbz
|
Posted: Fri Jan 05, 2018 2:41 am Post subject: RE:Initialize an array in an if statement |
|
|
just assign each value of the array, its troublesome but thats turing for you....
generally you shouldn't have to do that though, if i knew the question i could probly help you better but i dont know why you would want to have to do it like this...?
Turing: |
var ar : array 0 .. 1 of int
var c: int := 2
if c = 1 then
ar(0) := 5
ar(1) := 6
else
ar(0) := 7
ar(1) := 8
end if
put ar(0)
put ar(1)
|
http://compsci.ca/holtsoft/doc/init.html
http://compsci.ca/holtsoft/doc/array.html |
|
|
|
|
|
vansh03
|
Posted: Fri Jan 05, 2018 11:28 am Post subject: Re: RE:Initialize an array in an if statement |
|
|
TokenHerbz @ Fri Jan 05, 2018 2:41 am wrote: just assign each value of the array, its troublesome but thats turing for you....
generally you shouldn't have to do that though, if i knew the question i could probly help you better but i dont know why you would want to have to do it like this...?
Turing: |
var ar : array 0 .. 1 of int
var c: int := 2
if c = 1 then
ar(0) := 5
ar(1) := 6
else
ar(0) := 7
ar(1) := 8
end if
put ar(0)
put ar(1)
|
http://compsci.ca/holtsoft/doc/init.html
http://compsci.ca/holtsoft/doc/array.html
Sorry about that, I am just so frustrated with my program that I forgot ti say thanks. Thankyou for taking time out to help me.
My program is actually a word search game so it needs to have the actual game in between the if statement and the put. in the real program, its also not a put its just the actual use of the array in the game. The game is very long so It would unnecessary to copy and paste hundreds of lines in each if and elsif. That's before mentioning, my real program has 11 arrays, not 1, with 15 indexes each, so setting each index of each array separately is not really an option.
Thanks to everyone again for the responses.
Im hoping explaining my situation a little better helps finding the solution. |
|
|
|
|
|
Insectoid
|
Posted: Fri Jan 05, 2018 7:59 pm Post subject: RE:Initialize an array in an if statement |
|
|
Tokenherbz' solution is the correct one. What goes between the if and the put doesn't really matter at all. That said, why do you have 15 arrays? Why do you need that many for a word search? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Sun Jan 07, 2018 1:32 am Post subject: RE:Initialize an array in an if statement |
|
|
im curious aswell, perhaps we could give you advice to better tackle your problem if we knew what your trying to accomplish? |
|
|
|
|
|
|
|