Solved

How does this loop work to get this answer

Posted on 2004-10-19
6
226 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 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
Technology Partners: 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!

 

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 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