| | |
wat is flag??pls...can someone explain it to me!!
![]() |
C Syntax (Toggle Plain Text)
#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!
C Syntax (Toggle Plain Text)
do { flag=arrange(array,num); }while( flag==-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
C Syntax (Toggle Plain Text)
do arrange( array ) until ( the original array was sorted )
バルサミコ酢やっぱいらへんで
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
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.
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.
•
•
•
•
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
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.
•
•
Join Date: Aug 2006
Posts: 27
Reputation:
Solved Threads: 0
•
•
•
•
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
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
FA LSI Logic
Wichita KS
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
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
![]() |
Similar Threads
- i have problm in a datagrid pls checkit out (VB.NET)
- Can u pls help me in this scenario. (Networking Hardware Configuration)
- New HJT log, pls help me to clean up (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: CMapStringToPtr
- Next Thread: Listing in numeric order
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






