delphi - dividing an array into 3 individual arrays -


i have 45 values stored in array, sample. needs split 3 individual arrays of size 15, sample1, sample2 , sample3: first 15 items sample1, next 15 sample2 , remaining 15 sample3. tried code:

var   sample: array of integer; // source array contains data   sample1, sample2, sample3: array of integer; //target arrays needs worked upon   i: integer; begin  setlength(sample1, 15);  setlength(sample2, 15);  setlength(sample3, 15);   := 0 14    sample[i] := sample1[i];  i:= 15 29    sample[i] := sample2[i];    i:= 30 44    sample[i] := sample3[i];  := + 1; 

i able results in first array, not in other arrays. doing wrong?

if understand well, following want. assume sample array has 45 items, want this:

var    sample: array of integer;   sample1, sample2, sample3: array of integer;   i: integer; begin   setlength(sample, 45);   { fill sample values }   ...   { split: }   setlength(sample1, 15);   setlength(sample2, 15);   setlength(sample3, 15);   := 0 14   begin     sample1[i] := sample[i];     sample2[i] := sample[i + 15]; { = 0..14, i+15 = 15..29 }     sample3[i] := sample[i + 30]; { = 0..14, i+30 = 30..44 }    end; 

that should trick. if not wanted, should specify problem bit better. if sample array longer, won't split of it. if sample array shorter, you'll overflow, causing error or undefined behaviour.


Comments