-2

I need to write a C++ program where it swaps between two 1-dimensional arrays using pointers and functions. Firstly, a void function named showValues to display both arrays before swapping takes and also a void function named swap to swap the elements between both arrays.

My question is: I'm supposed to swap the function but for some reason it wont run and I am not sure where is the error in my code

#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 5;
void showValues(int[],int[]);
void swap(int[],int[]);

int main() {
    
    int array1[SIZE] = {10,20,30,40,50};
    int array2[SIZE] = {60,70,80,90,100};
    
    showValues (array1, array2);
    swap(array1, array2);
    
    return 0;
    
}

void showValues(int array1[], int array2[]){
    
    cout<<"The original arrays are as shown below: " << endl;
    cout << " Array 1 is: ";
    for (int i = 0; i < 5; ++i) {
        cout << array1[i] << "  ";
    }
    cout << "\n Array 2 is: ";
    for (int i = 0; i < 5; ++i) {
        cout << array2[i] << "  ";
    }
}

void swap(int array1[], int array2[])
{
    
    int temp,i;
    for(i=0; i<5; ++i)
    {
        temp = array1[SIZE];
        array1[SIZE] = array2[SIZE];
        array2[SIZE] = temp;
    }
    cout << "\nThe swapped arrays are as shown below: " << endl;
    cout << " Array 1 is: ";
    for (int i = 0; i < 5; ++i) {
        cout << array1[i] << "  ";
    }
    cout << "\n Array 2 is: ";
    for (int i = 0; i < 5; ++i) {
        cout << array2[i] << "  ";
    }
}
Moss
  • 6,002
  • 1
  • 35
  • 40
ian moone
  • 5
  • 3
  • 4
    You seem to have forgotten to ask a question. – Igor Tandetnik Jul 19 '20 at 01:21
  • In the `swap()` function, the statement `temp = array1[SIZE];` doesn't do what you think it does. Neither do the next two statements, either. None of them do what you think they do. – Sam Varshavchik Jul 19 '20 at 01:26
  • I'm supposed to swap the function but for some reason it wont run and I am not sure where is the error in my code – ian moone Jul 19 '20 at 01:29
  • Next time it would be immensly useful to step through the code in a debugger. Then you'd notice that `temp` contains a garbage value, and while investigating why that is you'd notice that `SIZE` equals `5` and hence you are accessing `array1[5]` which is beyond the end of the array (as its last element would be `array1[4]`). – CherryDT Jul 19 '20 at 01:40
  • 3
    Do not call your function `swap`, and at the same time have `using namespace std;`. There is a good chance your code will call `std::swap` instead of your own version. – PaulMcKenzie Jul 19 '20 at 01:43

2 Answers2

0

This part of your code doesn't make sense:

    temp = array1[SIZE];
    array1[SIZE] = array2[SIZE];
    array2[SIZE] = temp;

SIZE is 5. So, you are accessing array1[5] and array2[5], i.e. the 6th element of the array. Yet, your arrays have only 5 elements to begin with (array1[0] to array1[4], same for array2), so you are accessing elements beyond the end of the array, which is undefined behavior that is probably just corrupting memory somewhere!

You probably meant to use i here, not SIZE, then the code makes sense. Instead, it would be useful to replace the "magic number" 5 with SIZE:

for(i = 0; i < SIZE; ++i)
{
  temp = array1[i];
  array1[i] = array2[i];
  array2[i] = temp;
}
CherryDT
  • 25,571
  • 5
  • 49
  • 74
0

The void swap(int array1[], int array2[]) function is where you are having trouble. You actually don't even need to have another function for the swapping. You could just use std::swap() which is defined in the #include <utility> header. Since both arrays have the same size.

For example you could do something along these lines:

#include <iostream>
#include <iomanip>
#include <utility>
const int SIZE = 5;
void showValues(int[], int[]);
void swap(int[], int[]);

int main() {

    int array1[SIZE] = { 10,20,30,40,50 };
    int array2[SIZE] = { 60,70,80,90,100 };
    int n = sizeof(array1) / sizeof(array2[0]);

    showValues(array1, array2);
    std::swap(array1, array2);

    std::cout << "\n\nThe swapped arrays are as shown below:\n ";
    std::cout << "\nArray 1 is: ";
    for (int i = 0; i < n; i++)
        std::cout << array1[i] << ", ";

    std::cout << "\nArray 2 is: ";
    for (int i = 0; i < n; i++)
       std::cout << array2[i] << ", ";

 
    return 0;

}

void showValues(int array1[], int array2[]) {

    std::cout << "The original arrays are as shown below: " << std::endl;
    std::cout << "\nArray 1 is: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << array1[i] << "  ";
    }
    std::cout << "\nArray 2 is: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << array2[i] << "  ";
    }
}

Also consider not using using namespace std;.

Geno C
  • 1,401
  • 3
  • 11
  • 26
  • I don't think if we must need to import `utility` header file. The `std::swap()` already exists even without using it. – Rohan Bari Jul 19 '20 at 04:50
  • 1
    @RohanBari, indeed. However, it is general not a good idea to rely on other header files that have other header files. Especially if you want your code to be portable and compilable. – Geno C Jul 19 '20 at 06:46