0
Bon Yoyage! (C++)
// āYou are on a 5-hour sea voyage. The ship sails at a speed of 40km per hour. Write a program that will output how many kilometers the ship has traveled by each hour.ā #include <iostream> using namespace std; int main() { int distance = 0; //your code goes here for (int x; x<=200; x+=40){ cout << x + 40 << endl; } return 0; }
11 Answers
+ 5
Tahitiš· , please edit your question and add description on top of your question. It will be easy to understand your question.
Here, you have given distance = 40 km per hour. So, you will be outputting only distance (instead of x + 40)
Hope this helps! :) š
+ 5
Just one correction in for loop & one correction in your output statement Tahitiš· )
for (int = x) & check cout inside for loop
Rest of your code is good to go)
And, it happens don't worry. Slowly, you are going to code all steps correctly. šš
[correction for your code] š
#include <iostream>
using namespace std;
int main()
{
int distance = 0;
//your code goes here
for (int x =40; x <=200; x +=40)
{
cout <<""<< x << endl ;
}
return 0;
}
+ 1
Shivani šā [Less Active] Thank you! I will try it. I donāt understand why I always answer every question correctly in the lesson, but then I get to the quiz and code incorrectly. š¤·š½āāļø
+ 1
For anyone trying to figure this problem out do not follow the steps above. Below is the correct way to complete the problem.
#include <iostream>
using namespace std;
int main()
{
for (int x = 40; x<=200; x+=40){
cout << x << endl;
}
return 0;
}
0
āYou are on a 5-hour sea voyage. The ship sails at a speed of 40km per hour. Write a program that will output how many kilometers the ship has traveled by each hour.ā
0
#include <iostream>
using namespace std;
int main()
{
int distance = 0;
//your code goes here
for (int x; x<=200; x+=40){
cout << x << endl;
}
return 0;
}
// Your output:
0
40
80
120
160
200
Expected output:
40
80
120
160
200
0
Shivani šā [Less Active] What an awesome teacher (and scholar) you are! I know that you are going to ace your upcoming exams! Best wishes to you.
0
Why did you replace "distance" with "x" in for loop?
I'm doing this and it's not working
#include <iostream>
using namespace std;
int main()
{
int distance = 0;
//your code goes here
for (distance <= 0; distance + 40;){
cout << distance << endl;
}
return 0;
}
0
Rami Moazzen
I suppose I could have used ādistanceā instead of āxā as the integer. The way I understand it, ā0ā is the starting distance, and must be increased by 40, up to 200. So distance canāt be <= 0. That would mean it has to be less than or equal to zero.
0
Tahitiš·, I tried this and for some reason I ended up in an endless loop. I really don't understand it till now :/
0
Rami Moazzen
I myself am still learning C++, but I do know that you must instruct your program to stop the loop at some number, or it will loop endlessly. In this exercise, the sea voyage lasts 5 hours. So the distance loop must stop at the 5th hour. 40k per hour is 200k after 5 hours. Hence, for int=x, x<= 200. (The distance is less than or equal to 200k.)