Which of the following are invalid repetition statements in C? Explain why. Assume
any constants and variables (j, x, y, limit etc.) have been declared.
(a) for(j=2;j<=10;j+=2)
(b) While(count<12)
(c) while(count<=12)
(d) while(count <= limit);
(e) for(j=0,j<10,j++)
(f) while(count < limit)
(g) for(k=0;k<100;k=k+3)
(h) For(n=x;n<y;n+=z)
I put the answer as B and H, am i right? (Maybe d is wrong because it has the ;?)
second question)
Using a for loop, write a program fragment which sums all oddnumbers between 1
and 19.
How am i supposed to do this? i am extremely new to programming in C++ and i have read through the lecture notes, i asked the teachers for help but they won't, they just keep repeating the question itself.
(d) is OK
If you are extremely new to programming in C++ and you have read through the lecture and you don't know how to solve this you will need to read it again.
A. j+=2 does not look right to me. No idea what that would do.
B. While should be lower case
D. should not have a ;
E. should be j=0;j<10;j++
H. For should be lower case, and not sure what this does n+=z.
> Using a for loop, write a program fragment which sums all odd numbers between 1 and 19.
> How am i supposed to do this?
¿Can [you (as a person) resolve this task?
Please write the steps that you will follow
As an exercise, I think the idea is for you to compile those snips, see the compiler complains and try to figure out the errors.
As a test, I honestly don't understand its purpose. didn't realize the case issue (an editor would highlight reserved words) and the commas may be hard to see.
It should be more important to identify that there should be a condition, a step, and an initialization in a for structure. (or a simple collection traversal)
Anyways it asks which are invalid repetition the while loop with semicolon at the end will repeat N amount of times other than that it wont do anything so technically it is still valid as it repeats.
As for question two you want to use the modulus operator to tell if a number is even or odd or you can check the last binary bit odds end in 1s and evens end in 0 since the last bit is the 2 ^ 0 bit.
This loop while(count <= limit); would either do nothing at all, or loop forever. The syntax is valid, but in an actual program it almost certainly not be what was intended.
> either do nothing at all, or loop forever
Suppose that the `count' variable is linked to the A/D port, which is connected to an audio signal.
`limit' is a threshold of `no signal'
So the loop would wait until there is a signal.
That's a valid interpretation, but I feel not one which is suggested by the choice of variable names. Still, it's true that the original question was ambiguous, and as such, all interpretations are valid.