-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseries-c-08.tex
151 lines (126 loc) · 4.09 KB
/
series-c-08.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
\documentclass[french,a4paper,addpoints,11pt]{exam}
\usepackage{hexercises}
\usepackage{mathtools, nccmath}
\title{Programmation et algorithmique}
\seriesno{\texttt{0x08}}
\department{TIN}
\classroom{INFO1-TIN}
\setlength\answerlinelength{10 cm}
\setlength\answerskip{3ex}
\setlength\answerclearance{1.1ex}
\CorrectChoiceEmphasis{}
\renewcommand{\thepartno}{\alph{partno}}
\renewcommand{\partlabel}{\thepartno.}
\renewcommand{\arraystretch}{1.75} % expand the cells vertically
\begin{document}
\maketitle
\thispagestyle{headandfoot}
Pour vous préparer aux examens écrits, on vous propose de réaliser les exercices suivants sur papier avec un crayon et une gomme. Vous pouvez utiliser des feuilles de brouillon.
\begin{questions}
\question Écrire une fonction qui reçoit en paramètre une chaîne de caractères et qui transforme les minuscules en majuscules dans cette chaîne. Ne pas utiliser la fonction standard \CD{toupper}.
\begin{solutionordottedlines}[6cm]
\begin{lstlisting}
void to_upper(char *str) {
int i = 0;
while(str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 'a' - 'A';
i++;
}
}
\end{lstlisting}
\end{solutionordottedlines}
\question Sans utiliser \CD{strlen}, écrire une fonction qui calcule la longueur d'une chaîne de caractère.
\begin{solutionordottedlines}[5cm]
\begin{lstlisting}
size_t strlen(char *str) {
size_t size = 0;
while(str[++size] != '\0') {}
return size;
}
\end{lstlisting}
\end{solutionordottedlines}
\question Écrire une fonction qui retourne une valeur aléatoire entière entre a et b, vous pouvez utiliser la fonction \CD{rand()}
\begin{solutionordottedlines}[3cm]
\begin{lstlisting}
int rand_range(int a, int b) {
return rand() % (b - a + 1) + a;
}
\end{lstlisting}
\end{solutionordottedlines}
\question Écrire une fonction qui échange deux entiers passés par référence.
\begin{solutionordottedlines}[7cm]
\begin{lstlisting}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
\end{lstlisting}
\end{solutionordottedlines}
\question Écrire une fonction qui reçois trois valeurs réelle en paramètres et qui les trie dans l'ordre croissant. Par exemple si vous avez $a=15, b=23, c=4$ après l'appel de fonction, ces valeurs vaudront : $a=4, b=15, c=23$. Vous pouvez utiliser la fonction \CD{swap} écrite plus haut pour échanger les valeurs.
\begin{solutionordottedlines}[7cm]
\begin{lstlisting}
void sort_3(double *a, double *b, double *c)
{
if (*a > *b) swap(a, b);
if (*a > *c) swap(a, c);
if (*b > *c) swap(b, c);
}
\end{lstlisting}
\end{solutionordottedlines}
\question Écrire une fonction qui retourne la moyenne de trois valeurs réelles reçues en paramètres.
\begin{solutionordottedlines}[6cm]
\begin{lstlisting}
double mean_3(double a, double b, double c)
{
return (a + b + c) / 3;
}
\end{lstlisting}
\end{solutionordottedlines}
\question Écrire une fonction qui affiche \CD{version} sur la sortie standard si un argument \CD{--version} est présent dans les arguments du programme. Cette fonction aura le prototype suivant, et la fonction main est donnée
\ifprintanswers
\begin{solution}
\begin{lstlisting}
void parse_arguments(int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
if (strcmp(argv[i], "--version") == 0)
printf("Version\n");
}
int main(int argc, char *argv[]) {
parse_arguments(argc, argv);
}
\end{lstlisting}
\end{solution}
\else
\begin{lstlisting}
void parse_arguments(int argc, char *argv[]);
int main(int argc, char *argv[]) {
parse_arguments(argc, argv);
}
\end{lstlisting}
\fillwithdottedlines{8cm}
\fi
\question Écrire une fonction qui retourne la valeur minimale d'un tableau de réels. Le prototype est le suivant :
\ifprintanswers
\begin{solution}
\begin{lstlisting}
double min(double array[], size_t size) {
double min = array[0];
for (int i = 1; i < size; i++)
if (array[i] < min)
min = array[i];
return min;
}
\end{lstlisting}
\end{solution}
\else
\begin{lstlisting}
double min(double array[], size_t size);
\end{lstlisting}
\fillwithdottedlines{8cm}
\fi
\end{questions}
\end{document}