Ok, I am so totally frustrated with my programming class, I am ready to quit, but I don't give up that easily!! My instructor wants us to trace these arrays and give him the output. I can do this in C#, but that doesn't help me understand it, and won't help me on a written test. This is what I have...
int[] val = {2,4,6,8,10,11,12,14,16};
int key=10, lowPos=0, highPos=val.Length1, midPos;
do
{
midPos = (lowPos+highPos)/2;
Console.Write("{0} ",val[midPos]);
if (key<val[midPos])
highPos=midPos1;
else if (key > val[midPos])
lowPos=midPos+1;
}
while(val[midPos] != key && lowPos
<= highPos);
I know that the answer is 10, but I am having trouble figuring out how to trace this to get that value.
HELP me please!!!
Thank you
4 Replies  2501 Views  Last Post: 27 November 2008  12:53 AM
Replies To: Output from arrays
#2
Re: Output from arrays
Posted 27 November 2008  12:08 AM
If you really have trouble following some code, a nice trick you can use for tests etc is to draw out some boxes. One for each variable you encounter and place the value of that variable in the box. Each time something comes along and modifies that variable, erase the number and put the new value in it. That or draw another box right under it.
Here is an example...
int key=10, lowPos=0, highPos=val.Length1, midPos;
Draw a box for key and label it "key" above the box. Put the number 10 into it. Draw another box and label it "lowPos" and put the 0 inside the box. Create another box labeled "highPos" and figure out how long the array is (minus 1) and put the value into it...the array is 9 values long, so minus 1 and the value in that box will be 8. Create another box and label it "midPos" and put nothing in the box.
So now you have four boxes, labeled and ready to go.
Entering the loop you encounter the line midPos = (lowPos+highPos)/2;. So you take the value in the lowPos box, add it to the one in the highPos box, divide it by 2 and put that value into the midPos box. So now you will see you have 10 for key, 0 for lowPos, 8 for highPos and 0 + 8 /2 = 4 in the midPos box.
Then tackle the next line. Each time you see each label, go to your boxes and see what the current value in the box is, plug it into the line, figure out what the line value is and if it changes any other boxes. Then go onto the next line.
Tackle each line one by one and don't look at what is coming up next. Use a piece of paper to hide it if you need to. By doing this you are doing exactly what the computer is doing. So when you hit the end of the loop, you apply the values and if true, you start at the top and redo the whole loop again.
It can be a bit slow sometimes but it will get you the answer and soon you will find yourself being able to do it all in your head and get much faster.
Hope this tip helps you out. Should be great for those tough lines of code you might encounter on a test. You can do the boxes in the margins or on the back of the paper.
Here is an example...
int key=10, lowPos=0, highPos=val.Length1, midPos;
Draw a box for key and label it "key" above the box. Put the number 10 into it. Draw another box and label it "lowPos" and put the 0 inside the box. Create another box labeled "highPos" and figure out how long the array is (minus 1) and put the value into it...the array is 9 values long, so minus 1 and the value in that box will be 8. Create another box and label it "midPos" and put nothing in the box.
So now you have four boxes, labeled and ready to go.
Entering the loop you encounter the line midPos = (lowPos+highPos)/2;. So you take the value in the lowPos box, add it to the one in the highPos box, divide it by 2 and put that value into the midPos box. So now you will see you have 10 for key, 0 for lowPos, 8 for highPos and 0 + 8 /2 = 4 in the midPos box.
Then tackle the next line. Each time you see each label, go to your boxes and see what the current value in the box is, plug it into the line, figure out what the line value is and if it changes any other boxes. Then go onto the next line.
Tackle each line one by one and don't look at what is coming up next. Use a piece of paper to hide it if you need to. By doing this you are doing exactly what the computer is doing. So when you hit the end of the loop, you apply the values and if true, you start at the top and redo the whole loop again.
It can be a bit slow sometimes but it will get you the answer and soon you will find yourself being able to do it all in your head and get much faster.
Hope this tip helps you out. Should be great for those tough lines of code you might encounter on a test. You can do the boxes in the margins or on the back of the paper.
#3
Re: Output from arrays
Posted 27 November 2008  12:09 AM
Please post your code examples using 
midPos = (lowPos+highPos)/2; == 4.
while(val[midPos] != key && lowPos <= highPos); : key == 10, val[4] == 10. First assertion fails and do loop exits.
wswager, on 26 Nov, 2008  10:57 PM, said:
Ok, I am so totally frustrated with my programming class, I am ready to quit, but I don't give up that easily!! My instructor wants us to trace these arrays and give him the output. I can do this in C#, but that doesn't help me understand it, and won't help me on a written test. This is what I have...
I know that the answer is 10, but I am having trouble figuring out how to trace this to get that value.
HELP me please!!!
Thank you
int[] val = {2,4,6,8,10,11,12,14,16}; int key=10, lowPos=0, highPos=val.Length1, midPos; do { midPos = (lowPos+highPos)/2; Console.Write("{0} ",val[midPos]); if (key<val[midPos]) highPos=midPos1; else if (key > val[midPos]) lowPos=midPos+1; } while(val[midPos] != key && lowPos <= highPos);
I know that the answer is 10, but I am having trouble figuring out how to trace this to get that value.
HELP me please!!!
Thank you
midPos = (lowPos+highPos)/2; == 4.
while(val[midPos] != key && lowPos <= highPos); : key == 10, val[4] == 10. First assertion fails and do loop exits.
#4
Re: Output from arrays
Posted 27 November 2008  12:31 AM
First thank you for your quick response.
Second, I will put code in using code, wasn't sure how to do that.
Third, I think what is confusing me is I am not sure if I am supposed to use the array value, or the subscript value.
I appologize if this sounds elementary, but this is my first programming class of any kind, and I am taking it online, so I am kind of out here on my own, and very confused and overwhelmed.
Second, I will put code in using code, wasn't sure how to do that.
Third, I think what is confusing me is I am not sure if I am supposed to use the array value, or the subscript value.
midPos = (lowPos+highPos)/2;on the low pos and high pos are these the array values or sub values.
I appologize if this sounds elementary, but this is my first programming class of any kind, and I am taking it online, so I am kind of out here on my own, and very confused and overwhelmed.
#5
Re: Output from arrays
Posted 27 November 2008  12:53 AM
It would help if you provided the problem statement. What is the expected trace output?
How would you do it in C#?
How would you do it in C#?
This post has been edited by n8wxs: 27 November 2008  12:54 AM
Page 1 of 1
