![]() |
Problem with simple code, please help.
Hello everyone, i'm new here.
hope i'll be welcome :) So lets start with the newbiz questions :rolleyes: i have some exercise to do in home and i ran into some problems.. What's wrong with this pieces of codes: int i=1 , k=3; do k=k+i; while (i<=10); --- int i=1 , k=3; while (i<10) { k=k+i; } --- int i=1 , k=5; while (i<10); { k=k+i; i=i+3; } --- int k , i; for (i=1 ; i<10 ; i++); { k=k+i; i=i+3; } Sorry if it's too Newbi ;) *Thanks in advance ! |
At the top of the C++ forum (in fact, all forums on this site) there is a sticky thread that you should read BEFORE posting again. Your post has broken a number of forum rules. The most important rule you have broken is of that of not posting homework problems. The reason for that rule is that homework is generally designed to help you learn. You learn nothing if others do it for you.
|
Hello grumpy
well, i'm sorry about not reading the sticky thing.. i'll read it as soon as possible (before posting again..) about the homework problems.. i think it's alright to ask for help if i Don't understand something.. i didn't just post this to get the final answer and leave with it, i post it because i -tried to solve it, but got stuck.. so i came here to ask for help.. i want to understand and solve the problem.. so is it wrong for someone who knows the answer to come and -Explain it to me -Before he shows me the final answer ? best regards, Danny. |
That's where the discussion can get circular Danny.
If you make an attempt to answer your homework questions, and run into particular problems, people will be more helpful. Or if you do something like "these are the answers I've come up with; how can they be improved?", we will probably help you. Both show us that you've put some effort into understanding your homework, and made a stab at completing it. But what you did is obviously just a paraphrasing of your homework problem, and asking us to provide answers. That's not on. What you need to do is provide us with a list of things that you think are wrong with the code snippets, and the reasons you think they are wrong. By doing that, you would show us you've attempted to do the problem. It would also give us an idea of where you are really having difficulties, so we can help more effectively. |
I understand, thank you for the clear-up
i'll try to add more info about the problems, and try harder understanding them before posting again. thanks again and good night \ morning i have to get some sleep :) |
For a little help without giving things away, the first two have logic mistakes and the last two have parsing problems.
|
As a matter of fact, Jimbo, all of the examples have logic problems, and none actually have parsing issues (all will be accepted by a compiler; although some compilers might issue warnings they will successfully compile all of the examples, assuming they are within the body of a function).
Some of the examples have multiple problems. Some of the problems are infinite loops, undefined behaviour, and code which looks like it is being looped over but isn't. |
Thank you, both of you for the reply.
I fixed the code, is there any further problems with it? : And can i Improve it some-how ? :
// Do - While LoopThanks again :) |
:
i=i++;This should be either :
i++;:
i=i+1;I don't know if the exact behaviour of i=i++ is defined (grumpy or Narue would know), but it is incrementing i (using i++) and then assigning the value of i before the increment back into i. I think this would end up doing nothing. |
So I thought that "i = i++" results in "i = i; i += 1". But if it works as Dark said, by storing i in temporary, incrementing i, then assigning the temporary back to i, I guess it would do nothing.
I just checked with GCC, and with all warnings on it says the above operations are undefined, quite odd considering it increments the value properly and valgrind reports no memory access errors. |
| All times are GMT -5. The time now is 1:20 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC