简体   繁体   中英

Swapping two initialized arrays in C++ using Void and Pointers

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] << "  ";
    }
}

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] , ie 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;
}

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; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM