![]() |
|
#1
|
||||
|
||||
[Header]Classifying the FOR loopName/Brief Description:
Classifying the FOR loop explains the working of a header file with which it's possible to treat the FOR Loop as an object. Date of Original Submission: June 21st 2004 Submitted by: Mithun Jacob a.k.a Phantom XXIII License: Open source Detailed Description: I'm sure many a programmer has been frustrated by the bulkiness and unwieldy nature of the nested FOR loop. When I was creating a solution to the N Queen Problem, I wished I could treat the FOR loop itself as an object, which could be dynamically created from a single class with specific constraints, and other useful techniques which are used in optimizing the brute-force modus operandi. So, I created the following class keeping in mind the practical example of a counter. The class basically creates a counter of customizable width, range, and other knick-knacks like initial values, and whether it is continuous or not. I have used INT as the data type...the class can be easily modified to suit your needs. Alright, enough with the introduction, here's the code: CPP / C++ / C Code:
val : holds the value of the digit or whatever unitary value min : minimum value max : maximum value hitMax() : returns a flag stating whether the max value is attained. And the counter, itself, I'm presenting it in parts, the entire code is attached. CPP / C++ / C Code:
Pretty obvious.. CPP / C++ / C Code:
CPP / C++ / C Code:
I chose -1, as by default ALL my digits were in 0->9 void digSet(int, int , int , int): This function is used to change the range or starting value of any digit. And now, the backbone, of the class.. CPP / C++ / C Code:
This FOR loop scans through all the digits checking whether the maximum is attained in any one of them. if(i==0) { reset(); break; } If a maximum is attained in the first digit, then the counter's run is over [i]else digs[i].val=digs.min; Else assign the minimum value to the digit. And the flag 'upd' is TRUE if the counter is successfully updated. There, it's a pretty simple piece of code, but I find it pretty useful. Here's an application of the code: CPP / C++ / C Code:
Well, I suppose this wraps up the tutorial. I'll leave it's applications to your imagination... A brief description of brute-forcing: Brute-forcing is simply another, less desirable but effective solution to certain problems. For example, suppose you knew a password started and ended with the string "CH" and consisted of six alphabets, obviously you'd try, CHaaCH CHabCH CHacCH . . . CHbaCH CHbbCH CHbcCH . . CHupCH CHuqCH CHurCH---------------->BINGO! That's what brute-forcing is all about...trying out all the different combinations, well..basically. Of course, there are different methods to brute-force. There is the dictionary option where you wil only compare words which make sense, not meaningless combinations like CHabCH..using a dictionary based brute-forcer you would have got the answer in no time. Now, let me try to implement this in C++ using nested FOR loops, which are basically FOR loops inside other FOR loops, inside other FOR loops..so on... CPP / C++ / C Code:
Here, I'm assuming the required characters are in lower-case. Mithun Jacob a.k.a Phantom XXIII January 4th 2004 Contact: mithunjacob@vit.ac.in / phantom@phantom23.tk / mithunjacob@hotmail.com Site : http://www.phantom23.tk __________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times. |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Program using a for loop | tommy69 | C Programming Language | 3 | 10-Mar-2004 22:16 |
| coding a sentinel controlled loop | tommy69 | C++ Forum | 2 | 10-Mar-2004 14:52 |
| Problem - for loop input | st8tic1 | C++ Forum | 1 | 06-Mar-2004 06:42 |
| C switch / loop issue | spudtheimpaler | C Programming Language | 4 | 20-Feb-2004 20:45 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The