Ok, some comments about your code:
Try to avoid
using namespace std;
it brings heaps of stuff into the global namespace, polluting it, potential causing conflicts with other namespaces. It is easier on the compiler if you only refer to the specific things you need. There are several options:
1. put these statements instead of the
using namespace std;
1 2 3 4
|
using std::cin;
using std:: cout;
using std::endl;
//same for any other std:: thing
|
2. Leave out the
using namespace std;
and put std:: before each std thing throughout the code.
I do a mixture of those, option 1 for things that are used a lot like cin, cout etc. Option 2 for things that are not used much like std::find say.
Avoid using global variables, you could have put those variables in main and send to to functions that require them as arguments, or use pointers so you can change the value of several variables.
Declare your functions before main, and provide definitions for them after main. That way we don't have to scroll all the way to the bottom to see what main does.
There is a bit of rule that code should not exceed 80 chars per line. This is so we don't have tpo scroll sideways all the time. The main culprit or this is the cout statements, they don't have to be on one line - you can build them up in stages, like this:
1 2 3
|
cout << " Numbers: "<< Numbers[0]<<" , " << Numbers [1] << " , " << Numbers[2];
cout << ", " << Numbers[3] << " , " << Numbers[4] << " , " << Numbers[5];
cout << ". Sort Count:" << sort_count_cycles << " Sort needed:" << sort_needed << "\n" ;
|
You can also put comments over several lines, before the line that it refers to.
The other thing is indenting. I am not sure whether it is the way that this pages code formatter does things, or whether it is the users settings in their IDE. Most people use indenting of 4 spaces, I think the problem might arise where users have indenting with the tab length set to 4 as opposed to tabs being translated to 4 spaces.
With functions & variable names, I avoid the underscores because it makes the name too long.I also capitalise each word in the name like this :
GenerateRandomNumber
You can abbreviate a bit - a length of 10 chars is a reasonable goal, as long as it doesn't cause confusion:
GenRandNum
Larger programs, might have longer names, so that there is some sort of reasonable uniqueness about them.
Wth this:
int Numbers[6] = {0};
That is not quite the correct way to initialise an array.
int Numbers[6] = {0,0,0,0,0,0};
Try to avoid magic numbers like 6:
1 2
|
const unsigned SIZE = 6;
int Numbers[SIZE] = {0,0,0,0,0,0};
|
The variable SIZE can be used in a for loop condition.
With this:
int loop_count = {0};
The braces aren't needed, braces are only required for initialisation of arrays.
int LoopCount = 0;
Your display_numbers function could be written with a for loop:
1 2 3 4 5 6 7 8
|
cout << "Numbers ";
for (int Count = 0;Count < SIZE;Count++) {
cout << Numbers[Count] << " , "
}
cout << "\n. Sort Count:" << SortCountCycles << " Sort needed:" << SortNeeded << endl ;
|
Now, I personally hate do loops - I only use one If I really have to, which is very rare. Do loops can almost always be rewritten as a while or for loop. With your generate_random_number function, this should be a for loop, because there is a numerical limit. When processing arrays, I always use for loops.
In main, I think this is a bad use of a do loop:
while (sort_needed==true); // But only sort, if rquired
Do loops always execute once, which is counter to the comment you have.
Hope all this helps, and has been instructive.
Happy to help with any other queries. Cheers.