Solved

# How does this loop work to get this answer

Posted on 2004-10-19
221 Views
Last Modified: 2010-04-01
hey will you take me thought the process of these loops and tell me how it gets these answers  38 , 76 , 152

const int limit = 5
short values [] = {1,20,2,19,3};
for(int i = 1; i < limit ; ++i);
int j = 2;
while(j<limit + 2){
if (i <= j ++
continue;

else values[i] = values[j] << 1

cout <<  values[i]
0
Question by:tedschnieders
• 3
• 3
6 Comments

LVL 5

Expert Comment

ID: 12351843
It should not do much at all.
the for loop is completely meaningless since there is no command associated with it:
for(int i = 1; i < limit ; ++i);
that basically means do nothing (;)
from i = 1 to i = 4 in steps of 1.
The while loop however should do something
...
Let's try to correct the program:
const int limit = 5;
short values [] = {1,20,2,19,3};
for(int i = 1; i < limit ; ++i)
{
int j = 2;
while(j<limit + 2){
if (i <= j ++)   //dangerous incrementing at this point, but should work.
continue;    //next loop (while)
else values[i] = values[j] << 1;
}
}
cout <<  values[i];

Does this look right?
<< shifts the values[j] 1 byte to the left and stores the result in values[i]:
001 = 1
would then be  (001) << 1
010 = 2
Hopefully this helps,
Jens
0

Author Comment

ID: 12351883
const int limit = 5
short values [] = {1,20,2,19,3};
for(int i = 1; i < limit ; ++i)
int j = 2;
while(j<limit + 2){
if (i <= j ++
continue;

else values[i] = values[j] << 1

cout <<  values[i]

sorry that semicolon wasn't supose to be there

i run it and i get the numbers 38, 76, 152 and i was just wondering if some one could go thought the process of what it is doing so i can understand it better

thanks much
0

LVL 5

Accepted Solution

allmer earned 500 total points
ID: 12352167
I assume the above code is still wrong and take my proposition:
Assume:
... run until i > j
while loop
(i = 3 and j = 2)
if ... i>j
values[i] << (value = 19)
19 = ..0010011
shifted by << 1 bit
..0100110 = 38.
Tada that's about how it works.
I wouldn't trust the code one bit, though.
It looks very awkward.

1. arrays are zero based (value[0] to value[4] = 5 Values)
the for loop is missing the first one.
2. why would you overwrite your values in the array?
create a second array to hold your results.
3. You need a loop to give out all the results.

Maybe you should cut and paste the code that seems to be working for you.

Anyway the results are achieved by the pattern seen above.
Best,
Jens
0

Author Comment

ID: 12352209
this is an assignment and i am supsoe to tell the process of how it gets these numbers and i cant figgure it out

sorry it isn't suppose to be an actual progam

hopefull yi aint wasting your time
0

Author Comment

ID: 12352249
its so frustrating i cant see what it is  doing

its junk code

i know how it get the 38 but i cant see how it gets the 19 to shift 1 bit
0

LVL 5

Expert Comment

ID: 12352291
well,
the code is plainly junk.
the "<<" part of the code is the command to shift one bit to the left.
I don't see how you get the higher numbers.
I guess you should show the correct code or at least the correct magic
code first.
Best,
Jens

PS homework is not supposed to be resolved on ee.
0

## Join & Write a Comment Already a member? Login.

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

#### 757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!