Re: Dynamic array and foreach loop

2015-08-09 Thread Jay Norwood via Digitalmars-d-learn
On Sunday, 9 August 2015 at 19:10:01 UTC, Binarydepth wrote: On Sunday, 9 August 2015 at 16:42:16 UTC, Jay Norwood wrote: Oooh... I like how this works import std.stdio : writeln, readf; void main() { immutable a=5; int[a] Arr; int nim; foreach(num, ref nem; A

Re: Dynamic array and foreach loop

2015-08-09 Thread Binarydepth via Digitalmars-d-learn
On Sunday, 9 August 2015 at 16:42:16 UTC, Jay Norwood wrote: The i+3 initialization is just so you can see that v is the Arr member (not the index) in the other loops. import std.stdio : writeln; void main() { immutable a=5; int[a] Arr; foreach(i, ref v; Arr) {

Re: Dynamic array and foreach loop

2015-08-09 Thread Jay Norwood via Digitalmars-d-learn
On Sunday, 9 August 2015 at 15:37:23 UTC, Binarydepth wrote: So I should use the REF like this ? import std.stdio : writeln; void main() { immutable a=5; int[a] Arr; foreach(num; 0..a) { Arr[num] = num; } foreach(num, ref ele; Arr)

Re: Dynamic array and foreach loop

2015-08-09 Thread Alex Parrill via Digitalmars-d-learn
On Sunday, 9 August 2015 at 15:37:23 UTC, Binarydepth wrote: So I should use the REF like this ? import std.stdio : writeln; void main() { immutable a=5; int[a] Arr; foreach(num; 0..a) { Arr[num] = num; } foreach(num, ref ele; Arr

Re: Dynamic array and foreach loop

2015-08-09 Thread Binarydepth via Digitalmars-d-learn
On Sunday, 9 August 2015 at 00:22:53 UTC, Jay Norwood wrote: On Saturday, 8 August 2015 at 18:28:25 UTC, Binarydepth wrote: This is the new code : foreach(num; 0..liEle) {//Data input loop write("Input the element : ", num+1, " "); readf(" %d", &liaOrig

Re: Dynamic array and foreach loop

2015-08-08 Thread Jay Norwood via Digitalmars-d-learn
On Saturday, 8 August 2015 at 18:28:25 UTC, Binarydepth wrote: This is the new code : foreach(num; 0..liEle) {//Data input loop write("Input the element : ", num+1, " "); readf(" %d", &liaOrig[num]); } Even better : foreach(num; 0..liaOrig.len

Re: Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
On Saturday, 8 August 2015 at 18:24:48 UTC, Binarydepth wrote: On Saturday, 8 August 2015 at 17:19:08 UTC, DarthCthulhu wrote: Now 'num' is just an iterative number starting from 0 (the .init value of an int), while the actual element value is stored in 'element'. I added the writefln() stat

Re: Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
On Saturday, 8 August 2015 at 17:19:08 UTC, DarthCthulhu wrote: You can fix it like the following: foreach(num, element; liaOrig) {//Data input loop writefln("num: %s current element: %s liaOrig.length: %s", num, element, liaOrig.length); write("In

Re: Dynamic array and foreach loop

2015-08-08 Thread DarthCthulhu via Digitalmars-d-learn
On Saturday, 8 August 2015 at 15:57:15 UTC, Binarydepth wrote: Here's what happens : How many elements need to be used? 5 Input the element : 1 1 Input the element : 1 2 Input the element : 1 3 Input the element : 1 4 Input the element : 1 5 How many positions do you wish to rotate ? 3 The origi

Re: Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
Here's what happens : How many elements need to be used? 5 Input the element : 1 1 Input the element : 1 2 Input the element : 1 3 Input the element : 1 4 Input the element : 1 5 How many positions do you wish to rotate ? 3 The original patter is : 5 0 0 0 0 The final is : 0 0 0 5 0 Do you want t

Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
I'm writing a program that rotates numbers then asks the user if a new set of numbers should be rotated. I'm having trouble using a Foreach loop to fill a dynamic array with the elements to be rotated. Here's my code, I add a TAB when a loop is inside a loop and and do that too to the stateme