![]() |
| ||
| wat is flag??pls...can someone explain it to me!! #include <stdio.h> 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! |
| ||
| Re: wat is flag??pls...can someone explain it to me!! doThis 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 doRather inefficient, but a common sorting algorithm. Hope you understand. |
| ||
| 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. |
| ||
| 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 |
| ||
| Re: wat is flag??pls...can someone explain it to me!! Quote:
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. |
| ||
| Re: wat is flag??pls...can someone explain it to me!! Quote:
A flag is just that... a general use of check condition. The name flag can be used. I think you may be getting cornfused. |
| ||
| 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 |
| All times are GMT -4. The time now is 4:15 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC