944,052 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2324
  • C RSS
Aug 8th, 2006
0

wat is flag??pls...can someone explain it to me!!

Expand Post »
  1. #include <stdio.h>
  2. int arrange(int[],int);
  3.  
  4. main()
  5. {
  6. int array[10],num,i,flag;
  7.  
  8. clrscr();
  9. puts("Enter length of array");
  10. scanf("%d",&num);
  11. puts("Enter the enties:");
  12.  
  13. for( i=0; i<=num-1; i++)
  14. {
  15. printf("\n(%d).......",i+1);
  16. scanf("%d",&array[i]);
  17. }
  18. do
  19. {
  20. flag=arrange(array,num);
  21. }while( flag==-1);
  22.  
  23. printf("\nThe array in desending order is: ");
  24. for(i=0; i<=num-1; i++)
  25. printf("\n\t\t***** %3d *****",array[i]);
  26. getch();
  27.  
  28. }
  29.  
  30. int arrange(int *block,int num)
  31. {
  32. int j,status,temp;
  33.  
  34. status=1;
  35. for( j=0;j<num-1;j++)
  36. {
  37. if(block[j] < block[j+1])
  38. {
  39. temp=block[j];
  40. block[j]=block[j+1];
  41. block[j+1]=temp;
  42. status=-1;
  43. }
  44. }
  45. return(status);
  46. }

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!
Similar Threads
Reputation Points: 14
Solved Threads: 0
Light Poster
comp_sci11 is offline Offline
38 posts
since Jul 2006
Aug 8th, 2006
1

Re: wat is flag??pls...can someone explain it to me!!

  1. do
  2. {
  3. flag=arrange(array,num);
  4. }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
  1. do
  2. arrange( array )
  3. until ( the original array was sorted )
Rather inefficient, but a common sorting algorithm. Hope you understand.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Aug 8th, 2006
1

Re: wat is flag??pls...can someone explain it to me!!

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
[quote]
if (flag == -1) => array is not sorted and keep sorting
if (flag == 1) => array is sorted and now stop.

HOpe it helped.
Bye.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 9th, 2006
0

Re: wat is flag??pls...can someone explain it to me!!

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
b2daj is offline Offline
8 posts
since Aug 2006
Aug 9th, 2006
0

Re: wat is flag??pls...can someone explain it to me!!

Quote originally posted by b2daj ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 10th, 2006
0

Re: wat is flag??pls...can someone explain it to me!!

Quote originally posted by b2daj ...
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.
Reputation Points: 13
Solved Threads: 0
Light Poster
kingvjack is offline Offline
28 posts
since Aug 2006
Aug 10th, 2006
0

Re: wat is flag??pls...can someone explain it to me!!

The keywords in C++ are listed here, and, as you can see, flag isn't one of them: http://www.cppreference.com/keywords/index.html
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: CMapStringToPtr
Next Thread in C Forum Timeline: Listing in numeric order





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC