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
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.
#include <iostream>
#include <limits> // <--- Added
#include <algorithm> // <--- Added. For std::copy + others.
usingnamespace 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.