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

Reply

Join Date: Jul 2006
Posts: 38
Reputation: comp_sci11 is an unknown quantity at this point 
Solved Threads: 0
comp_sci11's Avatar
comp_sci11 comp_sci11 is offline Offline
Light Poster

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

 
0
  #1
Aug 8th, 2006
  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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
1
  #2
Aug 8th, 2006
  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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
1
  #3
Aug 8th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 8
Reputation: b2daj is an unknown quantity at this point 
Solved Threads: 0
b2daj b2daj is offline Offline
Newbie Poster

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

 
0
  #4
Aug 9th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #5
Aug 9th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 27
Reputation: kingvjack is an unknown quantity at this point 
Solved Threads: 0
kingvjack kingvjack is offline Offline
Light Poster

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

 
0
  #6
Aug 10th, 2006
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.
Steve
FA LSI Logic
Wichita KS
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

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

 
0
  #7
Aug 10th, 2006
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
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC