Sequential State Machine

This is a discussion on Sequential State Machine within the C++ Programming forums, part of the General Programming Boards category; Hi there; I was trying to build a DOS programme that helps you draw a state digram of a Sequential ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    40

    Sequential State Machine

    Hi there;
    I was trying to build a DOS programme that helps you draw a state digram
    of a Sequential State Machine. The application halts once I enter the codes, and I am clueless about where the error is. As much as I hate to send the whole code, I will do it anyway. Please guys any comment would help.......Thanks

    Code:
    #include <iostream>
    #include <list>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int Max(list<int>values)
    {
        list<int>::iterator current = values.begin();
        int high = *current++;
        
        while (current != values.end())
        {
           if (*current > high)
           {
              high = *current;
           }
           current++;
        }
        return high;
    }
    
    int main()
    {
        list<int>OrgSeq;
        int no_of_code;
        //list<int>::iterator point;
        
        cout<<"Enter the number of code: ";
        cin>>no_of_code;
        
        list<int>::iterator it=OrgSeq.begin();
        list<int>::iterator that;
        
        // User enter the original sequence in a list
        for(int y=0; y<no_of_code; y++)
        {
          int k;
          cout<<"Enter the next code (in Decimal): ";
          cin>>k;
          OrgSeq.push_back(k);
        }
        
        // The programme loops through all the states
        for(int j=0; j<no_of_code; j++)
        {
                it++;
                for(int i=0; i<=Max(OrgSeq); i++)
                {
                        list<int>OrgSeq2(OrgSeq);
                        that=OrgSeq2.begin();
                        
                        for(int s=j; s>=0; s++)
                        {
                                if(i==*it)
                                break;
                                
                                list<int>cpyseq(OrgSeq);
                                that++;
                                list<int>::iterator iter = cpyseq.begin();
                                
                                for(int L=0; L<=s; L++)
                                iter++;
                                
                                cpyseq.insert(iter,i);
                                cpyseq.insert(iter,that,OrgSeq2.end());
                                cpyseq.erase(iter,cpyseq.end());
                                
                                list<int>::iterator final=cpyseq.end();
                                
                                for(int t=0; t<no_of_code; t++)
                                final--;
                                
                                cpyseq.erase(cpyseq.begin(),final);
                                
                                if( OrgSeq == cpyseq )
                                {
                                    cout<<"State "<<j<<" goes back to state "<<s<<" by code "<<i<<endl;
                                    break;
                                }
                                
                                that--;
                        }
                }
        
        }
    
        system("PAUSE");
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    for(int s=j; s>=0; s++)
    This loop is going to run for a very, very, very long time.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Thank You So Much........boy do I feel stupid right now.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Something is wrong with the logic thou, the algorithm itself.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Can any one help me with this one, it crashes after I enter the codes donot know why, I haye to be such a bother but yiu guys are my only way right now.
    Code:
    #include <iostream>
    #include <list>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int Max(list<int>values)
    {
        list<int>::iterator current = values.begin();
        int high = *current++;
        
        while (current != values.end())
        {
           if (*current > high)
           {
              high = *current;
           }
           current++;
        }
        return high;
    }
    
    int main()
    {
        list<int>OrgSeq;
        list<int>cpyseq;
        int no_of_code;
       
       cout<<"STATE MACHINE DIGRAM"<<endl;
        cout<<"\nEnter the number of code: ";
        cin>>no_of_code;
        
        list<int>::iterator iter = OrgSeq.begin();
        list<int>::iterator iter2,it2;
        list<int>::iterator it = cpyseq.begin();
        
        // User enter the original sequence in a list
        for(int y=0; y<no_of_code; y++)
        {
          int k;
          cout<<"Enter the next code (in Decimal): ";
          cin>>k;
          OrgSeq.push_back(k);
        }
        cpyseq = OrgSeq;
        // The programme loops through all the states
        for(iter=OrgSeq.begin(); iter!=OrgSeq.end(); iter++)
        {
                it++;
                it2 = it;   
                                 //iter points to the code from "state" in "OrgSeq"
                for(iter2=iter; iter2!=OrgSeq.begin(); iter2--)
                {
                        
                        // for loop to go through all possible code
                        for(int i=0; i<=Max(OrgSeq); i++)
                        {
                               OrgSeq.insert(iter2,i);
                               OrgSeq.insert(iter2,it2,cpyseq.end());
                               OrgSeq.erase(iter2,OrgSeq.end());
                               
                               if( cpyseq == OrgSeq )
                               cout<<"State "<<*iter<<" goes back to "<<*iter2<<" by "<<i<<endl;
                               
                               OrgSeq = cpyseq;
                        }
                        
                        it2--;
                }
        }        
        system("PAUSE");
        return 0;
    }
    If anyone can comment on it , that would be most welcome.....thanks for your time.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    I'm sure more people would help if they could read your question. The font and color you selected make the question impossible for me to read.

    Jim

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
     OrgSeq.erase(iter2,OrgSeq.end());
                               
                               if( cpyseq == OrgSeq )
                               cout<<"State "<<*iter<<" goes back to "<<*iter2<<" by "<<i<<endl;
    Once you have erased iter2, it no longer exists; hence attempts to use iter2, by say typing "*iter2" later on, should be met with grave suspicion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. State machine
    By sergio in forum General Discussions
    Replies: 3
    Last Post: 07-03-2009, 09:03 AM
  2. State Machine Controller
    By scott_ill in forum C# Programming
    Replies: 0
    Last Post: 06-25-2009, 02:57 AM
  3. Finite State Machine
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 11:59 AM
  4. Finite State Machine Project Help
    By ryanbradley in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2004, 09:23 AM
  5. Designing State Machine
    By axon in forum Tech Board
    Replies: 3
    Last Post: 11-06-2003, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21