Thread: Swapping Elements in an Array

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    17

    Swapping Elements in an Array

    I thought I'm done doing mg activity, but my professor said that we need to use a Temporary Variable for Swapping and I have no idea where to put it. Mind giving me a hand, please?


    Here is his activity:


    Activity: Swapping
    Create a program that accepts a 10-element array of type int. Where the 1st user-input is stored in the 1st element of the array; the 2nd user-input is stored in the 2nd element of the array; so on so forth until the last user-input stored in the last element of the array.
    Your source code should be able to SWAP the values of the 1st and 10th; 2nd and 9th; 3rd and 8th; 4th and 7th; and 5th and 6th elements. It should display the values of the original and the swapped values of the array.
    example:
    Enter 10 integer values:
    array[0] = 1
    array[1] = 2
    array[2] = 3
    array[3] = 4
    array[4] = 5
    array[5] = 6
    array[6] = 7
    array[7] = 8
    array[8] = 9
    array[9] = 10
    Swapped:
    array[0] = 10
    array[1] = 9
    array[2] = 8
    array[3] = 7
    array[4] = 6
    array[5] = 5
    array[6] = 4
    array[7] = 3
    array[8] = 2
    array[9] = 1
    NOTE: SWAPPING is DIFFERENT from REVERSE.The values of each element in the array should be swapped. I'll check your codes next meeting.


    MY CODE:
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main ()
    {
        int array [10];
        
        for ( int i = 0; i <=9; i++ )
        {
            cout <<"array [" << i << "] = "; cin >> array [i];
            }
            
        cout <<"Swapped: \n";
        
        cout <<"array [0] = " << array [9];
        cout << endl;
        cout <<"array [1] = " << array [8];
        cout << endl;
        cout <<"array [2] = " << array [7];
        cout << endl;
        cout <<"array [3] = " << array [6];
        cout << endl;
        cout <<"array [4] = " << array [5];
        cout << endl;
        cout <<"array [5] = " << array [4];
        cout << endl;
        cout <<"array [6] = " << array [3];
        cout << endl;
        cout <<"array [7] = " << array [2];
        cout << endl;
        cout <<"array [8] = " << array [1];
        cout << endl;
        cout <<"array [9] = " << array [0];
        cout << endl;
        
            
        getch ();
        return 0;
        
    }


    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should write a loop to do that swapping. The variable that your professor suggested can then be declared just before the loop or inside the loop body.

    By the way, when your loop condition should be i < 10 instead of i <= 9. This will come in handy when you use a named constant for the array size instead of magic numbers like 9 or 10. Of course, for the loop that will reverse the elements of the array, you need to think about what the condition will be.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Call by Reference: Swapping Array Elements
    By Tripswitch in forum C Programming
    Replies: 8
    Last Post: 07-15-2014, 05:07 AM
  2. Swapping elements in Arrays
    By november1992 in forum C Programming
    Replies: 11
    Last Post: 03-10-2012, 01:58 AM
  3. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 01:53 PM
  4. Replies: 12
    Last Post: 08-04-2008, 08:27 PM
  5. swapping elements in a 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2003, 02:18 PM

Tags for this Thread