swapping elements between to arrays with eachother?

• 08-03-2008
rs07
swapping elements between to arrays with eachother?
i have two size 5 arrays. the user types in two numbers, the first number represents the index of the first array that will be swapped and the second number represents the index of the second array that will be swapped (these two indexes will be swapped together). so say an array as the numbers [1,2,3,4,5] and the other has the numbers [6,7,8,9,10] and the user types in 2 and 4, then the new arrays will be [1,9,3,4,5] and [6,7,8,2,10].

this is the way i'm doing it:

int table_A[5];
int table_B[5];
int indexA;
int indexB;
int tempInd;

printf("Enter index number in table_A to be swapped\n");
scanf("&#37;d", &indexA);
printf("Enter index number in table_B to be swapped\n");
scanf("%d", &indexB);
tempInd = table_A[indexA];
table_A[indexA] = table_B[indexB];
table_B[indexB] = tempInd;

its not working though, whats wrong? thanks.
• 08-03-2008
cas
When you're asking a question, a variation on "it doesn't work" is very vague and unhelpful. You should explain what is actually happening versus what you expect to happen.

I can make a guess as to what your problem is, but I'm not certain. In C, array indexing starts at zero. Thus if a user enters 2, that means the 3rd element. Modify your indices appropriately and you might get what you're after.
• 08-03-2008
MacGyver
It appears fine. Try showing a full, but very short, program that demonstrates the problem. And btw, while you're at it, explain what the actual problem is.
• 08-03-2008
robwhit
You don't initialize the tables.
• 08-03-2008
rs07
sorry guys. basically it looks like it is swapping something, but not the right ones.

for example the original arrays were [5.7.2.8.1] and [3,6,9,0,2] and I input element 3 in table_A to be swapped with element 4 in table_B.

and the arrays it gave me were [5, 7, 2, 2, 1] and [3, 6, 9, 0, 8].

it looks like its swapping the 3rd element in the first array with the last element in the 2nd array.

another one it gave me:

[1, 7, 9, 2, 3]
[3, 4, 1, 9, 0]
input 2, 4
gave me: [1, 7, 0, 2, 3], [3, 4, 1, 9, 9]
• 08-03-2008
Richie T
Array indices are zero based - A 5 element array has indices 0, 1, 2, 3 and 4. Subtract 1 from the numbers the user provides and it'll behave in the manner you want.
• 08-03-2008
MacGyver and cas, you need to get some coffee. He explained *exactly* what his problem was, gave an example of what the right answer should be, and also gave you a small code sample that showed the wrong answer he was getting.

Right in his first post.
• 08-03-2008
MacGyver
Indeed. Can I have double the caffeine in mine?

My apologies to the OP and anyone else that was forced to read through my stupidity here.
• 08-03-2008
cas
Quote:

MacGyver and cas, you need to get some coffee. He explained *exactly* what his problem was, gave an example of what the right answer should be, and also gave you a small code sample that showed the wrong answer he was getting.

Right in his first post.

Not quite. As an example, someone mentioned that his tables weren't initialized. Which, in the snippet he provided, was true. So it's possible he was getting junk numbers, or the wrong numbers swapped, or no output at all (because the posted snippet did not actually output anything), or something else...

I made a guess as to the problem which happened to be correct, but there are any number of things that could have been going wrong.
• 08-04-2008
Quote:

Originally Posted by cas
Not quite. As an example, someone mentioned that his tables weren't initialized. Which, in the snippet he provided, was true. So it's possible he was getting junk numbers, or the wrong numbers swapped, or no output at all (because the posted snippet did not actually output anything), or something else...

I made a guess as to the problem which happened to be correct, but there are any number of things that could have been going wrong.

Quite, actually.

He showed what the error in the result was, described what the correct answer should have been, and posted the code that produced it. You said the OP was "vague and unhelpful" because he did not do that:

Quote:

When you're asking a question, a variation on "it doesn't work" is very vague and unhelpful. You should explain what is actually happening versus what you expect to happen.
Now you're trying to defend your misstatement, as if you had not posted the part quoted above?

Who do you think you are, a politician? :)
• 08-04-2008
robwhit
In the OP's first post, nothing was mentioned about what was happening versus what was supposed to be happening. That information came in the OP's second post. But the code still did not show the behavior that was described.
• 08-04-2008
cas
Quote:

He showed what the error in the result was, described what the correct answer should have been, and posted the code that produced it. You said the OP was "vague and unhelpful" because he did not do that:
He did not post code that produced the answer, though. The code he posted did not compile, and so in order to get a complete program that exhibits any sort of behavior, assumptions have to be made about the missing bits. There's no way to know if he was having other issues apart from not understanding how arrays are indexed. In fact, because he's new to C, I would find it quite likely that other mistakes would be made.
• 08-04-2008