Académique Documents
Professionnel Documents
Culture Documents
ALGORITHMIQUE ET
STRUCTURE DE DONNÉES 1
TP EN LANGAGE C
Préparé par :
Mme Messabih.H
2021/2022
PLAN
Chapitre 1: langage C
Instructions de base
Structures conditionnelles
Structures répétitives
Tableaux
Fonction
STRUCTURES CONDITIONNELLES
La structure conditionnelles est une structure dont les
instructions sont exécutées selon les réponses des
conditions
STRUCTURES CONDITIONNELLES
1-Structure conditionnelle simple(un choix)
Syntaxe:
if (Condition) if (Condition) {
Instruction;
Instruction1;
Instruction2;
…..
}
STRUCTURES CONDITIONNELLES
Écrire un programme qui demande un entier à l’utilisateur,
teste si ce nombre est positif (≥ 0) ou non, et affiche “positif”
ou “négatif”.
#include<stdio.h>
#include<stdlib.h>
main() {
int i;
printf("tapez un entier\n");
scanf("%d",&i);
if(i>=0)
printf("il est positif\n");
system("pause");
}
LES OPÉRATEURS DE COMPARAISON
Symbole Signification
== Est égal à
> Est supérieur à
< Est inférieur à
>= Est supérieur ou égal à
<= Est inférieur ou égal à
!= Est différent de
LES OPÉRATEURS LOGIQUES
Les opérateurs logiques permettent de combiner des
expressions logiques(conditions).
Symbole Signification
&& et
|| ou
! non
LES OPÉRATEURS LOGIQUES
A B A&&B A||B !A
false false false false true
false true false true true
true false false true false
true true true true false
STRUCTURES CONDITIONNELLES
2-Structure alternative (deux choix)
Syntaxe:
if (Condition) {
if (Condition)
Instruction1;
Instruction1;
Instruction2;
else
…..
Instruction2;
}
else {
Instruction1;
Instruction2;
…..
}
STRUCTURES CONDITIONNELLES
Écrire un programme qui demande un entier à l’utilisateur,
teste si ce nombre est positif (≥ 0) ou non, et affiche “positif”
ou “négatif”.
#include<stdio.h>
#include<stdlib.h>
main() {
int i;
printf("tapez un entier\n");
scanf("%d",&i);
if(i>=0)
printf("il est positif\n");
else
printf("il est negatif\n");
system("pause");
}
STRUCTURES CONDITIONNELLES
3-Structure imbriquée (multiple choix)
if (Condition1) {
Syntaxe:
Instructions;
}
else if (Condition2) {
Instructions;
}
else if (Condition3) {
Instructions;
}
else {
Instructions;
}
STRUCTURES CONDITIONNELLES
4-Structure à choix multiple: switch
switch (Expression){
case valeur1 :Instruction;
break;
case valeur2 :Instruction;
break;
……………………….:………………
case valeurN :Instruction;
break;
default: AuterInstruction;
break;
}
STRUCTURES RÉPÉTITIVES
La structure répétitive (boucle) permet d’exécuter
plusieurs fois une séquence d’instructions.
Dans une boucle, le nombre de répétitions peut être
connu, fixé a l’avance, comme il peut dépendre d’une
condition permettant l’arret et la sortie de cette boucle.
On distingue types de boucles courantes en C :
1. while
2. do. . . while
3. for
STRUCTURES RÉPÉTITIVES (FOR)
Cette boucle permet d’exécuter une séquence
d’instructions un nombre de fois connu fixé à l’avance.
Utilisée pour préparer notre
variable compteur.
Syntaxe:
instructionN;
Est exécutée à la fin de chaque tour de
} boucle pour mettre à jour la variable
C'est la condition qui dit si la
boucle doit être répétée ou non. compteur.
main() {
int i;
for (i=0;i<5;i++){
printf("hello \n");
}
system("pause");
}
STRUCTURES RÉPÉTITIVES (WHILE)
Cette boucle permet de répéter un bloc d’instructions
tant qu’une condition est vraie.
Syntaxe:
while ( Condition ) {
instruction 1 ;
Instruction 2;
Instruction 3;
Instructions à
Instruction N; répéter
main() {
int i=0;
while (i<5){
printf("hello \n");
i++;
}
system("pause");
}
STRUCTURES RÉPÉTITIVES (DO
….WHILE)
Cette boucle permet de répéter un bloc d’instructions
tant qu’une condition est vraie.
Syntaxe:
do{
instruction 1 ;
Instruction 2;
Instruction 3;
Instructions à
Instruction N; répéter
} while ( Condition ) ;
main() {
int i=0;
do {
printf("hello \n");
i++;
} while (i<5);
system("pause");
}
STRUCTURES RÉPÉTITIVES (EX1)
#include<stdio.h>
#include<stdlib.h>
main(){
int i,fj,fi,f,n;
printf("Donner N un nombre entier positif:");
scanf("%d",&n);
fj=0;
fi=1;
for( i=2;i<=n; i++) {
f=3*fi+2*fj;
fj=fi;
fi=f;
}
printf("F%d=%d \n",n,f);
system("pause");
}
STRUCTURES RÉPÉTITIVES (EX2)
#include<stdio.h>
#include<stdlib.h>
main(){
int i,n,signe;
float s,x,p;
printf("Calcul de ln(X+1).\nDonner N et X, deux nombres entier:");
scanf("%d%f",&n,&x);
s=0;p=x;signe=1;
for( i=1;i<=n; i++) {
s=s+signe*(p/i);
p=p*x;
signe=(-1)*signe;
}
printf("ln(1+%f)=%f \n",x,s );
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
main(){
int val,s,choix,min,max,cpt;
printf("************Menu************\n");
printf("[1]->le min des valeurs lues\n");
printf("[2]->le Max des valeurs lues\n");
printf("[3]->la moyenne des valeurs lues\n");
do {
printf("Choix:[1] [2] [3]:");
scanf("%d", &choix);
}while(choix!=1 && choix!=2 && choix!=3 );
printf("Donner un nombre entier:");
scanf("%d",&val);
min=val; max=val; s=0; cpt=0;
while(val!=0) {
if (choix==1 && min>=val)
min=val;
else if(choix==2 && max<=val)
max=val;
if (choix==3) {
s+=val;cpt++;
}
printf("Donner un nombre entier:");
scanf("%d",&val);
switch (choix){
case 1:
printf("Le min :%d \n",min); break;
case 2:
printf("Le max :%d\n",max); break;
case 3:
printf("La moyenne :%f\n",(float)s/cpt); break;
}
system("pause");
}
STRUCTURES RÉPÉTITIVES (EX4)
#include<stdio.h>
#include<stdlib.h>
main(){
int a,b;
printf("Donner deux nombres entiers:");
scanf("%d%d",&a,&b);
while(a*b!=0) {
if (a>b)
a=a-b;
else
b=b-a;
}
if (a==0)
printf("le PGCD(%d,%d)=%d\n",a,b,b);
else
printf("le PGCD(%d,%d)=%d\n",a,b,a);
system("pause");
}
TABLEAUX
Definition:Un tableau est une suite d’éléments de même
type .
Premier élément
9ème élément
0 1 2 3 4 5 6 7 8 9 Indices
14 12 9.5 11 7.5 13 16 12 10 18
Valeurs
TABLEAUX (DECLARATION)
Syntaxe
0 1 2 3 4 5 6 7 8 Taille-1
Exemple:
int T[10];
TABLEAUX (DÉCLARATION ET
INITIALISATION)
Syntaxe 1 :
Syntaxe 2:
Type nom_tab [ ]={val1,val2,………,valn};
Syntaxe 3:
Type nom_tab [ ]={ };
TABLEAUX (REMPLIR UN TABLEAU)
Syntaxe d’affectation:
nom_tab [ indice]=Valeur;
Exemple:
#include<stdio.h>
#include<stdlib.h>
main(){
int i,T[5];
for (i=0;i<5;i++){
T[i]=i+1;
printf("T[%d]=%d\n",i,T[i]);
}
system("pause");
}
TABLEAUX (REMPLIR UN TABLEAU)
Syntaxe de lecteur:
scanf(“%...", &nom_tab [ indice]);
Exemple:
#include<stdio.h>
#include<stdlib.h>
main(){
int i,T[5];
for (i=0;i<5;i++){
Exemple:
#include<stdio.h>
#include<stdlib.h>
main(){
int i,T[]={1,2,3,4,5};
for (i=0;i<5;i++){
printf(“%d\n", T[ i]);
}
system("pause");
}
TABLEAUX À DEUX DIMENSIONS
Un tableau à deux dimensions est à interpréter comme
un tableau de dimension N dont chaque élément est un
tableau de dimension M.
Tab:
N Lignes
M Colonnes
TABLEAUX À DEUX
DIMENSIONS(DÉCLARATION)
Syntaxe :
Exemple:
int A[3][2];
TABLEAUX À DEUX
DIMENSIONS(INITIALISATION)
Syntaxe1 :
Exemple:
int A[3][2]={{1,2},
{10,20},
{100,200}};
TABLEAUX À DEUX
DIMENSIONS(ACCÈS )
Syntaxe d’affectation :
nom_tab [ nbr_lignes][nbr_colonnes]=Valeur;
Exemple:
int A[3][2]=2;
TABLEAUX À DEUX
DIMENSIONS(ACCÈS )
Syntaxe de lecteur:
Syntaxe d’écriture:
Sous-
Programmes
Sous-
Programmes
Sous-
Programmes
Résultats
FONCTION
X F(…) y
FONCTION
Une fonction est une suite d’instructions regroupées sous un nom; elle prend en
entrée des paramètres (arguments) et retourne un résultat.
Utilisation de la fonction en
C
Instructions;
Instructions;
}
}
X= nom_fonction ();
nom_fonction ();
Z= y/ nom_fonction ();
printf(nom_fonction ());
}
}
main () {
main () {
X= nom_fonction (arg1,…);
nom_fonction ( arg1,..); Z= y/ nom_fonction (arg1,..);
printf(nom_fonction (arg1,..));
}
}
FONCTION
Exemple: calculer la somme des 5 premiers nombres et la somme des 8 premiers nombres,
puis effectuer le produit entre les deux sommes
Solution 1, sans les fonctions Solution 2, avecles fonctions
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>