zane5
zane5

Reputation: 13

Rearranging Elements in Arrays in C++

I am creating a program that has a user enter in the number of elements they want to input in the array, then the user enters that number of values. The program is supposed to output the original indexes of the array, and then also output an arrangement were the 1st and last element of the array trade places. For example if the user entered in the elements, 1 2 3 4, the output would display that and 4 2 3 1. Any tips? My program currently rearranges all the elements so that it outputs 3 4 1 2.

    #include<iostream>
using namespace std;
void swapfrontback(int a[], int n);//function declaration
int main()
{

    int a[10], i, n;
    cout << "enter size" << endl; //size of array
    cin >> n;
    if (n == 0)
    {
        cout << "Array is empty!\n";
    }
    else
    {
        cout << "enter elements" << endl;//input the array elements by user
        for (i = 0; i < n; i++)
        {

            cin >> a[i];
        }
    }
    swapfrontback(a, n);//calling swap function
    return 0;
}
void swapfrontback(int a[], int n)//function defintion
{
    int i, temp;
    for (i = 0; i < n / 2; i++)
    {
        temp = a[i];
        a[i] = a[n / 2 + i];
        a[n / 2 + i] = temp;
    }

    cout << "swapping array is" << endl;

    for (i = 0; i < n; i++)
    {
        cout << a[i] << endl;
    }
}

Upvotes: 0

Views: 397

Answers (2)

user8234870
user8234870

Reputation:

#include<iostream>
using namespace std;
void swapfrontback(int a[], int n);//function declaration
void swap(int [],int);
int main(){
    int a[10], i, n;
    cout << "enter size" << endl; //size of array
    cin >> n;
    if (n == 0)
        cout << "Array is empty!\n";
    else{
        cout << "enter elements" << endl;//input the array elements by user
        for (i = 0; i < n; i++)
            cin >> a[i];
    }
    //Will swap all elements
    swapfrontback(a, n);//calling swap function
    //Will swap only first ans last
    swap(a,n);
    return 0;
}
//Will swap all elements
void swapfrontback(int a[], int n){
    int i, temp;
    for (i = 0; i < n / 2; i++){
        temp = a[i];
        a[i] = a[n-i-1];
        a[n-i-1] = temp;
    }
    cout << "swapping array is" << endl;
    for (i = 0; i < n; i++){
        cout << a[i]<<" ";
    }
    cout<<endl;
}
//Swaps only first and last
void swap(int arr[],int n){
    int temp=arr[0];
    arr[0]=arr[n-1];
    arr[n-1]=temp;
}

Upvotes: 0

AziMez
AziMez

Reputation: 2072

All you need is to correct the index from [n / 2 + i] to [n - i-1]

Corrected Code here:

#include<iostream>
using namespace std;
void swapfrontback(int a[], int n);//function declaration
int main()
{

    int a[10], i, n;
    cout << "enter size" << endl; //size of array
    cin >> n;
    if (n == 0)
    {
        cout << "Array is empty!\n";
    }
    else
    {
        cout << "enter elements" << endl;//input the array elements by user
        for (i = 0; i < n; i++)
        {

            cin >> a[i];
        }
    }
    swapfrontback(a, n);//calling swap function
    return 0;
}
void swapfrontback(int a[], int n)//function defintion
{
    int i, temp;
    for (i = 0; i < n / 2; i++)
    {
        temp = a[i];
        a[i] = a[n - i-1];
        a[n - i-1] = temp;
    }

    cout << "swapping array is" << endl;

    for (i = 0; i < n; i++)
    {
        cout << a[i] << endl;
    }
}

Upvotes: 1

Related Questions