0% found this document useful (0 votes)
30 views3 pages

TD2 (Corrigé)

The document contains examples of C code to: 1) Define and initialize an array and print its values. 2) Demonstrate operations on pointers and reference variables including initialization, assignment, dereferencing, and arithmetic. 3) Define functions to read values into an array, print array values using indexing and pointers, find the maximum value and index, calculate statistics, and check if a value exists in an array.

Uploaded by

Soufiane Hamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

TD2 (Corrigé)

The document contains examples of C code to: 1) Define and initialize an array and print its values. 2) Demonstrate operations on pointers and reference variables including initialization, assignment, dereferencing, and arithmetic. 3) Define functions to read values into an array, print array values using indexing and pointers, find the maximum value and index, calculate statistics, and check if a value exists in an array.

Uploaded by

Soufiane Hamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Corrigé TD No 2

Ex.1 : 1.
1 0 5 10 15 20 25 30 35 40 45

2.
1 # include < stdio .h >
2 # define N 10
3 main () {
4 int suite [ N ] , i ;
5 suite [0]=0;
6 for ( i =1; i < N ; i ++) {
7 suite [ i ]= suite [i -1]+5;
8 }
9
10 for ( i =0 ; i < N ; i ++)
11 printf ( " % d " , suite [ i ]) ;
12 }

Ex.2 :
a b c p q
initialisation 1 2 3 - -
p = &a 1 2 3 &a -
q = &c 1 2 3 &a &c
*p = *q 3 2 3 &a &c
(*q)++ 3 2 4 &a &c
p=q 3 2 4 &c &c
q + &b 3 2 4 &c &b
*p -= *q 3 2 2 &c &b
++(*q) 3 3 2 &c &b
*p *= *q 3 3 6 &c &b
++(*q) 3 4 6 &c &b
a = *q * *p 24 4 6 &a &b
p = &a 24 4 6 &a &b
*q = *p /= *q 6 6 6 &a &b

Ex.3 :
1 # include < stdio .h >
2 # define N 5
3 void lire ( float * notes , int n ) {
4 int i ;
5 for ( i =0 ; i < n ; i ++) {
6 do {
7 printf ( " Notes [% d ]= " ,i ) ;
8 scanf ( " % f " ,& notes [ i ]) ;
9 } while ( notes [ i ] <0 || notes [ i ] >20) ;
10 }
11 }

1
12
13 /* Formalisme tableau */
14 void afficherT ( float * notes , int n ) {
15 int i ;
16 for ( i =0 ; i < n ; i ++)
17 printf ( " %.2 f " , notes [ i ]) ;
18 }
19
20 /* Formalisme pointeur */
21 void afficherP ( float * notes , int n ) {
22 float * p ;
23 for ( p = notes ; p < notes + n ; p ++)
24 printf ( " %.2 f " , * p ) ;
25 }
26
27 float maximum ( float * notes , int n ) {
28 int i ;
29 float m ;
30 m = notes [0];
31 for ( i =1; i < n ; i ++)
32 if ( notes [ i ] > m ) m = notes [ i ];
33 return m ;
34 }
35
36 int indice_max ( float * notes , int n ) {
37 int i , k ;
38 k =0;
39 for ( i =1 ; i < n ; i ++)
40 if ( notes [ i ] > notes [ k ]) k = i ;
41 return k ;
42 }
43
44 int valide ( float * notes , int n ) {
45 int s =0;
46 float * p ;
47 for ( p = notes ; p < notes + n ; p ++)
48 if (* p >10) s ++;
49 return s ;
50 }
51
52 float moyenne ( float * notes , int n ) {
53 int i ;
54 float s =0;
55 for ( i =0; i < n ; i ++)
56 s += notes [ i ];
57 return s / n ;
58 }
59
60 int existe ( float * notes , float x , int n ) {
61 int i ;
62 for ( i =0; i < n ; i ++)
63 if ( notes [ i ]== x ) return 1;
64 return 0;
65 }
66
67 main () {
68 float notes [ N ] , x ;
69

2
70 lire ( notes , N ) ;
71
72 afficherT ( notes , N ) ;
73 printf ( " \ n " ) ;
74
75 afficherP ( notes , N ) ;
76 printf ( " \ n " ) ;
77
78 printf ( " Maximum =% f " , maximum ( notes , N ) ) ;
79 printf ( " \ n " ) ;
80
81 printf ( " Indice max =% d " , indice_max ( notes , N ) ) ;
82 printf ( " \ n " ) ;
83
84 printf ( " Moyenne =% f " , moyenne ( notes , N ) ) ;
85 printf ( " \ n " ) ;
86
87 printf ( " Entrer x : " ) ;
88 scanf ( " % f " ,& x ) ;
89
90 if ( existe ( notes , x , N ) )
91 printf ( " %.2 f existe " ,x ) ;
92 else
93 printf ( " %.2 f n ’ existe pas " ,x ) ;
94 }

You might also like