You are on page 1of 13

09/08/13

Exercices corrigs en C++ (Les tableaux)


Friday, 07 November 2008 15:16

Exercices corrigs en C++ (Les tableaux)

EXERCICE 1
Ecrire un programme qui demande l'utilisateur de saisir 10 entiers stocks dans un tableau. Le programme doit afficher le nombre d'entiers suprieurs ou gaux 10.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : recherche du nombre d'lments vrifiant une proprit. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n tt [ 1 0 ] , i , n b = 0 ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > t [ i ] ; } f o r ( i = 0 ; i < N ; i + + ) i f ( t [ i ] > = 1 0 ) n b + + ; c o u t < < " L en o m b r ed ' e n t i e r ss u p r i e u r so u g a u x1 0e s t:" < < n b < < e n d l ; r e t u r n0 ; }

EXERCICE 2
Ecrire un programme qui demande l'utilisateur de saisir 10 entiers stocks dans un tableau ainsi qu'un entier V. Le programme doit rechercher si V se trouve dans le tableau et afficher "V se trouve dans le tableau" ou "V ne se trouve pas dans le tableau".

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : recherche d'un lment dans un tableau. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n tt [ N ] , i , V ; b o o lt r o u v e ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > t [ i ] ; } c o u t < < " T a p e zl av a l e u rd eV:" ; c i n > > V ;
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 1/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

t r o u v e = f a l s e ; i = 0 ; w h i l e ( ! t r o u v e& &i < N ) i f ( t [ i ] = = V ) t r o u v e = t r u e ;e l s ei + + ; i f ( t r o u v e )c o u t < < " L av a l e u rVs et r o u v ed a n sl et a b l e a u " < < e n d l ; e l s ec o u t < < " L av a l e u rVn es et r o u v ep a sd a n sl et a b l e a u " < < e n d l ; r e t u r n0 ; }

EXERCICE 3
Ecrire un programme qui demande l'utilisateur de saisir 10 entiers stocks dans un tableau. Le programme doit ensuite afficher l'indice du plus grand lment.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : recherche de l'indice du plus grand lment. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n tt [ N ] , i , i n d i c e ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > t [ i ] ; }i n d i c e = 0 ; f o r ( i = 1 ; i < N ; i + + ) i f ( t [ i n d i c e ] < t [ i ] ) i n d i c e = i ; c o u t < < " L ' i n d i c ed up l u sg r a n d l m e n te s t:" < < i n d i c e < < e n d l ; r e t u r n0 ; }

EXERCICE 4
Ecrire un programme qui demande l'utilisateur de saisir 10 entiers stocks dans un tableau ainsi qu'un entier V. Le programme doit rechercher si V se trouve dans le tableau et doit supprimer la premire occurence de V en dcalant d'une case vers la gauche les lments suivants et en rajoutant un 0 la fin du tableau. Le programme doit ensuite afficher le tableau final.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux.
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 2/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

Un algorithme simple sur un tableau : supression d'un lments avec dcalage des suivants. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n tt [ N ] , i , j , V ; b o o lt r o u v e ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > t [ i ] ; } c o u t < < " T a p e zl av a l e u rd eV:" ; c i n > > V ; t r o u v e = f a l s e ; i = 0 ; w h i l e ( ! t r o u v e& &i < N ) i f ( t [ i ] = = V ) t r o u v e = t r u e ;e l s ei + + ; i f ( t r o u v e ) { f o r ( j = i ; j < N 1 ; j + + ) t [ j ] = t [ j + 1 ] ; t [ N 1 ] = 0 ; } f o r ( i = 0 ; i < N ; i + + ) c o u t < < t [ i ] < < e n d l ; r e t u r n0 ; } }

EXERCICE 5
Ecrire un programme qui demande l'utilisateur de saisir 10 entiers stocks dans un tableau ainsi qu'un entier V et un entier i compris entre 0 et 9. Le programme doit dcaler d'une case vers la droite tous les lements partir de l'indice i (en supprimant le dernier lment du tableau) et doit mettre la valeur V dans le tableau l'indice i. Le programme doit ensuite afficher le tableau final.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : insertion dans en tableau avec dcalage. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) {
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 3/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

i n tt [ N ] , i , i n d i c e , V ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > t [ i ] ; } c o u t < < " T a p e zu ni n d i c e( d e09 ):" ; c i n > > i n d i c e ; c o u t < < " T a p e zl av a l e u rd eV:" ; c i n > > V ; i f ( i n d i c e > = 0& &i n d i c e < = N 1 ) { f o r ( i = N 1 ; i > i n d i c e ; i ) t [ i ] = t [ i 1 ] ; t [ i n d i c e ] = V ; } f o r ( i = 0 ; i < N ; i + + ) c o u t < < t [ i ] < < e n d l ; r e t u r n0 ; }

EXERCICE 6
Ecrire un programme qui demande lutilisateur de taper 10 entiers qui seront stocks dans un tableau. Le programme doit ensuite afficher soit "le tableau est croissant", soit "le tableau est dcroissant", soit "le tableau est constant", soit "le tableau est quelconque".

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : vrifier si le tableau vrifie une proprit donne. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n ta [ N ] , i ; b o o lt r o u v e = f a l s e ; b o o lc r o i s s a n t = t r u e , d e c r o i s s a n t = t r u e ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " V e u i l l e zt a p e rl ' e n t i e rn u m e r o" < < i < < ":" ; c i n > > a [ i ] ; } f o r ( i = 0 ; i < N 1 ; i + + ) { i f ( a [ i ] > a [ i + 1 ] ) c r o i s s a n t = f a l s e ; i f ( a [ i ] < a [ i + 1 ] ) d e c r o i s s a n t = f a l s e ; } i f ( c r o i s s a n t& &d e c r o i s s a n t )c o u t < < " l et a b l e a ue s tc o n s t a n t " < < e n d l ; i f ( c r o i s s a n t& &! d e c r o i s s a n t )c o u t < < " l et a b l e a ue s tc r o i s s a n t " < < e n d l ;
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 4/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

i f ( ! c r o i s s a n t& &d e c r o i s s a n t )c o u t < < " l et a b l e a ue s td e c r o i s s a n t " < < e n d l ; i f ( ! c r o i s s a n t& &! d e c r o i s s a n t )c o u t < < " l et a b l e a ue s tq u e l c o n q u e " < < e n d l ; r e t u r n0 ; }

EXERCICE 7
Ecrire un programme qui demande lutilisateur de taper 10 entiers qui seront stocks dans un tableau. Le programme doit trier le tableau par ordre croissant et doit afficher le tableau. Algorithme suggr : On cherche l'indice du plus petit lment parmi les indices de 0 9 et on change cet lment avec t[0]. On cherche l'indice du plus petit lment parmi les indices de 1 9 et on change cet lment avec t[1]. On cherche l'indice du plus petit lment parmi les indices de 2 9 et on change cet lment avec t[2]. ... On cherche l'indice du plus petit lment parmi les indices de 8 9 et on change cet lment avec t[8].

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : tri d'un tableau. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n ta [ N ] , i , j , m i n , i m i n , t m p ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " V e u i l l e zt a p e rl ' e n t i e rn u m e r o" < < i < < ":" ; c i n > > a [ i ] ; } f o r ( i = 0 ; i < N 1 ; i + + ) { i m i n = i ; m i n = a [ i ] ; f o r ( j = i + 1 ; j < < N ; j + + ) i f ( a [ j ] < m i n ) { m i n = a [ j ] ; i m i n = j ; } t m p = a [ i m i n ] ; a [ i m i n ] = a [ i ] ; a [ i ] = t m p ; } c o u t < < " V O I C IL ET A B L E A UT R I E: " < < e n d l ; f o r ( i = 0 ; i < N ; i + + ) c o u t < < " a [ " < < i < < " ] = " < < a [ i ] < < e n d l ; r e t u r n0 ; }

EXERCICE 8
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 5/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

Ecrire un programme qui demande lutilisateur de taper 10 entiers qui seront stocks dans un tableau. Le programme doit trier le tableau par ordre croissant et doit afficher le tableau. Algorithme suggr (tri bulle) : On parcourt le tableau en comparant t[0] et t[1] et en changeant ces lments s'ils ne sont pas dans le bon ordre. on recommence le processus en comparant t[1] et t[2],... et ainsi de suite jusqu' t[8] et t[9]. On compte lors de ce parcours le nombre d'changes effectus. On fait autant de parcours que ncessaire jusqu' ce que le nombre d'changes soit nul : le tableau sera alors tri.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Un algorithme simple sur un tableau : tri d'un tableau. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n ta [ N ] , i , n b , t m p ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " V e u i l l e zt a p e rl ' e n t i e rn u m e r o" < < i < < ":" ; c i n > > a [ i ] ; } d o { n b = 0 ; f o r ( i = 0 ; i < N 1 ; i + + ) i f ( a [ i ] > a [ i + 1 ] ) { t m p = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = t m p ; n b + + ; } } w h i l e ( n b ! = 0 ) ; c o u t < < " V O I C IL ET A B L E A UT R I E: " < < e n d l ; f o r ( i = 0 ; i < N ; i + + ) c o u t < < " a [ " < < i < < " ] = " < < a [ i ] < < e n d l ; r e t u r n0 ; }

EXERCICE 9
Ecrire un programme qui saisit 2 tableaux de 10 entiers a et b. c est un tableau de 20 entiers. Le programme doit mettre dans c la fusion des tableaux a et b. On copiera dans les 10 premires cases de c le tableau a, dans les dix dernires le tableau b. Le programme affiche ensuite le tableau c.

Solution
Cet exercice a pour but de vrifier les points techniques suivants :

univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1

6/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

Utilisation simple de tableaux. Un algorithme simple sur un tableau : fusion de 2 tableaux. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n ta [ N ] , b [ N ] , c [ 2 * N ] , i ; c o u t < < " S A I S I ED UT A B L E A Ua " < < e n d l ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > a [ i ] ; } c o u t < < " S A I S I ED UT A B L E A Ub " < < e n d l ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > b [ i ] ; } f o r ( i = 0 ; i < 2 * N ; i + + ) i f ( i < N ) c [ i ] = a [ i ] ; e l s ec [ i ] = b [ i N ] ; c o u t < < " V O I C IL ET A B L E A Uc " < < e n d l ; f o r ( i = 0 ; i < 2 * N ; i + + ) c o u t < < c [ i ] < < "" ; c o u t < < e n d l ; r e t u r n0 ; }

EXERCICE 10
Ecrire un programme qui saisit 2 tableaux de 10 entiers a et b qui doivent tre tris dans l'ordre croissant. Le programme devra tout d'abord vrifier que les deux tableaux sont tris. Le tableau c est un tableau de 20 entiers. Le programme doit mettre dans c la fusion des tableaux a et b. Le tableau c devra contenir les lments de a et ceux de b et devra tre tri. Le programme affiche ensuite le tableau c.

Solution
# i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n ta [ N ] , b [ N ] , c [ 2 * N ] , i , t r i e , i n d i c e a , i n d i c e b ; c o u t < < " S A I S I ED UT A B L E A Ua " < < e n d l ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > a [ i ] ; } c o u t < < " S A I S I ED UT A B L E A Ub " < < e n d l ; f o r ( i = 0 ; i < N ; i + + ) { c o u t < < " T a p e zu ne n t i e r" ; c i n > > b [ i ] ; } t r i e = t r u e ; i = 0 ;
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 7/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

w h i l e ( t r i e& &i < N 1 ) i f ( a [ i ] > a [ i + 1 ] ) t r i e = f a l s e ;e l s ei + + ; i f ( ! t r i e ) c o u t < < " L et a b l e a uan ' e s tp a st r i " < < e n d l ; e l s e { t r i e = t r u e ; i = 0 ; w h i l e ( t r i e& &i < N 1 ) i f ( b [ i ] > b [ i + 1 ] ) t r i e = f a l s e ;e l s ei + + ; i f ( ! t r i e ) c o u t < < " L et a b l e a ubn ' e s tp a st r i " < < e n d l ; e l s e { i n d i c e a = 0 ; i n d i c e b = 0 ; f o r ( i = 0 ; i < 2 * N ; i + + ) { i f ( i n d i c e a = = N ) { c [ i ] = b [ i n d i c e b ] ; i n d i c e b + + ; } e l s ei f ( i n d i c e b = = N ) { c [ i ] = a [ i n d i c e a ] ; i n d i c e a + + ; } e l s ei f ( a [ i n d i c e a ] < b [ i n d i c e b ] ) { c [ i ] = a [ i n d i c e a ] ; i n d i c e a + + ; } e l s e{ c [ i ] = b [ i n d i c e b ] ; i n d i c e b + + ; } } } } c o u t < < " V O I C IL ET A B L E A Uc " < < e n d l ; f o r ( i = 0 ; i < 2 * N ; i + + ) c o u t < < c [ i ] < < "" ; c o u t < < e n d l ; r e t u r n0 ; }

EXERCICE 11
Ecrire un programme qui gre une liste d'entiers grce au menu suivant : 1. Ajouter un entier 2. Afficher la liste des entiers 3. Supprimer dernier entier de la liste. 4. Afficher la dernire note tape 5. Quitter Il y aura au maximum 10 entiers. Lorsqu'on rajoute un entier, il sera rajout la fin de la liste.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Festion d'une liste simple grce un tableau statique. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( )
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 8/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

{ i n tt [ N ] , n b = 0 , c h o i x , e , i ; b o o lf i n i = f a l s e ; w h i l e ( f i n i = = f a l s e ) { c o u t < < " 1 .A j o u t e ru ne n t i e r " < < e n d l ; c o u t < < " 2 .A f f i c h e rl al i s t ed e se n t i e r " < < e n d l ; c o u t < < " 3 .S u p p r i m e rl ed e r n i re n t i e rd el al i s t e " < < e n d l ; c o u t < < " 4 .A f f i c h e rl ed e r n i e re n t i e rd el al i s t e " < < e n d l ; c o u t < < " 5 .Q u i t t e r " < < e n d l ; c o u t < < " V o t r ec h o i x:" ; c i n > > c h o i x ; s w i t c h ( c h o i x ) { c a s e1:c o u t < < " T a p e zu ne n t i e r:" ; c i n > > e ; i f ( n b < N ) { t [ n b ] = e ;n b + + ;c o u t < < " E N T I E RA J O U T E " < < e n d l ; } e l s ec o u t < < " I M P O S S I B L EL ET A B L E A UE S TP L E I N " < < e n d l ; b r e a k ; c a s e2:i f ( n b = = 0 ) c o u t < < " L AL I S T EE S TV I D E " < < e n d l ; e l s e{ c o u t < < " V O I C IL AL I S T E " < < e n d l ; f o r ( i = 0 ; i < n b ; i + + ) c o u t < < t [ i ] < < "" ; c o u t < < e n d l ; } b r e a k ; c a s e3:i f ( n b > 0 ) { n b ;c o u t < < " E N T I E RS U P P R I M E " < < e n d l ; } e l s ec o u t < < " L AL I S T EE S TV I D E " < < e n d l ; b r e a k ; c a s e4:i f ( n b > 0 ) c o u t < < " l L ED E R N I E RE N T I E RE S T" < < t [ n b 1 ] < < e n d l ; e l s ec o u t < < " L AL I S T EE S TV I D E " < < e n d l ; b r e a k ; c a s e5:f i n i = t r u e ; b r e a k ; } } r e t u r n0 ; }

EXERCICE 12
Ecrire un programme qui gre une liste d'entiers grce au menu suivant : 1. Ajouter un entier 2. Afficher la liste des entiers 3. Supprimer le premier entier ayant une valeur donne. 4. Supprimer tous les entiers ayant une valeur donne 5. Quitter Il y aura au maximum 10 entiers. La liste devra tre en permanence trie : lorqu'on rajoute un entier, il sera insr au bon endroit dans la liste pour que celle-ci reste trie.
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=1 9/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Gestion d'une liste trie grce un tableau statique. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 1 0 ; i n tm a i n ( ) { i n tt [ N ] , n b = 0 , c h o i x , e , V , i , j , t r o u v e ; b o o lf i n i = f a l s e ; w h i l e ( f i n i = = f a l s e ) { c o u t < < " 1 .A j o u t e ru ne n t i e r " < < e n d l ; c o u t < < " 2 .A f f i c h e rl al i s t ed e se n t i e r " < < e n d l ; c o u t < < " 3 .S u p p r i m e rl ep r e m i e re n t i e ra y a n tu n ev a l e u rd o n n e " < < e n d l ; c o u t < < " 4 .S u p p r i m e rt o u sl e se n t i e r sa y a n tu n ev a l e u rd o n n e " < < e n d l ; c o u t < < " 5 .Q u i t t e r " < < e n d l ; c o u t < < " V o t r ec h o i x:" ; c i n > > c h o i x ; s w i t c h ( c h o i x ) { c a s e1:i f ( n b < N ) { c o u t < < " T a p e zu ne n t i e r:" ; c i n > > e ; i = 0 ; w h i l e ( i ! = n b& &t [ i ] < e ) i + + ; f o r ( j = n b ; j > i ; j ) t [ j ] = t [ j 1 ] ; t [ i ] = e ; n b + + ; } e l s ec o u t < < " I M P O S S I B L EL ET A B L E A UE S TP L E I N " < < e n d l ; b r e a k ; c a s e2:i f ( n b = = 0 ) c o u t < < " L AL I S T EE S TV I D E " < < e n d l ; e l s e{ c o u t < < " V O I C IL AL I S T E " < < e n d l ; f o r ( i = 0 ; i < n b ; i + + ) c o u t < < t [ i ] < < "" ; c o u t < < e n d l ; } b r e a k ; c a s e3:c o u t < < " T a p e zl av a l e u rs u p p r i m e r: " ; c i n > > V ; t r o u v e = f a l s e ; i = 0 ; w h i l e ( ! t r o u v e& &i < n b ) i f ( t [ i ] = = V ) t r o u v e = t r u e ;e l s ei + + ; i f ( t r o u v e )
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print= 10/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

{ f o r ( j = i ; j < n b 1 ; j + + ) t [ j ] = t [ j + 1 ] ; n b ; } b r e a k ; c a s e4:c o u t < < " T a p e zl av a l e u rs u p p r i m e r: " ; c i n > > V ; j = 0 ; f o r ( i = 0 ; i < n b ; i + + ) i f ( t [ i ] ! = V ) { t [ j ] = t [ i ] ; j + + ; } n b = j ; b r e a k ; c a s e5:f i n i = t r u e ; b r e a k ; } } r e t u r n0 ; }

EXERCICE 13
Ecrire un programme qui demande l'utilisateur de taper un entier N<=20 et qui affiche la N-ime ligne du triangle de pascal. ligne 1 : 1 1 ligne 2 : 1 2 1 ligne 3 : 1 3 3 1 ligne 4 : 1 4 6 4 1 et ainsi de suite ...

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation simple de tableaux. Gestion d'une liste trie grce un tableau statique. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; i n tm a i n ( ) { i n ta [ 2 1 ] , i , j , N ; c o u t < < " V e u i l l e zt a p e rN:" ; c i n > > N ; f o r ( i = 1 ; i < = N ; i + + ) { i f ( i = = 1 ) a [ 0 ] = 1 ; a [ i ] = 1 ; f o r ( j = i 1 ; j > = 1 ; j ) a [ j ] = a [ j ] + a [ j 1 ] ; } f o r ( i = 0 ; i < = N ; i + + ) c o u t < < a [ i ] < < "" ; c o u t < < e n d l ;
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print= 11/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

r e t u r n0 ; }

EXERCICE 14
Ecrire un programme qui demande l'utilisateur de taper 10 entiers compris entre 0 et 20 qui seront stocks dans un tableau et qui affiche le nombre de fois qu'on a tap un 0, le nombre de 1, le nombre de 2, ..., le nombre de 20.

Solution
# i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; i n tm a i n ( ) { i n ta [ 1 0 ] , n b [ 2 1 ] , i ; f o r ( i = 0 ; i < 1 0 ; i + + ) { d o{ c o u t < < " V e u i l l e zt a p e rl ' e n t i e rn u m e r o" < < i < < ":" ; c i n > > a [ i ] ; } w h i l e( a [ i ] > 2 0| |a [ i ] < 0 ) ; } f o r ( i = 0 ; i < 2 1 ; i + + ) n b [ i ] = 0 ; f o r ( i = 0 ; i < 1 0 ; i + + ) n b [ a [ i ] ] + + ; f o r ( i = 0 ; i < 2 1 ; i + + ) { c o u t < < " I lya" < < n b [ i ] < < "f o i sl ' e n t i e r" < < i < < e n d l ; } r e t u r n0 ; }

EXERCICE 15
Ecrire un programme qui demande l'utilisateur de taper le contenu d'un tableau de rels de 3 lignes et 3 colonnes et qui affiche ce tableau mais en affichant la moyenne des lments de chaque ligne, de chaque colonne et la moyenne globale.

Solution
Cet exercice a pour but de vrifier les points techniques suivants : Utilisation de tableaux 2 dimensions. Modlisation d'un problme mathmatique basique. Voici le fichier source : # i n c l u d e < i o s t r e a m > u s i n gn a m e s p a c es t d ; c o n s ti n tN = 3 ; c o n s ti n tM = 3 ; i n tm a i n ( ) { d o u b l et [ N ] [ M ] , m o y L [ N ] , m o y C [ M ] , m o y ; i n ti , j ; f o r ( i = 0 ; i < N ; i + + )
univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print= 12/13

09/08/13

Exercices corrigs en C++ (Les tableaux)

f o r ( j = 0 ; j < M ; j + + ) { c o u t < < " T a p e zl av a l e u rd el al i g n e" < < i < < "c o l o n n e" < < j < < ":" ; c i n > > t [ i ] [ j ] ; } f o r ( i = 0 ; i < N ; i + + ) m o y L [ i ] = 0 ; f o r ( j = 0 ; j < M ; j + + ) m o y C [ j ] = 0 ; m o y = 0 ; f o r ( i = 0 ; i < N ; i + + ) f o r ( j = 0 ; j < M ; j + + ) { m o y L [ i ] = m o y L [ i ] + t [ i ] [ j ] ; m o y C [ j ] = m o y C [ j ] + t [ i ] [ j ] ; m o y = m o y + t [ i ] [ j ] ; } f o r ( i = 0 ; i < N ; i + + ) m o y L [ i ] = m o y L [ i ] / N ; f o r ( j = 0 ; j < M ; j + + ) m o y C [ j ] = m o y C [ j ] / M ; m o y = m o y / ( N * M ) ; f o r ( i = 0 ; i < N ; i + + ) { f o r ( j = 0 ; j < M ; j + + ) c o u t < < t [ i ] [ j ] < < "" ; c o u t < < m o y L [ i ] < < e n d l ; } f o r ( j = 0 ; j < M ; j + + ) c o u t < < m o y C [ j ] < < "" ; c o u t < < e n d l ; r e t u r n0 ; }

univ-examens.freehostia.com/index.php?view=article&catid=74%3Aexercice&id=141%3Aexercices-corriges-en-c-les-tableaux-&tmpl=component&print=

13/13

You might also like