472,338 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

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 4008
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

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

Similar topics

3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI)...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on...
1
by: marco_segurini | last post by:
Hi, At the moment I am using Visual Studio 2005 beta 1. The following program does not compile using the debug configuration setting the...
15
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with...
1
by: sathyp | last post by:
Public Function SetPrinterDefaultsW(ByVal sPrinterName As String, _ ByVal nPaperSize As Long, ByVal nOrientation As Long) As Boolean Dim Prn...
1
by: hello2008 | last post by:
Hi, I have just started coding in PHP. I have coded a web page using HTML, JS, and PHP. An HTML table has to be populated dynamically using the...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.