Solved

How does this loop work to get this answer

Posted on 2004-10-19
6
222 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 clear a vector as well as how to detect empty vectors in C++.

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now