1

I am new to C++. I am trying to solve a problem in the textbook: swap the first and last element in an array. But when I run the code I wrote, nothing happened and even the sentence "Please enter the numbers in the array: " does not show up. Anyone could give some help? Thanks.

#include <iostream>

using namespace std;

int swap(int values[], int size)
{
    int temp = values[0];
    values[0] = values[size-1];
    values[size-1] = temp;
}

int main()
{
    const int SIZE = 5;
    int test[SIZE];
    cout << "Please enter the numbers in the array: " << endl;
    int input;
    cin >> input;
    for(int i=0; i<SIZE; i++)
    {
            test[i] = input;
    }
    swap(test, SIZE);
    cout << test[SIZE] << endl;
    return 0;
}
6
  • There are some output on Wandbox. Anyway, cout << test[SIZE] << endl; acccess out of bounds and it is bad. Commented Oct 1, 2015 at 15:05
  • 1
    swap() has a return type other than void but does not return anything. Commented Oct 1, 2015 at 15:11
  • Moreover, you fill the array with the same single input value in all cells. Swaping the cells yield no visible result. Commented Oct 1, 2015 at 15:13
  • 2
    Are you aware that the name swap already exists in namespace std, which you're bringing into the global scope with your using namespace std;? It won't cause problems here because overload resolution picks your function, but you'll run into clashes in future if you insist on using namespace std. Commented Oct 1, 2015 at 15:26
  • 1
    It is an anti-pattern and a bad habit to use constants in UPPERCASE (unless it is a preprocessor macro), please avoid that. Commented Oct 1, 2015 at 15:28

2 Answers 2

1

There were a few mistakes:

  • You should get the input inside the loop and then assign it to the test array.
  • When printing the swapped value, access the test array with SIZE-1 instead of SIZE, because array indexes run from 0 to SIZE-1, inclusive.
  • You declared swap() as returning int, but provided no return statement (this suggests that you haven't enabled enough warnings from your compiler).

    #include <iostream>
    
    using namespace std;
    
    void swap(int values[], int size)
    {
        int temp = values[0];
        values[0] = values[size-1];
        values[size-1] = temp;
    }
    
    int main()
    {
        const int SIZE = 5;
        int test[SIZE];
        int input;
        cout << "Please enter the numbers in the array: " << endl;
    
        for(int i=0; i<SIZE; i++)
        {
                cin >> input;
                test[i] = input;
        }
        swap(test, SIZE);
        cout << test[SIZE-1] << endl;
        return 0;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1
#include <iostream>

using namespace std;

//Here return type should be void as you are not returning value.
void swap(int values[], int size)
{
   int temp = values[0];
   values[0] = values[size-1];
   values[size-1] = temp;
}

int main()
{
   const int SIZE = 5;
   int test[SIZE];
   cout << "Please enter the numbers in the array: " << endl;

   //USE LOOP TO TAKE INPUT ONE BY ONE IN AN ARRAY
   for(int i = 0; i < SIZE; i++)
    cin >> test[i];

   swap(test, SIZE);

   //USE LOOP TO DISPLAY ELEMENT ONE BY ONE
   for(int i = 0; i < SIZE; i++)
     cout << test[i] << endl;

   return 0;
}

2 Comments

Perhaps size instead of SIZE? ALL-CAPS identifiers are best reserved for macros.
And in C++11: for(auto& e : test) std::cin >> e; swap(test, SIZE); for(auto e : test) std::cout << e << std::endl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.