Give a postcondition for the following algorithm that completely describes how the final value of i is related to x. Precondition: x is a positive odd integer. i ← 0 while i < x do i ← i + 2 i=???
@Zarkon
@Loser66
It' s computer science, sweeties. tag @e.mccormick. He is the right one. (computer science student)
Does i ← 0 mean i=0? and i ← i + 2 mean i = i + 2?
Seems like some sort of meta language or logic description, and not really programming. However, the principal should be the same. A while loop continues as long is the condition is true. Therefore, because x is any positive, odd integer, and i starts at 0, the first time through i will become 2. If x is 1, this all ends right there. If x is not 1, then it is still greater than 2 and the loop starts over. Now i becomes 4. Again, is x 3? If so, the loop condition is met and it ends. Otherwise it repeats. This happens as many times as needed until i passes x, but it stops as soon as i passes x. Therefore.... can you see what i would have to be at that point?
seems like the answer is not that simple..but thx for ur input
thx @Loser66
hahaaha why do you say thank you to me? e.mccormick helps you, right?
wrong address, sweeties.!!
hmm... What language is it in. The ← may have a different meaning. Otherwise, \(i=x+1\) is the postcondition.
I don't know what language it is..
I know loser66, but u hooked it up lol
e.mccormick asks you about the program you study in C, C++ or java?
Not just those. I feel malia667 got the question. Like I thought, it sounds like it is just a meta language or something manufactured for the course. Here is a reference to the sort of pre-post analysis I did to come up with my answer. It should be correct, so I don't know why it would fail to follow the same rules they describe there. http://cs.engr.uky.edu/~lewis/essays/algorithms/correct/correct1.html
ok lemme take a look..yes its more of a general question
@oldrin.bataku any ideas on this one?
I can handle any CS or programming questions as well
Note that since \(x\) is odd and \(i\) is even, we end up with the smallest even \(i\) that is greater than our odd \(x\) -- which happens to just be \(i=x+1\).
ok just like what e.mccormick said..got it thanks :) let me see if that works
It should make intuitive sense that repeatedly adding \(2\) to \(0\) gives you successive even integers, and the loop terminates once we reach an even number greater than or equal to \(x\) -- it'll always \(>\) since \(x\) is odd and thus we see it is necessarily \(i=x+1\)
Join our real-time social learning platform and learn together with your friends!