GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jun-2004, 16:39
mithunjacob's Avatar
mithunjacob mithunjacob is offline
Junior Member
 
Join Date: Jun 2004
Location: Vellore, India
Posts: 65
mithunjacob will become famous soon enough
Post

[Header]Classifying the FOR loop


Name/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:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class dig
{
public:
int val;
int min;
int max;

dig( int mi=0, int ma=9)
{
val=mi,max=ma,min=mi;
}

int hitMax()
{
return (val==max);
}
};
This class represents each digit of the counter. As you can see,
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:
class counter
{
dig *digs;

public:
int upd;
int digNo;
int beg;
int continuous;

/*digs                   : will hold the future digits of the counter, 
                             dynamically  allocated
 upd                    :  flag, will determine whether the counter should     
                             be updated or not
 digNO                 :  Total number of digits
 beg                    :  flag, determines whether this is the first run of 
                             the counter
 continuous          :  flag, will determine whether the counter must  
                             stop after finishing it's run or reset and start  
                             again*/

void init(int n,int c=0)
{
digNo=n;
digs=new dig[digNo];
continuous=c;
upd=1;
beg=1;
}

Pretty obvious..
CPP / C++ / C Code:
counter(){}

counter(int n, int c=0) {init(n,c);}

void set()
{
upd=1;
}

void reset()
{
for(int i=0;i<digNo;++i) digs[i].val=digs[i].min;
if(continuous) upd=1;
else upd=0;
}
Again, pretty obvious..
CPP / C++ / C Code:
int val(int no)
{
if(no>=0 && no<digNo) return digs[no].val;
else return -1;
}

void digSet(int no, int ma, int mi, int va)
{
digs[no].max=ma,digs[no].min=mi,digs[no].val=va;
}

~counter()
{
delete digs;
}
int val(int) : if the digit number is valid, the value is displayed.
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:
int update()
{
if(upd)
{
	for(int i=digNo-1;i>=0;--i)
		{
		if(digs[i].hitMax())
			{
			if(i==0)
				{
				reset();
				break;
				}
			else digs[i].val=digs[i].min;
			}
		else
			{
			++digs[i].val;
			break;
			}
		}
if(beg==1)
	{
	for(int i=0;i<digNo;++i) digs[i].val=digs[i].min;
	beg=0;
	}
}
return upd;   }

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:
//Application of "counter.h"
#include <fstream.h>
#include <conio.h>
#include "counter.h"

void main()
{
counter number(2,1);

//Suppose you wanted all nos. less than 50, then all of //you have to do is
//number.digSet(0,4,0,0);

for(;;number.update())
{
        for(int i=0;i<2;++i) cout<<number.val(i);
                	
	    if(getch()==27) break;
 //27 is ASCII for the Escape key
                        clrscr();
}

}

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:
for(char first='a';first<='z';++first)
	for(char secnd='a';secnd<='z';++secnd)
		cout<<"nCH"<<first<<secnd<<"CH";

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
Attached Files
File Type: txt counter.h.txt (1.2 KB, 23 views)
__________________
[b]There are times when the Phantom walks the streets as an ordinary man...this is one of those times.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 06:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.