473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. void main()
  5. {
  6.     int encode[8] = {3,1,1,1,3,1,1,1};
  7.     queue<char>myMagicWord;
  8.     myMagicWord.push('c');
  9.     myMagicWord.push('o');
  10.     myMagicWord.push('n');
  11.     myMagicWord.push('v');
  12.     myMagicWord.push('e');
  13.     myMagicWord.push('r');
  14.     myMagicWord.push('s');
  15.     myMagicWord.push('a');
  16.     myMagicWord.push('t');
  17.     myMagicWord.push('i');
  18.     myMagicWord.push('o');
  19.     myMagicWord.push('n');
  20.     for (int i=0; i<9; i++)
  21.     { 
  22.         for (int j=0; j<encode[i]; j++)
  23.             myMagicWord.pop();
  24.         if(!myMagicWord.empty())
  25.             cout << myMagicWord.front();
  26.         //return(0);
  27.     }
  28.  
  29.  
  30. }
Apr 28 '10 #1
4 1889
donbock
2,426 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. int encode[8] = {3,1,1,1,3,1,1,1};
  2. ...
  3. for (int i=0; i<9; i++)
  4.    { 
  5.    for (int j=0; j<encode[i]; j++)
  6.       ...
Apr 28 '10 #2
@donbock
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?
Apr 28 '10 #3
donbock
2,426 Expert 2GB
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.
Apr 29 '10 #4
Frinavale
9,735 Expert Mod 8TB
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
Apr 29 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Philip D Heady | last post by:
Ok, as some of you may know I'm an Oracle newbie w/ PHP. I'd rather use MySQL but at the office here we use Oracle and boy do I have alot to learn. I'm starting to hate it after using MySQL!! ...
3
by: Bart Nessux | last post by:
The below code works well, except for the database insert. I want each element of the array to be inserted into the database, but only the last element of the array is inserted... all the elements...
3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
2
by: bidalah | last post by:
Hi, Following is a simple version of my now non-working code: hihi = Dir("c:\burn2\*.*", FileAttribute.Normal) Do While Len(hihi) > 0 hoho = hehe hihi = Dir() Loop
17
by: AMP | last post by:
Hello I get 2 errors in the following code that I cant Debug without some help. They are both in the TryParse function. Seems simple enough.The Errors are Below. private void...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: ptq2238 | last post by:
Hi, I'm getting confused with arrays and hope someone can shed light on my code. I wrote this to try and learn about files and arrays as I thought if I could grab each element and place them into...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.