"For loop test" my code is not working. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

"For loop test" my code is not working.

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.” 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; } Expected Output 40 80 120 160 200

27th Mar 2021, 11:43 AM
Rami Moazzen
Rami Moazzen - avatar
30 Answers
+ 4
#include <iostream> using namespace std; int main() { for (int i=1;i<=5;i++){ cout<<40*i<<endl; } return 0; }
27th Mar 2021, 12:21 PM
Atul [Inactive]
+ 4
Farnaz.A I think so we have to print each distance in a new line
27th Mar 2021, 12:25 PM
Atul [Inactive]
+ 3
Rami Moazzen Here is solution,hope it will help you speed*time=distance and we have 5 hours so distance travel in 5 hours is something like this #include <iostream> using namespace std; int main() { int distance=0; int speed=40; for (int time=1;time<=5;time++) { distance=speed*time; cout<<distance<<endl; } return 0; }
28th Mar 2021, 3:30 AM
Deepesh Patel
Deepesh Patel - avatar
+ 2
Rami Moazzen It says that it is a 5 hour trip, but your code will run indefinitely. you have to stop after 5 hours. it would make more sense in this case to loop through the days, instead of the distance: for (int day=1; day<=5; day++) { int distance = day*40; cout << distance << endl; }
28th Mar 2021, 8:08 AM
John Doe
+ 2
Rami Moazzen Speed=40 is constant but time is increasing by 1 untill time reaches to 5 so every time distance is calculated which is multiple of 40 40*1=40 40*2=80 40*3=120 40*4=160 40*5=200
28th Mar 2021, 10:37 AM
Deepesh Patel
Deepesh Patel - avatar
28th Mar 2021, 5:43 PM
Atul [Inactive]
+ 1
#include <iostream> using namespace std; int main() { int distance = 0; //your code goes here for (distance=0;distance <= 0; distance += 40){ cout << distance << endl; } return 0; }
27th Mar 2021, 11:51 AM
Atul [Inactive]
27th Mar 2021, 12:03 PM
Rami Moazzen
Rami Moazzen - avatar
+ 1
#include <iostream> using namespace std; int main() { int speed; int time; cin>>speed; cin>>time; for(int i=1;i<=speed;i++){ cout<<speed*time<<endl; } return 0; }
27th Mar 2021, 12:04 PM
Atul [Inactive]
+ 1
Farnaz.A true, but the main thing from the quiz is teach me how to use for loop and unfortunately it's not working with me so I'm doing something wrong, and still can't figure it out :(
27th Mar 2021, 12:12 PM
Rami Moazzen
Rami Moazzen - avatar
+ 1
Rami Moazzen try this code
27th Mar 2021, 12:22 PM
Atul [Inactive]
+ 1
Deepesh Patel, just one more question, I'm still beginner and don't understand why you used "time++" As i understood x++ means increase by one, while here we want to increase it with 40 each time. So how did it work here?
28th Mar 2021, 9:54 AM
Rami Moazzen
Rami Moazzen - avatar
+ 1
#include <iostream> using namespace std; int main() { int a; cin >> a; for (int i=1;i<=a;i++) cout << i*40 << endl; return 0; } // Hope this helps
28th Mar 2021, 1:00 PM
Calvin Thomas
Calvin Thomas - avatar
0
This is the correct code. In terms of syntax
27th Mar 2021, 11:52 AM
Atul [Inactive]
0
Why did you define that distance is 0 if the int distance is already 0?
27th Mar 2021, 11:53 AM
Rami Moazzen
Rami Moazzen - avatar
0
#include <iostream> using namespace std; int main() { int distance = 0; //your code goes here for (;distance <= 0; distance += 40){ cout << distance << endl; } return 0; } //For that you can use this
27th Mar 2021, 11:55 AM
Atul [Inactive]
0
Farnaz.A Expected Output 40 80 120 160 200
27th Mar 2021, 11:58 AM
Rami Moazzen
Rami Moazzen - avatar
0
Alex the test wants me to use for loop
27th Mar 2021, 11:59 AM
Rami Moazzen
Rami Moazzen - avatar
0
Atul in the test it doesn't require an input :/
27th Mar 2021, 12:06 PM
Rami Moazzen
Rami Moazzen - avatar
0
Alex I'm not copy pasting but i said already it's requires a for loop in the test!!! I can already use while and I'm not asking for how to use while?!!
27th Mar 2021, 12:08 PM
Rami Moazzen
Rami Moazzen - avatar