Bon Yoyage! (C++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

26th Feb 2021, 9:48 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
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! :) šŸ‘
26th Feb 2021, 10:17 PM
Shivani šŸ“šāœ
Shivani šŸ“šāœ - avatar
+ 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; }
26th Feb 2021, 11:54 PM
Shivani šŸ“šāœ
Shivani šŸ“šāœ - avatar
+ 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. šŸ¤·šŸ½ā€ā™€ļø
26th Feb 2021, 10:46 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 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; }
27th Jun 2021, 12:28 AM
John Mclean
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.ā€
26th Feb 2021, 9:49 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
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
26th Feb 2021, 10:50 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
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.
27th Feb 2021, 2:57 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
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; }
27th Mar 2021, 11:37 AM
Rami Moazzen
Rami Moazzen - avatar
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.
27th Mar 2021, 6:15 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
TahitišŸ·, I tried this and for some reason I ended up in an endless loop. I really don't understand it till now :/
27th Mar 2021, 6:21 PM
Rami Moazzen
Rami Moazzen - avatar
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.)
27th Mar 2021, 6:33 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar