Swapping Arrays.

Hello. I was wondering how to swap array in place of another array. For example, if my array is 1, 4 , 9, 10, 15. I want to produce 15, 10, 9, 4 , 1. This is my attempt on it:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;


void initializelist(int list[],int size, int a, int b)
{
    int counter;

    for (counter = 0; counter < size; counter++)
    {
        list[counter] = rand()%(b-a+1)+a;
    }
}
void printlist (int list[], int size)
{
    int counter;
    int c[7];

    for (counter = 0; counter < size; counter++)
    {
        cout<<list[counter]<<" ";
        c[counter]= list[counter];
    }
    cout<<endl;
}
void swap(int list[],int  list2[], int size)
{
    int counter;
    int c[7];
    c[7]= list[7];
    list[7] = list2[7];
    list2[7] = c[7];
    for (counter = size; counter >= size; counter--)
    {
        cout<<c[counter]<<" ";
    }
}
int main()
{
    srand(time(0));

    int list[7];
    int list2[7];


    initializelist(list,7, 20, 40);
    cout<<"List 1: ";
    printlist(list, 7);
    swap(list, list2, 7);
    return 0;
}
Last edited on
Line 33: Where does the 7 come from?
Lines 33-37: Is this supposed to be a swap sequence? How is this supposed to work, even for simple variables?
1
2
3
4
5
int temp;
a = b; // <-- ?
temp = a;
a = b;
b = temp;


You can't move around arrays in a single assignment. You have to use a loop to swap each pair of elements individually. std::swap(a[i], b[i])
My bad, I edited line 34. So, do you mean I need to use a loop instead of assigning array variables?
Your edited line 34 results in undefined behavior. 7 is not a valid index into c. (Nor is it a valid index into list.
Yeah I just realised that now, list[7] would only replace the last character or, in this case undefined. But, I have no idea of how to fix it. I've tried list[counter] while passing by reference of counter. Still didn't work.
You don't need a whole temp array. Swapping the elements of two arrays of T only requires a single extra T for swap temp.
CppCoreGuidelines:
Prefer using STL array or vector instead of a C array

Reason
C arrays are less safe, and have no advantages over array and vector. For a fixed-length array, use std::array, which does not degenerate to a pointer when passed to a function and does know its size. For a variable-length array, use std::vector, which additionally can change its size and handles memory allocation.

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rsl-arrays


In addition, both std::array<> and are std::vector<> are swappable types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <array>
#include <vector>

int main()
{
    {
        std::array<int,5> a = { 0, 1, 2, 3, 4 } ;
        std::array<int,5> b = { 5, 6, 7, 8, 9 } ;
        a.swap(b) ; // http://en.cppreference.com/w/cpp/container/array/swap
    }

    {
        std::vector<int> a = { 0, 1, 2, 3, 4, 5 } ;
        std::vector<int> b = { 6, 7, 8, 9 } ;
        a.swap(b) ; // http://en.cppreference.com/w/cpp/container/vector/swap
    }
}
C++ already has a way to swap arrays:

std::swap(list, list2);

give it a try:

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
#include <iostream>
#include <utility>
#include <algorithm>
#include <random>

template<int N>
void print(int(&a)[N]) { for(int n: a) std::cout << n << ' '; std::cout << '\n'; }

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(20, 40);
    
    int list[7];
    int list2[7];

    std::generate_n(list, 7, [&]{return dis(gen);});
    std::generate_n(list2, 7, [&]{return dis(gen);});

    std::cout << "List 1: "; print(list);
    std::cout << "List 2: "; print(list2);

    std::swap(list, list2);
    
    std::cout << "List 1: "; print(list);
    std::cout << "List 2: "; print(list2);
}

demo: http://coliru.stacked-crooked.com/a/f03564a7ca14450d
Thanks. Never realised that C++11 <utility> had an overload of std::swap for arrays.
Oh, thanks guys, I figured it out. But, I lack the understanding of "temp" function. I mean is it really a function that is imprinted in the C++ programs. And what does it do?
1
2
3
4
5
char i[]={1, 4, 9, 10, 15};
char reverse[5];
char c=0;
for(char *iPtr=i + (strlen(i)-1);iPtr>=i;i--)
      reverse[c++]=*iPtr;

can convert to int with atoi
"C arrays are less safe, and have no advantages over array and vector."
disagree %100 - if you know the size of data you will store arrays are much faster
"Vectors are much Higher level constructs, internally they have many moving parts compared to a simple array " -Quara
and unsafe? - the entirety of C is based off pointers to arrays - so you just called C - which runs your C++ compilers unsafe
just because C/C++ doesn't have bounds checking doesn't mean it is unsafe it actually lets you do a lot more and is like that for a reason
if you know the size of data you will store arrays are much faster
Are you saying that C arrays will be faster than std::arrays? How is that possible?

and unsafe? - the entirety of C is based off pointers to arrays - so you just called C - which runs your C++ compilers unsafe
C is unsafe. It's weakly typed, so the compiler will let you call a flood fill function on a bignum without any warnings; it provides practically no mechanisms to enforce invariants at compile time; it provides no form of automatic resource management for any non-trivial type.

just because C/C++ doesn't have bounds checking doesn't mean it is unsafe it actually lets you do a lot more and is like that for a reason
C is the way it is as a direct consequence of the time when it was designed. Not everything in it is the way it is because that was the best way to do it. Some things are like that because of historical reasons, and some things are because there was just no other way to make compilers that could terminate in a reasonable time. Could you imagine compiling even a simple Boost.Spirit program on a PDP-11?
Topic archived. No new replies allowed.