t

t

  • NA
  • 1
  • 0

C#: For Loop & 2 Arrays

Oct 5 2007 10:06 PM
Thanks for your time!

.Net 2.0

Having problems on second iteration of for loop.

Output looks like this:

arrImgIT[0*2] = 0 = 3 = arrImgI[4+0] = 4 = 3
arrImgIT[0+1] = 1 = 0 = arrImgI[0] = 0

arrImgIT[1*2] = 2 = SHOULD BE 2
arrImgIT[1*2] = 2 = 1 = arrImgI[4+1] = 5 = 2
arrImgIT[1+1] = 2 = 1 = arrImgI[1] = 1

arrImgIT[2*2] = 4 = 1 = arrImgI[4+2] = 6 = 1
arrImgIT[2+1] = 3 = 2 = arrImgI[2] = 2

arrImgIT[3*2] = 6 = 0 = arrImgI[4+3] = 7 = 0
arrImgIT[3+1] = 4 = 3 = arrImgI[3] = 3



CODE:

//arrImgI array stores indices of arrImg twice to represent the pairs.
//Items will be shuffled later.
int[] arrImgI ={ 0, 1, 2, 3, 3, 2, 1, 0 };


//Test
string strTest="";

int intCut = arrImgI.Length / 2;

//Test
//strTest="intCut = " + Convert.ToString(intCut);
strTest="intCut = " + intCut;
MessageBox.Show(strTest);
//Test

//arrImgIT is a temporary containers use for shuffle & cut.
int[] arrImgIT= new int[arrImgI.Length];

//Test
strTest = null;

for (int i = 0; i < intCut; i++)
{
//1st (0), 3rd (2), 5th (4), etc. items equal to last half of deck.
arrImgIT[i*2] = arrImgI[intCut+i];

//2nd (1), 4th (3), 6th (5), etc. items equal to first half of deck.
arrImgIT[i + 1] = arrImgI[i];

//Test
strTest += "arrImgIT[" + i + "*2] = " + (i*2) + " = " + arrImgIT[i * 2] + " = arrImgI[" + intCut + "+" + i + "] = "
+ (intCut+i) + " = " + arrImgI[intCut+i] + "\n";
strTest += "arrImgIT[" + i + "+1] = " + (i + 1) + " = " + arrImgIT[i + 1] + " = arrImgI[" + i + "] = " + arrImgI[i] + "\n";
}

//Test
//MessageBox.Show(strTest);
txtTest.Text = strTest;
//Test

Answers (1)