Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How does this loop work to get this answer

Posted on 2004-10-19
6
Medium Priority
?
228 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
Comment
Question by:tedschnieders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:allmer
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

by:tedschnieders
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

by:
allmer earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:tedschnieders
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

by:tedschnieders
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

by:allmer
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

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

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

Join & Ask a Question