i just wanna know wht hapen on array and for loop and what is result of that program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donthomasino
    New Member
    • Apr 2010
    • 3

    i just wanna know wht hapen on array and for loop and what is result of that program

    Code:
    #include <iostream>
    #include <queue>
    using namespace std;
    void main()
    {
    	int encode[8] = {3,1,1,1,3,1,1,1};
    	queue<char>myMagicWord;
    	myMagicWord.push('c');
    	myMagicWord.push('o');
    	myMagicWord.push('n');
    	myMagicWord.push('v');
    	myMagicWord.push('e');
    	myMagicWord.push('r');
    	myMagicWord.push('s');
    	myMagicWord.push('a');
    	myMagicWord.push('t');
    	myMagicWord.push('i');
    	myMagicWord.push('o');
    	myMagicWord.push('n');
    	for (int i=0; i<9; i++)
    	{ 
    		for (int j=0; j<encode[i]; j++)
    			myMagicWord.pop();
    		if(!myMagicWord.empty())
    			cout << myMagicWord.front();
    		//return(0);
    	}
    	
        
    }
    Last edited by Banfa; Apr 28 '10, 11:26 PM. Reason: Added [code] ... [/code] tags
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please use CODE tags. That would have provided line numbers, making it easy to refer to your code.

    The following snippet involves accessing array encode entries 0 through 8. Unfortunately, entry 8 doesn't exist. The array only has entries 0 through 7.
    Code:
    int encode[8] = {3,1,1,1,3,1,1,1};
    ...
    for (int i=0; i<9; i++)
       { 
       for (int j=0; j<encode[i]; j++)
          ...

    Comment

    • donthomasino
      New Member
      • Apr 2010
      • 3

      #3
      Originally posted by donbock
      Please use CODE tags. That would have provided line numbers, making it easy to refer to your code.

      The following snippet involves accessing array encode entries 0 through 8. Unfortunately, entry 8 doesn't exist. The array only has entries 0 through 7.
      Code:
      int encode[8] = {3,1,1,1,3,1,1,1};
      ...
      for (int i=0; i<9; i++)
         { 
         for (int j=0; j<encode[i]; j++)
            ...
      so wht will going to happen after the going through the loop and array cause is push and pop wht will be the result of that?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Accessing an array thorugh an out-of-bounds index is "undefined behavior". Anything can happen. Something different could happen each time. You cannot reliably predict what happens during "undefined behavior". You should try hard to expunge all undefined behavior from your programs.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Donthomasino,

          Donbock was kind enough to explain that there is an error in the code...and was kind enough to explain the error in detail.

          Why don't you compile the code and run it to find out what happens first hand?
          I know of no better way to learn than through experience.

          -Frinny

          Comment

          Working...