Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Using a for loop to reference multiple variables with similar names
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
swegmonger




PostPosted: Thu Oct 19, 2017 1:58 pm   Post subject: Using a for loop to reference multiple variables with similar names

What is it you are trying to achieve?
Hello. I am trying to build a 3D Rubik's cube simulation as a final project for the G11 computer science course I am taking. I've figured out all the math required to actually rotate the cube model in 3D space, but my code is extremely long and repetitive.


What is the problem you are having?
My main problem is the majority of my code is repetitive procedures that accept real coordinate values of the vertices and output new values to simulate the rotation of the cube.
Here is a condensed excerpt from my code:
(please assume the variables used below, x1, x2, x3, etc. are global variables that have already been declared outside of the procedure)

Turing:


procedure rotateRight
    if x1 > 0 and z1 >= 0 then
        x1 := sqrt (3 - y1 ** 2) * cosd (round (arctand (abs (z1 / x1))) + 1)
        z1 := sqrt (3 - y1 ** 2) * sind (round (arctand (abs (z1 / x1))) + 1)
    elsif x1 < 0 and z1 >= 0 then
        x1 := sqrt (3 - y1 ** 2) * -cosd (round (arctand (abs (z1 / x1))) - 1)
        z1 := sqrt (3 - y1 ** 2) * sind (round (arctand (abs (z1 / x1))) - 1)
    end if

    if x2 > 0 and z2 >= 0 then
        x2 := sqrt (3 - y2 ** 2) * cosd (round (arctand (abs (z2 / x2))) + 1)
        z2 := sqrt (3 - y2 ** 2) * sind (round (arctand (abs (z2 / x2))) + 1)
    elsif x2 < 0 and z2 >= 0 then
        x2 := sqrt (3 - y2 ** 2) * -cosd (round (arctand (abs (z2 / x2))) - 1)
        z2 := sqrt (3 - y2 ** 2) * sind (round (arctand (abs (z2 / x2))) - 1)
    end if

    if x3 > 0 and z3 >= 0 then
        x3 := sqrt (3 - y3 ** 2) * cosd (round (arctand (abs (z3 / x3))) + 1)
        z3 := sqrt (3 - y3 ** 2) * sind (round (arctand (abs (z3 / x3))) + 1)
    elsif x3 < 0 and z3 >= 0 then
        x3 := sqrt (3 - y3 ** 2) * -cosd (round (arctand (abs (z3 / x3))) - 1)
        z3 := sqrt (3 - y3 ** 2) * sind (round (arctand (abs (z3 / x3))) - 1)
    end if

    if x4 > 0 and z4 >= 0 then
        x4 := sqrt (3 - y4 ** 2) * cosd (round (arctand (abs (z4 / x4))) + 1)
        z4 := sqrt (3 - y4 ** 2) * sind (round (arctand (abs (z4 / x4))) + 1)
    elsif x4 < 0 and z4 >= 0 then
        x4 := sqrt (3 - y4 ** 2) * -cosd (round (arctand (abs (z4 / x4))) - 1)
        z4 := sqrt (3 - y4 ** 2) * sind (round (arctand (abs (z4 / x4))) - 1)
    end if
end rotateRight



As you can probably tell, the 4 if statements in the procedure are identical except for the fact that the variable names differ by a single number.

I would like to know if there is a way to use a controlled loop (for loop) to modify the variable names through each iteration of the loop so that all of x1, y1, z1; x2, y2, z2, etc. can be referenced in the procedure.
Something like the following:

Turing:


procedure rotateRight
    for i : 1 .. 4
        if xi > 0 and zi >= 0 then
            xi := sqrt (3 - yi ** 2) * cosd (round (arctand (abs (zi / xi))) + 1)
            zi := sqrt (3 - yi ** 2) * sind (round (arctand (abs (zi / xi))) + 1)
        elsif xi < 0 and zi >= 0 then
            xi := sqrt (3 - yi ** 2) * -cosd (round (arctand (abs (zi / xi))) - 1)
            zi := sqrt (3 - yi ** 2) * sind (round (arctand (abs (zi / xi))) - 1)
        end if
    end for
end rotateRight



Describe what you have tried to solve this problem
I have tried to use the above code, but as expected, using "xi, yi, zi, etc" would end up referencing completely new variables. If you know of a way to properly implement loops in such a way, my project and I would greatly appreciate it if you could help.

Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Oct 19, 2017 4:29 pm   Post subject: RE:Using a for loop to reference multiple variables with similar names

You're gonna wanna take a look at arrays.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: