_mumu_ | Tiens pour l'addition des matrices. Inspires toi de ce code pour faire les deux autres.
Code :
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int **create_mat(int, int);
- void print_sum(int **, int **, int, int);
- void desalloc(int **, int);
- int main(int argc, char **argv){
- int **mat1, **mat2;
-
- if (argc != 3){
- printf("Usage : %s n p\n", argv[0]);
- return -1;
- }
- mat1 = create_mat(atoi(argv[1]), atoi(argv[2]));
- mat2 = create_mat(atoi(argv[1]), atoi(argv[2]));
- print_sum(mat1, mat2, atoi(argv[1]), atoi(argv[2]));
- desalloc(mat1, atoi(argv[1]));
- desalloc(mat2, atoi(argv[1]));
- return 0;
- }
- int **create_mat(int n, int p){
- int **matrice;
- int i, j;
-
- matrice = (int **) malloc(n * sizeof(int *));
- for (i=0; i<n; i++)
- matrice[i] = (int *) malloc(p * sizeof(int));
- srand(time(NULL));
- for (i=0; i<n; i++){
- for (j=0; j<p; j++)
- matrice[i][j] = rand()%101;
- }
- return matrice;
- }
- void print_sum(int **mat1, int **mat2, int n, int p){
- int i,j;
- for (i=0; i<n; i++){
- for (j=0; j<p; j++)
- printf("%d ", mat1[i][j]+mat2[i][j]);
- printf("\n" );
- }
- }
- void desalloc(int **mat, int n){
- int i;
- for (i=0; i<n; i++)
- free(mat[i]);
- free(mat);
- }
|
PS : je n'ai pas commenté donc si tu as des questions sur le code, n'hésite pas. Message édité par _mumu_ le 01-03-2008 à 22:00:03 ---------------
"Software is like sex. It's better when it's free..." Linus Torvalds
|