One more thing: Several things:
Avoid having magic numbers like 5 in your code, make them a
const
variable instead, and use that variable name throughout the code.
Also try to avoid c style casts, prefer the c++
static_cast
. It's unfortunate (IMO) that some of the the reference on this site has some c style casts (if that is where you saw an example). cppreference has definitive reference material, although it might be (necessarily) harder to read.
It's good that you have the cast though,
std::time
returns a
std::time_t
type, and on old implementations that might be implemented as a signed integer, and
std::srand
expects an unsigned value anyway. With that, it is always good to read the documentation for each function that you use, and make sure you use the types that are specified there, along with any thing else in order to use the function correctly.
Here is your code with a couple of changes, I haven't tested 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
|
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const unsigned int ArraySize = 5;
int options[ArraySize]= {1,2,3,4,5};
int array2; // is there a better name for this variable?
std::srand( static_cast<unsigned int> std::time(0) );
std::random_shuffle(&options[0], &options[ArraySize]);
for (unsigned i = 0; i < ArraySize; i++)
{
std::cout << options[i] << endl;
}
array2 = options[0];
std::cout << "\n" << array2;
return 0;
}
| |