#include <stdio.h>
int arrange(int[],int);

main()
{
  int array[10],num,i,flag;

  clrscr();
  puts("Enter length of array");
  scanf("%d",&num);
  puts("Enter the enties:");

  for( i=0; i<=num-1; i++)
  {
     printf("\n(%d).......",i+1);
     scanf("%d",&array[i]);
  }
  do
  {
     flag=arrange(array,num);
  }while( flag==-1);

  printf("\nThe array in desending order is: ");
  for(i=0; i<=num-1; i++)
     printf("\n\t\t***** %3d *****",array[i]);
   getch();

}

int arrange(int *block,int num)
{
   int j,status,temp;

   status=1;
   for( j=0;j<num-1;j++)
   {
      if(block[j] < block[j+1])
      {
     temp=block[j];
     block[j]=block[j+1];
     block[j+1]=temp;
     status=-1;
      }
   }
   return(status);
}

Can someone help me simulate this program?? i know how this program works but i don't understand what is the use of the flag in the program!

Dani AI

Generated

Short answer: the variable flag is simply a "did-a-swap-happen" indicator used to stop the repeated passes once the array is fully sorted. The posted arrange does one bubble pass and signals (with -1) that at least one swap happened, so main keeps calling arrange until a pass makes no swaps.

Practical improvements and a clearer pattern: use a descriptive boolean/int named swapped (0/1) instead of a magic -1/1, and use the common two-loop bubble structure that shortens the inner range each pass. This both documents intent and avoids an extra function-call per pass:

void bubble_sort_desc(int a[], int n) {
    int i, j, swapped;
    for (i = 0; i < n - 1; ++i) {
        swapped = 0;
        for (j = 0; j < n - 1 - i; ++j) {
            if (a[j] < a[j+1]) {
                int t = a[j]; a[j] = a[j+1]; a[j+1] = t;
                swapped = 1;
            }
        }
        if (!swapped) break;  /* no swaps = already sorted */
    }
}

Notes, cautions and quick fixes:

  • Prefer int main(void) and return 0;. Avoid nonstandard clrscr()/getch() unless using Turbo C.
  • Always validate num (0 < num <= array capacity) and check scanf's return value to avoid undefined behaviour.
  • Use swapped/changed instead of flag for clarity. In modern C use <stdbool.h> and bool swapped.
  • Complexity: worst-case O(n^2); with the early-stop (swapped) best-case is O(n) when array is already sorted — that’s why the flag matters.

Acknowledgement: and were on the right track explaining the role of the flag; ’s claim that flag is a reserved word is incorrect (as others pointed out).

Recommended Answers

All 6 Replies

do
  {
     flag=arrange(array,num);
  }while( flag==-1);

This is the only place where flag is being used. So isn't it simple? flag is used to store the result of arrange and arrange is called while the flag is -1.

Now go to the arrange function. It returns -1 when two elements in the array is swapped. That means the original array didnt have it's data in sorted order. Now from that knowledge what can you say? If the original array was not in sorted order, arrange returns -1. So the above loop means

do
     arrange( array )
until ( the original array was sorted )

Rather inefficient, but a common sorting algorithm. Hope you understand.

Your program basically uses Bubble Sort to sort the array entries in descending order. Bubble sort uses 2 for loops to sort the array provided to it. The while loop in main serves as a replacement to the second for loop of Bubble sort. And it is the variable flag which decides till wat time the sorting should continue. When the array is completely sorted the control in the funtion never satisfies the condition if(block[j] < block[j+1]) . Hence the flag remains 1 and the sorting is complete.

In short

if (flag == -1) => array is not sorted and keep sorting
if (flag == 1) => array is sorted and now stop.

HOpe it helped.
Bye.

flag is a preordered keyword that isnt avaible to be used as a variable or a function name or a structure name blah blah.... simply change the keyword and it'll work

flag is a preordered keyword that isnt avaible to be used as a variable or a function name or a structure name blah blah.... simply change the keyword and it'll work

Hello friend, i really fail to understand on what topic are u talking about. Are u giving out answers to ppl just randomly? flag is not a reserverd keyword in C++ or C. THe original poster wanted to understand why the variable flag was used.

Please dont misguide the people reding this thread by giving out contradictory answers.

Bye.

flag is a preordered keyword that isnt avaible to be used as a variable or a function name or a structure name blah blah.... simply change the keyword and it'll work

Nope, Flag in its self is fine.
A flag is just that... a general use of check condition. The name flag can be used. I think you may be getting cornfused.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.