Population

SEQUENCE

    A.First Edition

    This is my first edition of assignment.

    B.The problem

C.The idea of program

    Very simple.

    D.The major functions

    C.Further improvement

    กก

    #include <iostream>

    using namespace std;

    const int LENGTH = 4;
    const int SEEDNUMBER = 6;

    int counter=0;

    int seed[SEEDNUMBER]={12, 13, 21, 23, 31, 32};

    int numString[LENGTH]={0};

    void seq(int current, int length, int first);

    bool checkNum(int curIndex, int selected, int first);

    void printStr();

    int main()
    {
    	seq(0, 1, 12);
    	seq(1, 1, 13);
    	return 0;
    }

    bool checkNum(int curIndex, int length, int selected, int first)
    {
    	bool result = false;

    	switch (seed[curIndex])
    	{
    	case 12:
    		result = selected==12||selected==13||selected==23||selected==21;
    		break;
    	case 13:
    		result = selected==13||selected==23||selected==21||selected==31;
    		break;
    	case 23:
    		result = selected==23||selected==21||selected==31||selected==32;
    		break;
    	case 21:
    		result = selected==21||selected==31||selected==32;
    		break;

    	case 31:
    		result = selected==31||selected==32;
    		break;
    	case 32:
    		result = selected==32;
    		break;
    	}
    	if (length<LENGTH)
    	{
    		return result;
    	}
    	else
    	{
    		switch (first)
    		{
    		case 12:
    			result = result &&(selected==21||selected==31||selected==32);
    			break;
    		case 13:
    			result = result &&(selected==31||selected==32);
    			break;
    		}
    		return result;
    	}
    }

    void seq(int curIndex, int length, int first)
    {
    	numString[length-1] = seed[curIndex];

    	if (length<LENGTH)
    	{
    		for (int i=0; i<SEEDNUMBER; i++)
    		{
    			if (checkNum(curIndex, length+1, seed[i], first))
    			{
    				seq(i, length+1, first);
    			}
    		}
    	}
    	else
    	{
    		printStr();
    	}
    }

    void printStr()
    {
    	counter++;
    	cout<<"Number "<<counter<<" string is:\t ";
    	for (int i=0; i< LENGTH; i++)
    	{
    		cout<<numString[i]<<'\t';
    	}
    	cout<<"\n";
    }

    The result is like following:

    Number 1 string is: 12 12 12 21
    Number 2 string is: 12 12 13 21
    Number 3 string is: 12 12 13 31
    Number 4 string is: 12 12 21 21
    Number 5 string is: 12 12 21 31
    Number 6 string is: 12 12 21 32
    Number 7 string is: 12 12 23 21
    Number 8 string is: 12 12 23 31
    Number 9 string is: 12 12 23 32
    Number 10 string is: 12 13 13 21
    Number 11 string is: 12 13 13 31
    Number 12 string is: 12 13 21 21
    Number 13 string is: 12 13 21 31
    Number 14 string is: 12 13 21 32
    Number 15 string is: 12 13 23 21
    Number 16 string is: 12 13 23 31
    Number 17 string is: 12 13 23 32
    Number 18 string is: 12 13 31 31
    Number 19 string is: 12 13 31 32
    Number 20 string is: 12 21 21 21
    Number 21 string is: 12 21 21 31
    Number 22 string is: 12 21 21 32
    Number 23 string is: 12 21 31 31
    Number 24 string is: 12 21 31 32
    Number 25 string is: 12 21 32 32
    Number 26 string is: 12 23 21 21
    Number 27 string is: 12 23 21 31
    Number 28 string is: 12 23 21 32
    Number 29 string is: 12 23 23 21
    Number 30 string is: 12 23 23 31
    Number 31 string is: 12 23 23 32
    Number 32 string is: 12 23 31 31
    Number 33 string is: 12 23 31 32
    Number 34 string is: 12 23 32 32
    Number 35 string is: 13 13 13 31
    Number 36 string is: 13 13 21 31
    Number 37 string is: 13 13 21 32
    Number 38 string is: 13 13 23 31
    Number 39 string is: 13 13 23 32
    Number 40 string is: 13 13 31 31
    Number 41 string is: 13 13 31 32
    Number 42 string is: 13 21 21 31
    Number 43 string is: 13 21 21 32
    Number 44 string is: 13 21 31 31
    Number 45 string is: 13 21 31 32
    Number 46 string is: 13 21 32 32
    Number 47 string is: 13 23 21 31
    Number 48 string is: 13 23 21 32
    Number 49 string is: 13 23 23 31
    Number 50 string is: 13 23 23 32
    Number 51 string is: 13 23 31 31
    Number 52 string is: 13 23 31 32
    Number 53 string is: 13 23 32 32
    Number 54 string is: 13 31 31 31
    Number 55 string is: 13 31 31 32
    Number 56 string is: 13 31 32 32
    Press any key to continue