Hello, I am new to this web and new to programming. I am not sure what does mean the following algorithm: set counta = 0 set countb = 0 read number while read was succesful do if number mod 2 equals 0 then set counta = counta + 1 otherwise if number mod 4 equals 0 then set countb = countb + 1 read number write counta write countb ....................................................... I need help with this: Explain: 1- what takes as input 2- clearly specify any assumption regarding the input 3- what Generates as output (not what output means) 4- what does (relationship btw input and output) What I have done is 1 this takes as input any number right? 2 I think that the number should be any number >= 0 ? 3 for counta and countb will be always 1? 4 there is relationship because for counta and countb to be 1, the number need to be 0?? also what does counta = 0 means? It means that counta is zero always? thank you very much for your help!!
1 this takes as input any number right?
Not quite, it takes a set of numbers. The while, indicates a loop that repeats as long as "read was successful". It's difficult to determine from the way your post is tabbed, but it probably loops after the "read number" statement.
2 I think that the number should be any number >= 0 ?
That's a fair assumption.
3 for counta and countb will be always 1?
also what does counta = 0 means? It means that counta is zero always?
No, they are set to 0 initially. However the statements:
set counta = counta + 1
set countb = countb + 1
indicate that counta and countb increment by 1. So if counta was 0, it would become 1, then if it was incremented again, it would become 2, and so on.
4 there is relationship because for counta and countb to be 1, the number need to be 0??
No, since it is in a loop, counta and countb can be far greater than 1, depending on the number of successful reads. Also, number doesn't need to be 0. The modulo (aka mod) operator gets the remainder. In computer science, this is very important when dealing with integer arithmetic (i.e. integers 1, 2, 10, 10034 and not real numbers 1.2, 3.456, etc.). For instance, 25 / 7 = 3.57... or 3 as an integer (decimal removed, NOT ROUNDED). Since 3 x 7 = 21, the remaineder is 4 (25 - 21). In other words 25 mod 7 is 4. So, back to mod 2: this gives us a table like this:
0 mod 2 = 0 1 mod 2 = 1 2 mod 2 = 0 3 mod 2 = 1 4 mod 2 = 0 5 mod 2 = 1 ...
Note that mod 2 is special, in that it gives an indication of even and odd numbers (hint hint).
Now the question remains, if mod 2 of a number does not equal 0, when would mod 4 of that number equal 0?
Hope that helps.
1)Your answer is somewhat correct. As nmaillet said, it is difficult to determine what it would be. Assume (or regardless) the read number will handle any invalid input in some way and will always return a valid input, your answer would be correct.
2)Let's see how modulo work. If -2 mod 2, what is the result? Of course, 0. So does that mean the number must still be greater than or equal to 0?
3)As nmaillet said, the code is to increment the value by 1, not assign value of 1 to the variables. However, this portion of code is a tricky one. There is the if-else...
if number mod 2 equals 0 set counta = counta + 1 else if number mod 4 equals 0 set countb = countb + 1
Look at the indentation. If a number is not divisible by 2, would the number be divisible by 4? Also, if a number is divisible by 2, would it still gets checked with divisble by 4? Therefore, you should get the answer how the output is generated (counta and countb).
4)Look at nmaillet answer...
Edited by Taywin