Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Using a for loop to reference multiple variables with similar names
Author Message
swegmonger

Posted: 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

Insectoid

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: