Professional Documents
Culture Documents
Algorithm&CProgramming Chap 1&2
Algorithm&CProgramming Chap 1&2
Algorithm&CProgramming Chap 1&2
ALGORITHMIC; C
PROGRAMMING AND
DATA STRUCTURES
Email : bjsylva@gmail.com
jean-sylvain.bucumi@ub.edu.bi
Phone: +257 71 017 262
+257 75 964 441
+257 69 686 003
Contents
Chapter V: Structures
TABLE OF CONTENTS
Contents ................................................................................................................................................... ii
TABLE OF CONTENTS ....................................................................................................................... iii
Chapter I: Background on Algorithms .................................................................................................... 1
I.1 Introduction .................................................................................................................................... 1
I.2 Caracteristics of an algorithm ........................................................................................................ 1
I.3 How to design an algorithm ........................................................................................................... 2
I.4 Structure of an algorithm ............................................................................................................... 2
I.5 Inputs/Outputs ................................................................................................................................ 2
I.6 Variables ........................................................................................................................................ 3
I.6.1 Variables of simple types ........................................................................................................ 3
I.6.2 Variables of complex types ..................................................................................................... 4
I.7 Determination of variables ............................................................................................................. 4
I.8 Operators ........................................................................................................................................ 5
I.9 Applications ................................................................................................................................... 5
Chapter II: BASES OF PROCEDURAL PROGRAMMING ................................................................. 6
II.1 Introduction................................................................................................................................... 6
II.2 Some programming forms ............................................................................................................ 6
II.3 C language program ...................................................................................................................... 6
II.4 Structure of a C program .............................................................................................................. 7
II.5 Steps for running a C program ...................................................................................................... 7
II.6 Creation of a project ..................................................................................................................... 7
II.7 Variables ....................................................................................................................................... 8
II.8 Display and input .......................................................................................................................... 8
II.9 Operators....................................................................................................................................... 9
II.10 Application exercises .................................................................................................................. 9
Chapter III: CONTROL STRUCTURES .............................................................................................. 10
III.1 Introduction ............................................................................................................................... 10
III.2 Conditional control structures ................................................................................................... 10
III.2.1 IF – ELSE ........................................................................................................................... 10
III.2.2 SWITCH - CASE ............................................................................................................... 11
III.3 Repetitive control structures ...................................................................................................... 11
III.3.1 For ...................................................................................................................................... 12
III.3.2 Do-while ............................................................................................................................. 12
III.3.3 While .................................................................................................................................. 13
We can have multiple algorithms to solve the same problem and an algorithm can be
implemented in different programming languages. The faster algorithm is the one which takes
least space and time to execute.
I.5 Inputs/Outputs
An algorithm uses data supplied by the user to do the processing and can also display
messages intended for the user.
In algorithmic, the instruction to display a message may be:
Write (« Message »)
The message can be composed by text and / or data contained in the variables.
If it is a text, it must be enclosed in quotation marks. If it is data contained in variables, you
just need to mention only the names of the variables outside the quotation marks.
Example: Let consider the variable b that contains the value 2. The instruction to display this
value is:
Write (« The value of b = », b)
To get a value entered on the keyboard by the user, we can use the instruction:
Read (variable)
The variable mentioned between () must be declared in the section. It is this variable which
will contain the value entered by the user on the keyboard. This value must have the same
type as the variable.
You can get more than one value in the same Read instruction. In this case, just indicate the
different variables that will contain the entered values separated by commas.
Example. Read (a,b)
I.6 Variables
A variable (in computer science) is a memory location that can contain data. Its type depends
on the data it can contain. A variable is characterized by:
- The name: generally composed by letters, it used to identify the variable in a unique
way in the algorithm.
- The type: it indicates the type of data that the variable can contain. This is the area of
definition of the variable.
- The value: it is what the variable contains. At a given moment, a variable contains
only one value at a time.
Variables can be classified into 2 main categories:
- Variables of simple types
- Variables of complex types
I.8 Operators
The algorithms use different types of operators in their processing. The main types of
operators are :
- Arithmetic operators: +; -; ×; ÷; Mod
- Relational operators (comparison) : >, <, ≥,≤, =, ≠
- Logical operators: AND, OR, NOT
- Assignment operator: <-
I.9 Applications
Ex1: Write an algorithm to display Hello
Ex2: Write an algorithm to input an integer and then display its triple
Ex3: Write an algorithm allowing inputting 2 real numbers then to display the sum of these
values
Correction
1) Algorithm DisplayHello
Variables : …
START
Write (« Hello»)
END
Ex4 : Write an algorithm allowing to input 2 integers then to exchange their values and to
display these values before and after the exchange.
The created project contains source files; especially the one that contains the main program.
To enlarge or reduce the font size, just:
- Keep the Ctrl key pressed on the keyboard
- Use the middle mouse button to enlarge or reduce the size then release the Ctrl key.
If the mouse does not work properly, you can do otherwise as follows:
- - Go to the Edit menu and click on Special Commands
- - In the menu that appears, go to Zoom then click on In to enlarge or Out to reduce
Before writing the code, make sure the compiler is working properly. To do this, simply
execute the code in the main.c file as follows:
- Go to the Build menu
- Click on Build and run in the menu that appears
You can also execute the file by pressing the F9 key
II.7 Variables
All variables used in the program must be declared. The C language offers a syntax for the
different types of data. Simple type data are declared using the following keywords: int, float,
char, long, double.
To declare a variable, you must specify the type and name of the variable. Any instruction;
even the statement; ends with a semicolon.
Example: Declaration of 2 integers a and b is done by: int a, b ;
It is always advisable to put comments in the program to facilitate its understanding. To
comment one line, just put // at the beginning of the line to comment.
To comment on several consecutive lines, just put /* at the beginning of the first line to
comment and */ at the end of the last line of the comment.
Comments are not executed by the compiler because it considers them as explanations.
To declare an array tab of 5 integers, we write: we write: int tab [5] ;
To display the value of a variable, you must specify its format in the place where the value
will be displayed in the message, then mention the name of the variable outside the quotes.
The format depends on the type of data.
For example: %d for int, %f forr float, %c for char, %l for long and double, %x for
hexadecimal, %s for string.
Ex1. Display the addresses of variables a and i.
printf("\nL'adresse de a est %x", &a);
printf("\nL'adresse de i est %x", &i);
To get a value entered on the keyboard, you can use the instruction:
scanf("format",&variable);
Example: the instruction to enter an integer and save it in the variable a is: scanf("%d",&a);
II.9 Operators
Different types of operators are used in C language operations. These operators are classified
in the following categories:
- Arithmetic operators: +, -, *, /, %
- Relational operators (comparison): >, <, >=, <=, ==, !=
- Logical operators: &&, ||, !
- Assignment operator: =
- Address operator: &
- Increment operator: ++
- Decrement operator: --
III.2.1 IF – ELSE
This structure is used to choose an instruction block to execute from 2 alternatives offered. Its
syntax is as follows:
If (condition)
Instruction(s)_if_true ;
Else
Instruction(s)_if_false ;
It is a structure which tests a condition to know the block of instructions to execute: the one
which has the condition True as value.
The Else block is optional if the treatment to be done has not been specified when the
condition is not verified.
Ex1 : Write a program in C to input a real and then say whether it is less than 10.
Ex2 : Write a program in C to check if an integer given by the user is negative or not.
Ex3 : Write a program in C to check if a real given by the user is between 10 and 20
(inclusive).
- While : this structure is used if we are not sure to have even one execution if not
several executions (0;n).
III.3.1 For
This structure is used as a counter with a start value and a final stop value. Its syntax is as
follows:
For(i=0 ;i<n ;i++)
{ instructions ;}
Initialization : i = 0
Repetition condition: i < n
Modification of the value of the variables : i++
The modification may be in ascending order (++) or descending (--).
NB : The count starts with 0 in programming while it starts with 1 in algorithmic.
Ex6 : Write a program in C to display Hello 10 times.
Ex7 : Write a program in C to display the Arabic numbers.
III.3.2 Do-while
This structure is used if the number of executions is not known in advance but it is sure to
have at least one execution. Its syntax is of the form:
do
{
instruction(s);
}while (condition) ;
Initialization can be done before or inside the loop. This structure makes possible to execute
the instructions once then to test the condition to know whether or not to resume the execution
of the instructions of the block.
The repeat condition generally corresponds to the opposite of the stop condition. This
structure is often used in input control.
In this structure, one of the block instructions must modify the value of the variable used in
the condition.
NB : Instructions using the for structure can be translated into the do - while structure, but not
vice versa.
Ex8 : Write a program in C to display the Arabic numerals using do-while
Ex9 : Write a program in C allowing to input a negative integer then display the selected
value.
III.3.3 While
This structure is used if we do not know the number of executions and it is even possible not
to execute the instructions of the block. Its syntax is of the form :
while (condition)
{
instruction(s);
}
This structure first tests a condition, if its value is TRUE, it executes the instructions of the
corresponding block. Otherwise, it ignores the instruction block to continue with the program.
In this structure, initialization is done before entering the loop. To avoid an infinite loop, one
of the block instructions must modify the value of the variable used in the condition.
Ex10 : Write a program in C allowing to input a negative integer using while then display the
selected value.
Ex11 : Write a program in C allowing you to input a strictly positive integer then calculate
and display its multiples which are less than 100.
Ex22 : Write a program in C allowing to input 2 integers and to check if they are divisible one
by the other.
Ex23 : Write a program in C allowing to input an integer greater than 10 then to say if it is a
prime number or not.
Ex24 : Write a program in C allowing you to input a secret code of integer type and then
guess it without exceeding 10 attempts.
Ex25 : Write a program in C allowing you to input an integer a and a positive integer n then
compute and display the value of an.
Ex26 : Write a program in C allowing to input 2 strictly positive integers then to find their
least common multiple (‘PPCM’).
Ex27 : Write a program in C allowing to input 2 strictly positive integers then to find their
greatest common divisor (‘PGCD’).
Ex28 : Write a program in C allowing to count and display the number of occurrences of an
integer given by the user among 30 integers entered before.
Ex29 : Write a program in C to verify that an integer given by the user exists among 40
integers entered before.
Ex30 : Write a program in C to calculate the success rate of a class of 60 students knowing
that the pass mark is 10/20.
Ex31 : Write a program in C allowing you to input an integer strictly greater than 10 then
calculate and display the sum of the 3 integers directly greater than the integer entered.
Ex32 : Write a program in C allowing you to input 5 integers in ascending order (each new
value must be greater than the previous one) then display them.
IV.2 Definitions
A function is an independent program triggered by a call, with or without parameters; from
another program that executes its instruction block and stops by returning a value to the
calling program.
A procedure is also an independent program triggered by a call; with or without parameters;
from another program that executes its instruction block and returns nothing after execution or
rather returns the void.
We can classify functions and / or procedures into 2 categories:
- Simple functions (procedures)
- Recursive functions (procedures)
FunctionName : this is the name of the function. It is written in one word and designates the
processing performed by the function or the expected result.
Parameters : It is one or more values provided to the function to perform its task. If a
function uses parameters, you must always specify the type and possibly the name of the
variable.
The first line of the function declaration (Return_Type, FunctionName and parameters)
constitutes its signature. It is also called the function prototype. The return statement is last
executed before exiting the function.
A procedure has the same syntax as a function with void as return_type and without having
the return statement. A procedure is of the form :
void ProcédureName (parameters)
{
Instructions(s) ;
}
The signature of a procedure is composed by void, procedureName and parameters.
NB : Even if a function or procedure has no parameters, you must always put empty
parentheses.
Ex1 : Write a procedure in C named Greeting to display Hello.
Ex2 : Write a function in C allowing to input an integer then to return the entered value.
Ex3 : Write a function in C to compute the triple of an integer given in parameters.
Ex4 : Write a function in C to compute the sum of 2 integers given in parameters.