Hello grkanklcsln,
My apologies. In my hast to head the call of nature I missed some things in your program.
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
|
#include <iostream>
#include <cstdlib> // <--- Should use these C++ versions.
#include <ctime>
using namespace std; // <--- Best not to use.
//int p0, p1, p2, p3; // <--- Best not to use non constant global variables.
int main()
{
constexpr unsigned MAXSIZE{ 4 };
int p0{}, p1{}, p2{}, p3{}; // <--- ALWAYS initialize all your variables.
int a0[MAXSIZE]{ 1, 0, 0, 0 };
int a1[MAXSIZE]{ 0, 1, 0, 0 };
int a2[MAXSIZE]{ 0, 0, 1, 0 };
int a3[MAXSIZE]{ 0, 0, 0, 1 };
//srand(time(NULL)); //initialize the random seed
srand(static_cast<unsigned int>(time(nullptr))); // <--- The more up to date method. Only needs done once.
for (int i = 0; i < MAXSIZE; i++)
{
constexpr int arrayNum[MAXSIZE]{ 0, 1, 2, 3 };
int randIndex = rand() % 4; //generates a random number between 0 and 3
if (arrayNum[randIndex] == 0)
{
p0 = a0[0];
p1 = a0[1];
p2 = a0[2];
p3 = a0[3];
}
else if (arrayNum[randIndex] == 1)
{
p0 = a1[0]; p1 = a1[1]; p2 = a1[2]; p3 = a1[3];
}
else if (arrayNum[randIndex] == 2)
{
p0 = a2[0]; p1 = a2[1]; p2 = a2[2]; p3 = a2[3];
}
else if (arrayNum[randIndex] == 3)
{
p0 = a3[0]; p1 = a3[1]; p2 = a3[2]; p3 = a3[3];
}
cout << p0 << " " << p1 << " " << p2 << " " << p3 << '\n'; // <--- Could also use single quotes here.
}
return 0; // <--- Not required, but makes a good break point for testing.
}
|
Line 11 makes the program easier to change as you can see where the variable is used.
In lines 13 - 17 the use of "MAXSIZE" is not needed. I put it there as a demonstration.
In line 22 try to avoid using magic numbers as you did. This is one of the more important reasons for using a constant variable.
In lines 13 - 17 and line 24 I changed these to an "int" from the "double". Defining these variable arrays as "double"s then trying to store the "double" in an "int" has possible data loss because you can not store a floating point number into a non floating point variable.
Your if/else if statements are OK, but if you have studied a "switch" it could be an alternate choice.
For line 48 you do not need a separate "cout" for every little part. That is what the insertion operator (<<) is for to chain everything together.
When you define variable the = before the uniform initializer, the {}s, is not needed.
For lines 30 - 33 you may want to consider using this form until you better understand C++ coding an especially the error messages generated by the compiler. At least until you get use to everything.
If the compiler only gives you a line number of the error and there are 4 choices on that line to check, how much time would you waste trying to figure out which statement is the problem.
Last point is to use a good name for your variables. Something more than just a single letter. I makes the code easier to follow and understand.
Andy