I really need help for programming

1)

I have the following question:

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) for(j=2;j<=10;j+=2)
(b) While(count<12)
(d) while(count <= limit);
(e) for(j=0,j<10,j++)
(h) For(n=x;n<y;n+=z)

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.
Last edited on
why (d) should not have a ; ?
It tingles. The body does not modify the variables in the condition, so an infinite loop is feared.
(but its syntax is valid)

@SamueulAdams: see http://www.cplusplus.com/doc/tutorial/operators/ (compound assignment)


> 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
Last edited on
THank you ne555, i did that and i made it working.

and thanks for the first 3 replies, they work really well and i think i got it.
@ne555 I thought of a do-while loop
do statement while (condition);
I would have to say b , e , and h.

b - it is while not While
e -should be semi-colons not commas
h - same reason as b it needs to be lowercase for loop not uppercase.

*fixed code tags had /cdoe instead of /code
Last edited on
then it is invalid as it's missing the do xP

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.

There is more in the world than PCs.
Last edited on
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.
Topic archived. No new replies allowed.