prog_avancee_C/TP1/part_2.c

149 lines
3.3 KiB
C
Raw Permalink Normal View History

2024-10-22 13:03:17 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NB_LIGNES 9
#define NB_COLONNES 3
float ** tableau(int n, int m);
void autofill_tableau(float** tab, int n, int m);
void autofill_tableau_int(float** tab, int n, int m, int v);
void fill_tableau(float** tab, int n, int m);
void print_tableau(float** tab, int n, int m);
void print_tableau_int(float** tab, int n, int m);
float ** right_rot(float** tab, int n, int m);
/*
* Libère le tableau
* n: nombre de lignes du tableau
*/
void free_tab(float** tab, int n);
float ** triangle_pascal(int size);
#define PASCAL 5
int main(int argc, char *argv[]) {
srand(time(NULL));
float ** tab = tableau(NB_LIGNES, NB_COLONNES);
autofill_tableau(tab, NB_LIGNES, NB_COLONNES);
//fill_tableau(tab, NB_LIGNES, NB_COLONNES);
print_tableau(tab, NB_LIGNES, NB_COLONNES);
printf("\n\n");
float ** rot_tab = right_rot(tab, NB_LIGNES, NB_COLONNES);
print_tableau(rot_tab, NB_COLONNES, NB_LIGNES);
free_tab(tab, NB_LIGNES);
free_tab(rot_tab, NB_COLONNES);
/*float ** t = triangle_pascal(PASCAL);
print_tableau_int(t, PASCAL, PASCAL);*/
return 0;
}
float ** triangle_pascal(int size) {
int i,j;
float ** tr = tableau(size, size);
autofill_tableau_int(tr, size, size, 0);
tr[0][0] = 1;
for (i=1; i<size; i++) {
for (j=0; j<size; j++) {
tr[i][j] = tr[i-1][j-1]+tr[i-1][j];
}
}
return tr;
}
void free_tab(float** tab, int n) {
int i;
for (i=0; i<n; i++) {
free(tab[i]);
}
free(tab);
}
float ** right_rot(float** tab, int n, int m) {
int lgn, col;
float ** rot_tab = tableau(m, n);
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
rot_tab[col][(m-lgn)] = tab[lgn][col];
}
}
return rot_tab;
}
float ** tableau(int n, int m) {
int i;
int success=1;
void* ptr;
float ** tab = malloc(sizeof(float*)*n);
for (i=0; i<n; i++) {
ptr = malloc(sizeof(float)*m);
if (ptr == 0) {
success=0;
break;
}
tab[i] = ptr;
}
if(success!=0)
return tab;
else
return NULL;
}
#define RAND_RANGE 69
void autofill_tableau(float** tab, int n, int m) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
tab[lgn][col] = (float)rand()/(float)(RAND_MAX/RAND_RANGE);
}
}
}
void autofill_tableau_int(float** tab, int n, int m, int v) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
tab[lgn][col] = v;
}
}
}
void fill_tableau(float** tab, int n, int m) {
int lgn, col;
float tmp;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
printf("Ligne %d / Colonne %d: ", lgn, col);
scanf("%f", &tmp);
tab[lgn][col] = tmp;
}
}
}
void print_tableau(float** tab, int n, int m) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
printf(" %f ", tab[lgn][col]);
}
printf("\n");
}
}
void print_tableau_int(float** tab, int n, int m) {
int lgn, col;
for (lgn=0; lgn<n; lgn++) {
for (col=0; col<m; col++) {
printf(" %d ", (int)tab[lgn][col]);
}
printf("\n");
}
}