468,513 Members | 1,750 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Using/setting flag for if statement

Okay basically I have built the code using the pseudocode given to me but i still have on psedocode to mark off and I dont know how to do what it says.

Its says "a flag is just an integer whose value is either true (1) or false(0). So create an int and set it to false before the inner loop starts, then if a swap is made set it to true. After the inner loop finishes check the value of that int and if it is true then break out of the outer loop. "

Could someone show me how to do that, im pretty sure thats the difference between getting the output shown above as at the moment my ouput is just..

Data items in original order
2 6 4 8 10 12 89 68 45 37

Data items in ascending order
2 4 6 8 10 12 37 45 68 89

My code so far..
Expand|Select|Wrap|Line Numbers
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. using std::setw;
  6.  
  7. main()
  8. {
  9.  
  10. int numbers[10] = {2,6,4,8,10,12,89,68,45,37};
  11. int Swap;
  12.  
  13.     cout <<" Data items in original order\n";
  14.  
  15.     for(int ctr=0; ctr<10; ctr++)
  16.         {
  17.         cout<< setw(4) <<numbers[ctr];
  18.         }
  19.         cout<<"\n\n";
  20.  
  21.  
  22.  
  23.     for(int i=0; i<10; i++)
  24.         for(int n=0; n<10; n++)
  25.  
  26.  
  27.     if (numbers[n] > numbers[n + 1])
  28.     {
  29.         Swap = numbers[n];
  30.         numbers[n] = numbers[n + 1];
  31.  
  32.         numbers[n + 1] = Swap;
  33.     }
  34.  
  35.  
  36.     cout<<"Data items in ascending order\n";
  37.  
  38.     for (int n=0; n<10; n++)
  39.  
  40.     cout<< setw(4) << numbers[n];
  41.  
  42.     cout<< endl <<endl;
  43.  
  44.  
  45. return 0;
  46.  
  47. }
Jan 28 '08 #1
2 3834
sicarie
4,677 Expert Mod 4TB
Its says "a flag is just an integer whose value is either true (1) or false(0). So create an int and set it to false before the inner loop starts, then if a swap is made set it to true. After the inner loop finishes check the value of that int and if it is true then break out of the outer loop. "

Could someone show me how to do that, im pretty sure thats the difference between getting the output shown above as at the moment my ouput is just..

int i_flag = 0;
Jan 28 '08 #2
RRick
463 Expert 256MB
The flag allows you to do a quick exit from your bubble sort. With a buble sort you can stop whenever "nothing" gets swapped during the inner loop. Like the instructions say, you need to set the flag inside the swap code section.

You then need to act on that flag outside of the inner loop. You can check for flag's value in the outer loop or between the inner and outer loop. Your choice.
Jan 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Random Person | last post: by
1 post views Thread by Daveyk0 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.