
-----------------------------------
amz_best
Sat Sep 27, 2014 3:38 pm

Access array without a for loop?
-----------------------------------
What is it you are trying to achieve?
Access an array without a for loop


What is the problem you are having?
idk how 2.


Describe what you have tried to solve this problem
tried not using a for loop.  didnt work.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
nothing relevant, i just need the basic idea







Please specify what version of Turing you are using
latest version of open turing

-----------------------------------
Tony
Sat Sep 27, 2014 5:52 pm

Re: Access array without a for loop?
-----------------------------------

tried not using a for loop.  didnt work.
Lets see how you've tried it. While at it, post an example that _does_ use a loop as well -- this can be used to figure out parts that are necessary, and what can be taken out.

-----------------------------------
Nathan4102
Sat Sep 27, 2014 7:08 pm

RE:Access array without a for loop?
-----------------------------------
If you want to iterate through the entirety of an array, you need a for loop.

If you want to access a specific index of an array, you can get away with no for loop.

Whether in a for loop or not in a for loop, the array-access code syntax is identical.

-----------------------------------
Raknarg
Sun Sep 28, 2014 11:06 pm

RE:Access array without a for loop?
-----------------------------------
@nathan technically you don't need a for loop, per se...

-----------------------------------
Tony
Sun Sep 28, 2014 11:19 pm

RE:Access array without a for loop?
-----------------------------------
or a loop at all. E.g. recursion or https://en.wikipedia.org/wiki/Loop_unrolling

-----------------------------------
Raknarg
Sun Sep 28, 2014 11:42 pm

RE:Access array without a for loop?
-----------------------------------
How would you do it with recursion?

-----------------------------------
Tony
Mon Sep 29, 2014 3:05 am

RE:Access array without a for loop?
-----------------------------------
writing loops using recursive calls

$ irb
2.1.2 :001 > def go(arr)
2.1.2 :002?>   if arr.any?
2.1.2 :003?>     puts "item #{arr.first}"
2.1.2 :004?>     go(arr

-----------------------------------
amz_best
Mon Sep 29, 2014 6:47 pm

RE:Access array without a for loop?
-----------------------------------
can someone explain to me what this recursion idea is?

-----------------------------------
Tony
Mon Sep 29, 2014 9:00 pm

RE:Access array without a for loop?
-----------------------------------
https://en.wikipedia.org/wiki/Recursion_(computer_science)

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem


-----------------------------------
wtd
Sat Oct 04, 2014 9:56 pm

RE:Access array without a for loop?
-----------------------------------
When you loop in a program, what you're really doing, however the code may express it, is setting an initial state, checking for a condition, performing some set of operations, then doing it all again.

We can do this with a for loop in C.

again it can also repeat this.

[code]void my_function(int i) {
   if (i < 5) {
      printf("%d\n", i);
      my_function(i + 1);
   }
}[/code]

Such that the following will replicate the original for loop's effects.

[code]my_function(0);[/code]

-----------------------------------
wtd
Sat Oct 04, 2014 9:58 pm

RE:Access array without a for loop?
-----------------------------------
Oh, oops... thought this was in C help.  Ah well, concept is the same.

-----------------------------------
Dreadnought
Sat Oct 04, 2014 10:10 pm

Re: Access array without a for loop?
-----------------------------------
Here's the translation into Turing.


When you loop in a program, what you're really doing, however the code may express it, is setting an initial state, checking for a condition, performing some set of operations, then doing it all again.

We can do this with a for loop in C for i : 0 .. 4
    put i
end for

And this will print the following to stdout procedure my_procedure (i : int)
    if (i < 5) then
        put i
    end if
end my_procedure 

And if we call the function again it can also repeat this.

procedure my_procedure  (i : int)
    if (i < 5) then
        put i
        my_procedure (i + 1)
    end if
end my_procedure 

Such that the following will replicate the original for loop's effects.

my_procedure (0)

