0% ont trouvé ce document utile (0 vote)
122 vues43 pages

Présentation Cour Et TPen C

Le document présente les structures de contrôle en langage C, notamment les structures conditionnelles comme if/else et switch, ainsi que les structures répétitives comme for, while et do-while. Il contient également des explications sur les tableaux et les fonctions.

Transféré par

selsabilhaddab2004
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PPTX, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
122 vues43 pages

Présentation Cour Et TPen C

Le document présente les structures de contrôle en langage C, notamment les structures conditionnelles comme if/else et switch, ainsi que les structures répétitives comme for, while et do-while. Il contient également des explications sur les tableaux et les fonctions.

Transféré par

selsabilhaddab2004
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PPTX, PDF, TXT ou lisez en ligne sur Scribd
Vous êtes sur la page 1/ 43

Université d’Alger 1

Faculté des sciences


Département Mathématique Et Informatique
Licence
1ère année

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

Ils servent à comparer deux expressions entre elles

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:

for (initialisation; condition; incrémentation){


instruction1;
instruction2;

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.

for (initialisation; condition; incrémentation)


instruction1;
STRUCTURES RÉPÉTITIVES (FOR)
#include<stdio.h>
#include<stdlib.h>

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

La vérification de la condition s’effectue


avant l’exécution des instructions.
STRUCTURES RÉPÉTITIVES (WHILE)
#include<stdio.h>
#include<stdlib.h>

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 ) ;

La vérification de la condition s’effectue


après l’exécution des instructions.
STRUCTURES RÉPÉTITIVES (DO
….WHILE)
#include<stdio.h>
#include<stdlib.h>

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

Type nom_tab [ Taille];

nom_tab 14 12 9.5 11 7.5 13 16 12 10 18

0 1 2 3 4 5 6 7 8 Taille-1

Exemple:

int T[10];
TABLEAUX (DÉCLARATION ET
INITIALISATION)
Syntaxe 1 :

Type nom_tab [ N]={val1,val2,………,valn};

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++){

scanf(“%d", &T[ i]);


}
for (i=0;i<5;i++)
printf("T[%d]=%d\n",i,T[i]);
system("pause");
}
TABLEAUX (AFFICHER LES ÉLÉMENTS DU TABLEAU)
Syntaxe d’écriture:

printf(“%...", nom_tab [ indice]);

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 :

Type nom_tab [ nbr_lignes][nbr_colonnes];

Exemple:

int A[3][2];
TABLEAUX À DEUX
DIMENSIONS(INITIALISATION)

Syntaxe1 :

Type nom_tab [ N][M]={{val11,val12 ….. ,val1m},


{val21,val22 ….. ,val2m},
…………
{valn1,valn2 ,valnm}};
Syntaxe1 :
Type nom_tab [ N][M]={val11,val12 ….. ,valnm};

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:

scanf(“%...", &nom_tab [ nbr_ling] [ nbr_colon]);

Syntaxe remplir tous les éléments:

for (i=0;i<nbr_lignes ;i++){


for (j=0;j<nbr_lignes ;j++){
scanf(“%...", &nom_tab [ i] [ j]);}}
TABLEAUX À DEUX
DIMENSIONS(ACCÈS )

Syntaxe d’écriture:

printf(“%...", nom_tab [ nbr_ling] [ nbr_colon]);

Syntaxe afficher tous les éléments:

for (i=0;i<nbr_lignes ;i++){


for (j=0;j<nbr_lignes ;j++){
printf(“%...", nom_tab [ i] [ j]);}}
FONCTION
Entrées

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.

Une fonction est écrite séparément du corps de programme principal (main) et


sera appelée par celui-ci lorsque cela sera nécessaire.
FONCTION

Utilisation de la fonction en
C

Déclarer la fonction Appeler la fonction


FONCTION

Syntaxe de déclaration de la fonction :


Argument de
fonction
Type_retour nom_fonction (type arg1,type arg2,….) {
Instruction1;
Instruction2;
Instruction3;
….;
return resultat; Traitement
}

Type de Résultat retourné


résultat
retourné
FONCTION (TYPES)
Void nom_fonction () { Type_retour nom_fonction () {

Instructions;
Instructions;
}
}

Pas de retour et pas Avec retour et pas


d’arguments d’arguments

Pas de retour et avec Avec retour et avec


d’arguments d’arguments

Void nom_fonction (type arg1,..) {


Type_retour nom_fonction (type arg1,..) {
Instructions;
Instructions;
}
}
Procédure
FONCTION (APPEL DE LA FONCTION)
main() { main () {

X= nom_fonction ();
nom_fonction ();
Z= y/ nom_fonction ();
printf(nom_fonction ());
}
}

Pas de retour et pas Avec retour et pas


d’arguments d’arguments

Pas de retour et avec Avec retour et avec


d’arguments d’arguments

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>

main() int somme(int N)


{ { int S, i;
S=0;
int i, S1, S2,P;
for(i=0;i<=N;i++){S=S+i;}
S1=0; return S;
for(i=0;i<=5;i++){S1=S1+i;} }
main()
S2=0; {
for(i=0;i<=8;i++){S2=S2+i;} int S1, S2,P;
S1=somme(5);
P=S1*S2; S2=somme(8);
P=S1*S2;
printf("le produit =%d",P); printf("le produit =%d",P);
} }

Vous aimerez peut-être aussi