THIS IS NOT A POST TO HELP ME WITH ANY KIND OF HOMEWORK
Hi guys,
I really can't understand the use of Boolean values in sorting. I feel that some part of the code doesn't make sense and messes up the output. Also, I am confused about keeping flag at the end of the condition.
#include <iostream>
using namespace std;
int main()
{
int a[7] = {70, 80, 60, 50 ,60}, t;
bool flag;
do
{ flag = false;
for (int k = 0 ; k < 7 ; k++)
if (a[k] > a[k+1])
{
t = a[k];
a[k] = a[k+1];
a[k+1] = t;
flag = true;
}
} while (flag); //What does that mean to start with does flag have a certain false limit to collapse or so
for (int u=0 ; u<7; u++)
cout << a[u] << endl;
system("pause");
}
Thank you
Ok thank you
can I, please, know if "flag" has a limit or something because I can see that I am ordering the loop to keep going on until "flag," does that mean that there will be a point when "flag" is not present. If yes, can I please know when and how?
Thank you again
> keep going on until "flag,"
> does that mean that there will be a point when "flag" is not present
when `flag' is false the loop will exit.
1 2 3 4 5 6 7 8
do {
flag = false; // <--- (a)
for (int k = 0; k < 7; k++)
if (a[k] > a[k + 1]) {
//...
flag = true; // <--- (b)
}
} while (flag);
notice that in (a) it has the value `false', but you change it to `true' in (b) (inside the if)
so the loop will exit when the condition if (a[k] > a[k + 1]) does not hold. That is, when the whole sequence is sorted