Array with copy

Hi guys happy easter,
This is what I am trying to accomplish

Create a 5-element array
Initialize the array with 1, 2, 3, 4, 5
Print the contents
Create a 10-element array
Copy the contents of the 5-element array into the 10-element array
Print the contents


So far this is what I get. i don't think the 10 element arrays output is correct. Help Im a noob too lol go easy on me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
    int ar1[5] = { 1, 2, 3, 4, 5};
    int ar2[10];

    for (int n = 0; n < 5; ++n)
        cout << ar1 [n] << ' ';
    cout << '\n';

    copy(begin(ar1), end(ar1), begin(ar2));

    for (int n = 0; n < 10; ++n)
        cout << ar2[n] << ' ';
    cout << '\n';
   
    cin.get();
    return 0;
}
@nickmcp11

Your program works just fine. The other 5 ar2 array locations contain random junk values. When you created ar2 on line line 7, you could have assigned a beginning value with int ar2[10] = {0};, and you would have seen 5 0's when you printed the 10 values in ar2.
Hello nickmcp11,

Give this 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
29
30
31
32
#include <iostream>
#include <limits>     // <--- Added

#include <algorithm>  // <--- Added. For std::copy + others.

using namespace std;  // <--- Best not to use.

int main()
{
    int ar1[5] = { 1, 2, 3, 4, 5 };
    int ar2[10]{};  // <--- ALWAYS initialize your variables. Especially the arrays and variables that hold a total or sum.

    for (int n = 0; n < 5; ++n)
        cout << ar1[n] << ' ';

    cout << '\n';

    copy(begin(ar1), end(ar1), begin(ar2));

    for (int n = 0; n < 10; ++n)
        cout << ar2[n] << ' ';
    cout << '\n';

    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.

}

You should include "<algorithm>" to use the "std::copy" As is you age getting "std::copy" through "<iostream>" including the header file "<xutility>" or through another header file that yo do not see. Do not count on this happening for every compiler. It is better to include the correct header file and be safe.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <valarray>
using namespace std;

template<typename T>
ostream &operator << ( ostream &out, const valarray<T> &V )
{
   for ( T e : V ) out << e << ' ';
   return out;
}

int main()
{
    valarray<int> a = { 1, 2, 3, 4, 5 };
    cout << a << '\n';
    valarray<int> b(0,10);
    b[slice(0,5,1)] = a;
    cout << b << '\n';
}


1 2 3 4 5 
1 2 3 4 5 0 0 0 0 0 
Last edited on
Topic archived. No new replies allowed.