Algorithms – Chapter 3

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


The conditions

A program does not always run sequentially. Normally it makes a decision at some point to execute only part of itself. Programming languages provide instructions on how to make these choices. Among these instructions we have conditional instructions.

  • Conditional instruction (simple)
  • Alternative (conditional) instruction

Flowcharts were one of the important tools for writing algorithms because a well designed flowchart is easy to follow. But, as the algorithm becomes more complex the flowchart becomes more difficult to read and write.Now a more modern presentation called Unified Modelling Language (U.M.L.) is preferred.

Conditional instructions

In English pseudo language

  1. If condition
  2. Then
  3. Sequence of instructions
  4. End

In C language

if (Condition)
{
Sequence of instructions
}

Example

Write a program that reads two numbers ( A and B ) and then, if A is greater than B, exchanges their values. Assuming A and B are initialized, the part of the program which expresses the condition is present as below.

In English pseudo language

IF A > B
THEN
C: = A ;
A: = B ;
B: = C ;
ENDIF

In C language

if (A > B) 
{
C = A ;
A = B ;
B = C ;
}

The complete solution of the example:

In pseudo language

VARIABLES A,B,C : integers;
WRITE "Enter the value of A: "
READ A
WRITE "Enter the value of B: "
READ B
IF A > B
THEN
C := A ;
A := B ;
B := C ;
ENDIF

WRITE « A= »,A ;
WRITE « B= »,B ;

In C language

#include <stdio.h>
int main()
{
/* the declarations */
int A, B, C ;
/* lecture de A ey B */
printf("\n Get the value of A : ");
scanf("%d",&A) ;
printf("\n Get the value o B : ");
scanf("%d",&B) ;
/* execute the condition */
if ( A > B )
{
C = A ;
A = B ;
B = C ;
}
/*output the result */
printf("\n A = %d \n", A);
printf("\n B = %d \n", B);
}

Alternative conditional instruction

In pseudo language

IF condition
THEN
Sequence of instructions 1
IFNOT
Sequence of instructions 2
ENDIF

In C Language

if ( Condition )
{
Sequence of instructions 1
}
else {
Sequence of instructions 2
}

Example

Write an algorithm that asks for the pre-tax price and quantity of an item, then displays an invoice (VAT rate = 19.60%). A discount of 5% on the total price excluding tax is granted when the quantity ordered is greater than 9.

Q: integer; /* ordered quantity */
PTHT: actual number: /* total price excluding tax. */
TR: actual number /* discount rate. */
Rist: actual number; /* discount granted */
QMR: integer; /* Minimum quantity for rebate */

The part of the program that expresses the alternative condition is as follows.

In pseudo language

IF Q > QMR
THEN
Rist := PTHT * TR 
IFNOT
Rist := 0 
ENDIF 

In C language

if (Q > QMR)
{
Rist = PTHT * TR ;
}
else
{
Rist = 0 ;
}

Exercise 0

Give the complete solution of the previous example (The diagram, the pseudo code, the program in C language).

In Psuedocode

Variables QMR, RIST, TR, PTHT, Q, TOT, VAT;
QMR := 9;
Rist:= 0;
TR : = 0.05;
START
Write << What is the price before tax?:  >>;
Read PTHT;
WRITE << How many are there?:  >>;
READ Q;
IF Q > QMR
THEN
Rist = (PTHT * Q) * TR;
ENDIF
TOT: = (Q * PTHT) - RIST;
VAT: = TOT * 0.196;
WRITE << Your invoice is: >> (VAT + TOT)

In Java

import java.util.Scanner;

public class Application {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int qMR = 9;
        double rIST = 0;
        double tR = 0.05;

        System.out.print("What is the price before tax?: ");
        int pTHT = Integer.parseInt(reader.nextLine());
        System.out.print("How many are there?: ");
        int q = Integer.parseInt(reader.nextLine());

        if (q > qMR) {
            rIST = (pTHT * q) * tR;
        }

        double tOT = (q * pTHT) - rIST;
        double vAT = tOT * 0.196;
        System.out.println("Your invoice is: " + (vAT + tOT));
    }
}

In C

#include <stdio.h>
int main()
{
/* the declarations */
int Q, PTHT, TR, Rist, QMR, TOT, VAT, INV;
QMR = 9;
TR = 0.05;
/* Get values of Q and PTHT */
printf("\n What is the price before tax?: ");
scanf("%d",&PTHT) ;
printf("\n How many are there?: ");
scanf("%d",&Q) ;
/* execute the condition */
if (Q > QMR)
{
Rist = (PTHT * Q) * TR;
}
else
{
Rist = 0 ;
}

TOT = (Q * PTHT) - Rist;
VAT = TOT * 0.196;
INV = TOT + VAT;

/*output the result */
printf("\n INV = %d \n", INV);
}

Leave a comment