Solved

How does this loop work to get this answer

Posted on 2004-10-19
6
225 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 140
Unresolved External Symbols 3 130
Best book to learn C++ 4 87
Least Squares Curve Fitting 4 117
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

730 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