# Simple Loops – Problems

The fourth problem of the "Programming Basics" Practical Exam includes a single loop with simple logic in it. Let's take a few examples.

## Problem: Sum Step 3

There are n integers a1, a2, …, an given. Calculate the sums:

• sum1 = a1 + a4 + a7 + … (the numbers are summed, starting from the first one with step 3).
• sum2 = a2 + a5 + a8 + … (the numbers are summed, starting from the second one with step 3).
• sum3 = a3 + a6 + a9 + … (the numbers are summed, starting from the third one with step 3).

### Input

The input data is read from the console. The first row is an integer n (0 ≤ n ≤ 1000). On the next n rows are n integers in the range [-1000 … 1000]: a1, a2, …, an.

### Output

On the console should be printed 3 rows containing the 3 sums in a format such as in the example.

### Sample Input and Output

Input Output Input Output Input Output
2
3
5
sum1 = 3
sum2 = 5
sum3 = 0
4
7
-2
6
12
sum1 = 19
sum2 = -2
sum3 = 6
5
3
5
2
7
8
sum1 = 10
sum2 = 13
sum3 = 2

We will take the count of numbers from the console and declare starting values of the three sums.

Since we do not know in advance how many numbers we will process, we will take them one at a time in a loop which will be repeated n times and we will process them in the body of the loop.

### Allocating Numbers and Print Results

To find out in which of the three sums we need to add the number, we will divide its sequence number into three and we will use the remainder. We will use the variable i, which tracks the number of spins per loop, to find out which sequence number we are. When the remainder of i/3 is zero, it means we will add this number to the first sum when it is 1 to the second and when it is 2 to the third.

Finally, we will print the result on the console in the required format.

## Problem: Increasing Elements

A number of n numbers are given: a1, a2, , an. Calculate the length of the longest increasing sequence of consecutive elements in the row of numbers.

### Input

The input data is read from the console. The first row is an integer n (0 ≤ n ≤ 1000). On the following n lines are n integers in the range [-1000 … 1000]: a1, a2, , an.

#### Output

On the console must be printed one number - the length of the longest increasing sequence.

### Sample Input and Output

Input Output Input Output Input Output Input Output
3
5
2
4
2 4
2
8
7
6
2 4
1
2
4
4
3 4
5
6
7
8
4

To solve this problem, we need to think a bit more algorithmically. A sequence of numbers is given to us, and we need to check whether each subsequent will be larger than the previous one, and if so we count how long is the sequence in which this condition is fulfilled. Then we have to find which one subsequence of these is the longest. To do this, let's make some variables that we will use during the problem.

The variable n is the count of numbers we get from the console. In countCurrentLongest, we will keep the number of elements in the increasing sequence we are currently counting. For example, in the sequence: 5, 6, 1, 2, 3 countCurrentLongest will be 2 when we reach the second element of the counting (5, 6, 1, 2, 3) and will become 3 when we reach the last element (5, 6, 1, 2, 3), because the increasing row 1, 2, 3 has 3 elements. We will use countLongest to keep the longest increasing sequence. The other variables are a - the number we are currently in, and aPrev - the previous number which we will compare with a to see if the row is growing.

### Determining Increasing Sequence

We begin to rotate the numbers and check if the present number a is larger than the previous aPrev. If this is true, then the row is growing and we need to increase its number by 1. This is preserved in the variable that tracks the length of the sequence we are currently in - countCurrentLongest. If the number a is not greater than the previous one, it means that a new sequence starts and we have to start the count from 1. Finally, after all the checks, aPrev becomes the number we are currently using and we start the loop from the beginning with the next entered a.

Here is a sample implementation of the algorithm described:

### Find and Print the Longest Sequence

It remains to be seen which of all sequences is the longest. We will do this by checking in the loop if the sequence we are currently has become longer than the longest by now. The whole loop will look like this:

Finally, we print the length of the longest sequence found.