The Best Days in a Row

Next week, you can open the stand only on a few days in a row, so you'd like to know which days in a row sold the most this week. The new variable days_in_a_row says how many days it will be: 3. Add one more line to the report:

Goal reached on day 5
Best days: day 3 to day 5 (64 cups)

On days 3, 4 and 5, you sold 9 + 25 + 30 = 64 cups. Days 4 to 6 sold 62 cups and days 5 to 7 only 58. If two runs of days sold the same, the earlier one wins.

The program has to work for any number of days in a row, not only for 3: the exercise tries 2 and 7 too. With days_in_a_row = 2, the best days are day 4 to day 5, with 55 cups.

A tip: for each day on which a run can start, add up its days in an inner loop, as in the exercise on nested loops. Watch the last run: it ends on the last day of the week, and no run may reach past the end of the array.

That's the whole report. Your program goes through a week of sales and finds the total, the average, the best day, the day you reached your goal and the best days in a row, all with loops, arrays and conditions. In the next lesson, on strings, you'll find out that text is an array too, so the report could show the names of the days instead of their numbers.

Instructions

Print the best days in a row and how many cups you sold on them.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

3/3