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:

#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. :)

Recommended Answers

All 4 Replies

#include <iostream>
#include <conio.h>
using namespace std;
int main ()

    for ( int i = 0; i <=9; i++ )
    {
        cout <<"array [" << i << "] = "; cin >> array [i];
    }
    for ( int i = 0; i <=9; i++ )
    {
        for (int j=0 ; j<=9 ; j++)
        {
           if (array[i]<array[j])
           {
               int temp;
               temp=a[i];
               a[i]=a[j];
               a[j]=temp;
           }
        }
    }
    for ( int i = 0; i <=9; i++ )
    {
        cout << array [i];
    }
    cout << endl;
    getch ();
    return 0;
}
start from
int main (){
    int array [10];
    ....

It does not :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.