My teacher asks me to do c++'s homework but u know i don't know how to write the code and it is should be written by for looping and by iostream. please help me ...
and the question is :
Someone drives a car with the speed :
1. the first 10 seconds's speed is 3 m/s.
2. the second one is changed to 4 m/s.
3. And the next after that, the speed increases 1 m/s than 10 seconds before.
If you don't know about loops I would recommend checking out control structures in general. Because you are going to need to use these a lot so you need to understand how they work.
Here's a tutorial on this site that talk about them http://www.cplusplus.com/doc/tutorial/control/
Or just google C++ loops. Other people giving you the answer to your homework wont solve the problem. Because you wont learn anything, and then you will just keep coming back with more problems so might as well learn it now :)
Also the code above why declare a bool variable when a simple int would be more easy to manage later if you need to use that number for other loops? Something like this.
1 2 3 4 5 6 7
int exitspeed = 10;
for (int SpeedTime = 0; SpeedTime != exitspeed; ++SpeedTime)
{
cout << "From " << SpeedTime * 10 << " to " << SpeedTime * 10 + 10
<< " seconds the speed of the vehicle is " << SpeedTime + 2 << " m/s." << endl;
}
Or if your not going to use exitspeed in any other loops you can do something like this.
1 2 3 4 5 6 7 8
int main(){
for (int SpeedTime = 0; SpeedTime != 10; ++SpeedTime)
{
cout << "From " << SpeedTime * 10 << " to " << SpeedTime * 10 + 10
<< " seconds the speed of the vehicle is " << SpeedTime + 2 << " m/s." << endl;
}
}
But the main point here is to learn loops cause you will need them later so its best to get the jump start now. If you do need any help on them please just post any questions you have and I would be glad to help. Also clearer information on your exercise the teacher gave you would help to.