# Algorithms – Chapter 5

Ekluboko, K. (2018) Algorithme (Course material for Programmation : s’initier)

## Loop or repeat

Thanks to iterative statements, it is possible to repeat the execution of a statement more than once. A loop consists of executing a sequence of instructions or blocks more than once in language C.

In most languages there are two types of repetition:

• Where the continuation of the repetition of the instructions concerned depends on a certain condition. In this case we don’t know how many times the loop will repeat (conditional or indefinite loop).
• Where the repetition happens a specific number of times.

## Loop, as long as…do

In English;

``````As long as the condition is met
[execute instructions
Finish as long as...;``````

Another way to describe how this structure works (For languages that only have si(if) and
return or go or jump (goto)).

``````1)Evaluate the condition
3)Execute the entire sequence
5)continuation of the algorithm.``````

In the C language;

``````while (condition)
{
sequence of instructions
}``````

## Repeat loop until…

In English;

``````repeat
[execute instructions
until;``````

The functioning of this structure can also be described as;

``````1)Execute the entire sequence
2)Evaluate the condition
4)continuation of the algorithm.``````

In the C language;

``````do
{
sequence of instructions
}
while (condition)``````

## Loop for… do

In the previous cases, the body of the loop had to be executed a variable number of times, but the number of executions may be known so we can use for;

In English;

``````for variable: loop until completion
[execute instructions
end for loop``````

If the start is higher than the finish, the sequence is never executed. The variable automatically changes value with each iteration.

In the C language;

``````for ( initialise ; condition ; modify variable )
{
sequence of instructions
}``````

The functioning of the FOR loop can be described as;

``````a) Initialization of departure
b) Evaluate condition, if its value is NO, go to f)
c) Run the entire sequence
d) Modify the variable
f) end``````

## More examples in C

``````#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int x;
int sum = 0;

printf("Enter a number: \n");

do{
scanf("%d",&x);
sum = sum + x;
} while ( x != 0 );

printf("Sum = %d\n",sum);

system("PAUSE");
return 0;
}``````

Here we take the variables x and sum, both of which are integers. We then ask for the user to enter a number which is scanned into variable x. Variable sum is then added to x to create a new value for sum and finally variable x is tested to make sure it was not 0 ( the value designated to escape the loop). If it is not the loop repeats and another number is entered, etc… If it is 0 then the loop ends and the program prints “Sum = ” to the screen with whatever the value of sum is after that.

``````#include <stdio.h>
#include <stdlib.h>
#define zero (0)

int main(int argc, char *argv[])
{
int x = 0;
int sum = 0;

printf("Enter a number\n");

do{

scanf("%d",&x);
if (x%2 == 0){
sum = sum + x;
}
} while ( x != 0 );

printf("Sum = %d\n",sum);

system("PAUSE");
return 0;
}``````

This program in almost the same as the one above except that an if statement has been added so that is only adds x to sum if x is an even number. This is tested through the use of x modulo 2 (x%2) which gives the remain of x if divided by 2. If the remainder is equivalent to 0 then the number must have been an even number and the inside of the if condition is executed.

``````#include <stdio.h>
#include <stdlib.h>
#define zero (0)

int main(int argc, char *argv[])
{
int x = 0;
int sum = 0;
int prod = 1;

printf("Enter a number\n");

do{

scanf("%d",&x);
if (x%2 == 0){
sum = sum + x;
}
else if(x%2 != 0){
prod = prod * x;
}

} while ( x != 0 );

printf("Somme = %d\n",sum);
printf("Product = %d\n",prod);

system("PAUSE");
return 0;
}``````

A further extension of above except that the variable prod is added and another condition is added in the if statement (else if) so that if x modulo 2 does not equal 0 (eg; the number is odd) then x is multiplied by prod. This also accounts for prod being initialised with a value as 1 because if it wasn’t then prod would always equal 0.

## Exercise 1

Make a program in C in which a sum is entered and if it is over 299 it is discounted by 10% and if not it is discounted by 5%. The sum, rate of discount and discount should then be output.

``````#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int rab;
int cot;
int somme = 0;

printf("Entrer la somme: ");
scanf("%d",&somme);

if ( somme > 299) {
cot = 10;
rab = somme * 0.1;
somme = somme * 0.9;
}
else {
cot = 5;
rab = somme * 0.05;
somme = somme * 0.95;
}

printf("Somme = %d",somme);
printf("\nRabais = %d",rab);
printf("\nCoter = %d",cot);
printf("%%");

system("PAUSE");
return 0;
}``````

## Exercise 2

Translate the pseudocode below into C.

``````Variable x: integer
START
WRITE "Give the starting integer"
As long as x < 100 Do
WRITE x
x:= x+20
End Do
END``````

### C code

``````#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int x = 0;

printf("Give the starting integer: ");
scanf("%d",&x);
while( x < 100)
{
x = x + 20;
}

printf("%d",x);

system("PAUSE");
return 0;
}
``````

## Exercise 3

As in exercise 2, translate the pseudocode into C.

``````Variable x: integer
START
WRITE "Give the starting integer"
REPEAT
WRITTEN x
x: = x*2
UP TO > 100
END``````

### C code

``````#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int x = 0;

printf("Givethe starting integer: ");
scanf("%d",&x);
while( x < 100)
{
x = x * 2;
}

printf("%d",x);

system("PAUSE");
return 0;
}``````