Swapping the elements in an array using functions.

Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
using namespace std;

void SwapArrayEnds(int sortArray[],int SORT_ARR_SIZE)
{
   for (int i=0;i<SORT_ARR_SIZE;i++)
   {
      if (i=0)
      {
         sortArray[i]=sortArray[SORT_ARR_SIZE-1];
      }
      else if (i=SORT_ARR_SIZE-1)
      {
         sortArray[i]=sortArray[SORT_ARR_SIZE-SORT_ARR_SIZE];
      }
      else
      {
         sortArray[i]=sortArray[i];
      }
   }
}

int main() {
   const int SORT_ARR_SIZE = 4;
   int sortArray[SORT_ARR_SIZE];
   int i = 0;

   sortArray[0] = 10;
   sortArray[1] = 20;
   sortArray[2] = 30;
   sortArray[3] = 40;

   SwapArrayEnds(sortArray, SORT_ARR_SIZE);

   for (i = 0; i < SORT_ARR_SIZE; ++i) {
      cout <<  sortArray[i] << " ";
   }
   cout << endl;

   return 0;
}


My code is from line 4 to 21.

I tried the above code. Everything is working fine but only the first element in the array is not swapping with the last element. Please explain me where I was wrong.
Last edited on
Hi philip,

First thing to understand is that = is the assignment operator, and == tests for equality.
At both line 8 and 12, you are assigning i a value, instead of checking for equality.

Second thing to understand is that the following line of code does absolutely nothing.
sortArray[i]=sortArray[i];
What's happening here, is that the current value of sortArray is being assigned to sortArray, which is itself.

I think you're over-complicating this in one way, but forgetting an important detail.

If you're only swapping the first and last elements of the array, you don't need a for loop. No matter how huge your array is, you're only going to be working with two elements: array[0] and array[Size-1].

In order to successfully swap two elements, you need to save the first element before you overwrite its value.

Here is an example of swapping two variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    int a = 42;
    int b = 19973;

    int temp = a; // we need to store a copy of a before we re-assign a to be a new number
    a = b;       // a becomes b
    b = temp;   // b becomes the original value of a
               // (if we had done "b = a;" here, that would have just made b become its original value,
              // because a was already assigned the value of b.)
    std::cout << " a = " << a << ", b = " << b << "\n";
}


Can you apply your array to this?
Last edited on
if you are trying to sort, youll want to initialize in unsorted order, not 10/20/30/40 which is sorted already. (going off your function name and common use of the word sort).

c++ contains both sorting and swapping algorithms. These are very good things to know how to do yourself as these algorithms are building blocks for many other things that are not part of languages, and they help learn to 'think like a programmer'. But you should be aware that they are in there.

not sure what you are really trying to do (unless this is it, just a swapping exercise?). get your swap working first, and get back to us if you need more help.
Last edited on
Topic archived. No new replies allowed.