+ 3

c++ loop

You are on a 5 hour sea voyage. The ship sails at a speed of 40 km per hour. Write a program that will output how many kilometers the ship has traveled by each hour. My attempt is in the comments

19th Sep 2021, 3:50 PM
Mando
Mando - avatar
9 Respostas
+ 2
It count up. hour++ is hours = hours + 1 We need to increment in the loop
19th Sep 2021, 4:14 PM
Lisa
Lisa - avatar
+ 2
It get incremented by one on each iteration – just like the k in your attempt
19th Sep 2021, 4:16 PM
Lisa
Lisa - avatar
+ 1
Lisa but wouldnt that make hours = 2?
19th Sep 2021, 4:15 PM
Mando
Mando - avatar
0
#include <iostream> using namespace std; int main() { int distance = 0; for (int k = 40; k < 200; k+=40){ cout << "k" << endl; return 0; }
19th Sep 2021, 3:51 PM
Mando
Mando - avatar
0
I don't understand what you intend with the for loop. Can you explain, please? According to the task description, you should only do one calculation and output.
19th Sep 2021, 4:02 PM
Lisa
Lisa - avatar
0
Lisa It should output 40 80 120 160 200
19th Sep 2021, 4:04 PM
Mando
Mando - avatar
0
Ah, okay, sorry! Misunderstood the question!
19th Sep 2021, 4:05 PM
Lisa
Lisa - avatar
0
Martin Taylor what is the hours++ for?
19th Sep 2021, 4:11 PM
Mando
Mando - avatar
0
Can helb me
29th Mar 2023, 5:29 AM
حسام حسن
حسام حسن - avatar