Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Exercice 1 : AFFICHAGE D’UN TABLEAU LIGNE PAR LIGNE
- Déclarer un tableau
tcarré d'ordre N - tirer au hasard 2 nombres entiers
metn, tous deux entre 2 et N. Il s'agira des nombres de lignes et de colonnes utiles du tableaut. - Initialiser
tdans lemain()avec des valeurs aléatoires comprises entre 1 et 100, par indices. - Afficher ces éléments ligne par ligne, via une fonction qui reçoit
ten paramètre.
Spoiler : une solution type !
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define N 10
// -> N = nombre de lignes et de colonnes du tableau. Il s'agit donc d'un tableau "carré" d'ordre N.
#define RANDMAX 100
void afficheTableau(short t[][N], short nl, short nc);
int main()
{
short t[O][O] = { 0 };
short m, n, i, j;
srand(time(NULL));
// nombres de lignes et de colonnes tirées au hasard
m = rand() % (O-1) + 2;
n = rand() % (O-1) + 2;
// Parcours classique, lignes x colonnes, par indices :
for (i = 0; i < m; i++) { // pour chaque ligne :
for (j = 0; j < n; j++) {
t[i][j] = rand() % RANDMAX + 1;
}
}
printf("\n\n\nLe tableau :\n");
afficheTableau(t, m, n);
return 0;
}
void afficheTableau(short t[][O], short nl, short nc)
{
short i, j;
for (i = 0; i < nl; i++) {
for (j = 0; j < nc; j++) {
printf("%3.hd ", t[i][j]);
}
printf("\n");
}
return;
}
Press desired key combination and then press ENTER.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content