swapping elements between to arrays with eachother?

This is a discussion on swapping elements between to arrays with eachother? within the C Programming forums, part of the General Programming Boards category; i have two size 5 arrays. the user types in two numbers, the first number represents the index of the ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    20

    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("%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.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    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.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You don't initialize the tables.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    20
    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]

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  9. #9
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Quote Originally Posted by Adak View Post
    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.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cas View 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.
    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:

    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?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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.
    Last edited by robwhit; 08-04-2008 at 01:07 PM.

  12. #12
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    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.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It came through clear to me, just from reading the post. Maybe I'm getting clairvoyant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 11:20 PM
  2. Help swapping 2 arrays
    By Boboki in forum C Programming
    Replies: 8
    Last Post: 03-03-2005, 05:07 AM
  3. summing an array's elements
    By gomindi in forum C++ Programming
    Replies: 10
    Last Post: 11-21-2003, 03:37 PM
  4. swapping elements in a 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2003, 01:18 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21