Not sure why this isn't working.

For whatever reason, I had to declare int I before the for loop due to some error code, however when I try to run this, I am only getting the first numbers switched in each array. I have to switch a total of 2 arrays, A and B switch, and array C is reversed. Not sure why this is happening.

int I;
for (int I; I < 7; I++)
{
temp1 = Array_A[I];
Array_A[I] = Array_B[I];
Array_B[I] = temp1;
}
cout << "Array A is: " << Array_A[I] << " " << "Array B is: " << Array_B[I] << endl;
You need to set the initial value of I:
for (int I=0; I < 7; I++)
Note that this variable I only exists inside the loop. That brings us to your next problem, the last line is OUTSIDE the loop, and that's why you had to declare int I at the beginning of the program. Note that this is a completely different variable from the one inside of the for loop.

I think you want to print the full contents of the arrays, which means you'll want another loop to do it.
I actually forgot about the I = 0 for the post. I am trying to swap a 7 value array with another 7 value array, however when I try this, it only prints out the first number from each array. Should I change the for loop to a while loop or just copy the
cout << "Array A is: " << Array_A[I] << " " << "Array B is: " << Array_B[I] << endl;

and just add a +1, +2, +3..... +6 to the [I]?
closed account (1yvU5Di1)
What do you mean by "swap" exactly?
Do you want a[0] to become b[0] while b[0] becomes a[0]
If so...

1
2
3
4
5
6
7
8
9

for (int I = 0; I < 7; I++)
{
temp1 = Array_A[I];
temp2 = Array_B[I];

Array_A[I] = temp2;
Array_B[I] = temp1;
}
Like I said, you need a second loop to print the arrays, or you need to put the printing line inside the loop.
Topic archived. No new replies allowed.