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 odpowiedzi
+ 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