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
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "EXO4.h"
int main(int argc, char **argv)
{
int n, a, b, i, j;
int nbRows, nbCols;
printf("Taille du tableau: ");
scanf("%d", &n);
printf("Génération de nombre aléatoire entre a et b:");
printf("\na: ");
scanf("%d", &a);
printf("\nb: ");
scanf("%d", &b);
/* Allocation du tableau statique */
int *tab=AllocTab(n);
/* Remplissage du tableau */
RandTab(tab, n, a, b);
/* Libération du tableau */
FreeTab(tab);
/* Affichage du tableau */
for(i=0; i<n; i++)
{
printf("\n%d", tab[i]);
}
printf("\nNombre de lignes et colonnes de la matrice:");
printf("\nnbRows: ");
scanf("%d", &nbRows);
printf("\nnbCols: ");
scanf("%d", &nbCols);
/* Allocation de matrices */
int **mat=AllocMat(nbRows, nbCols);
/* Remplissage de la matrice */
RandMat(mat, nbRows, nbCols, a, b);
/* Affichage matrice */
for (i=0; i<nbRows; i++)
{
for(j=0; j<nbCols; j++)
{
printf(" %d ", mat[i][j]);
if(j==nbCols)
{
printf("\n");
}
}
}
/* Libération de la matrice */
FreeMat(mat, nbRows);
return 0;
}
/* Pour les tableaux */
int* AllocTab( int n )
{
int *tab=(int*)calloc(n, sizeof(int));
return tab;
}
void RandTab( int *tab, int n, int a, int b )
{
srand(time(NULL));
int i;
for(i=0; i<n; i++)
{
tab[i]=(a + rand()%(b-a));
}
}
void FreeTab( int *tab )
{
free(tab);
}
/* Pour les matrices */
int** AllocMat( int nbRows, int nbCols )
{
int i;
int **mat=(int**)calloc(nbRows, sizeof(int));
for(i=0; i<nbRows; i++)
{
mat[i]=(int*)calloc(nbCols, sizeof(int));
}
return mat;
}
void RandMat( int **mat, int nbRows, int nbCols, int a, int b )
{
int i, j;
for(i=0; i<nbRows; i++)
{
for(j=0; j<nbCols; j++)
{
mat[i][j]=(a + rand()%(b-a));
}
}
}
void FreeMat( int **mat, int nbRows )
{
printf(" ");
int i;
for(i=0; i<nbRows; i++)
{
free(mat[i]);
}
free(mat);
} |
Partager