Advertisement
yanni_yagami

matrice puissance

Jan 10th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void afficher_matrice(int matrice[][], int dimention)
  5. {
  6.     for(int i = 0; i < dimention; i++)
  7.     {
  8.         for(int j = 0; j < dimention; j++)
  9.         {
  10.             printf("%03d\t", matrice[i][j]);
  11.         }
  12.         printf("\n");
  13.     }
  14. }
  15.  
  16. /*-------------------------------------------------------*/
  17.  
  18. void remplir_matrice(int matrice[][], int dimention)
  19. {
  20.     printf("veillez remplir votre matrice : \n");
  21.  
  22.     for(int i = 0; i < dimention; i++)
  23.     {
  24.         printf("\nLigne %d : \n", i+1);
  25.  
  26.         for(int j = 0; j < dimention; j++)
  27.         {
  28.             printf("Colonne %d : ", j+1);
  29.             scanf("%d", &matrice[i][j]);
  30.         }
  31.     }
  32. }
  33.  
  34. /*-------------------------------------------------------*/
  35.  
  36. int calcul_element(int matrice_a[][], int matrice_b[][], int dimention, int ligne, int colonne)
  37. {
  38.     int somme = 0;
  39.     for(int i = 0; i < dimention; i++)
  40.     {
  41.         somme += matrice_a[ligne][i] * matrice_b[i][colonne];
  42.     }
  43.  
  44.     return somme;
  45. }
  46.  
  47. /*-------------------------------------------------------*/
  48.  
  49. void prod(int **matrice_a, int matrice_b[][], int resultat[][], int dimention)
  50. {
  51.     for(int i = 0; i < dimention; i++)
  52.     {
  53.         for(int j = 0; j < dimention; j++)
  54.         {
  55.             resultat[i][j] = calcul_element(matrice_a, matrice_b, dimention, i, j);
  56.         }
  57.     }
  58. }
  59.  
  60. /*-------------------------------------------------------*/
  61.  
  62. void matrice_puissance(int matrice[][], int resultat[][], int dimention, int puissance)
  63. {
  64.     int matrice_b[dimention][dimention];
  65.  
  66.     if(puissance == 1)
  67.     {
  68.         affecter_matrice(resultat, matrice, dimention);
  69.     }
  70.  
  71.     else if(puissance == 2)
  72.     {
  73.         prod(matrice, matrice, resultat, dimention);
  74.     }
  75.  
  76.     else
  77.     {
  78.         prod(matrice, matrice, matrice_b, dimention);
  79.  
  80.         for(int i = 2; i < puissance; i++)
  81.         {
  82.             prod(matrice, matrice_b, resultat, dimention);
  83.             affecter_matrice(matrice_b, resultat, dimention);
  84.         }
  85.     }
  86. }
  87.  
  88. /*-------------------------------------------------------*/
  89.  
  90. void affecter_matrice(int matrice_a[][], int matrice_b[][], int dimention)
  91. {
  92.     for(int i = 0; i < dimention; i++)
  93.     {
  94.         for(int j = 0; j < dimention; j++)
  95.         {
  96.             matrice_a[i][j] = matrice_b[i][j];
  97.         }
  98.     }
  99. }
  100.  
  101. int main(void)
  102. {
  103.     int dimention, puissance;
  104.  
  105.     printf("veillez entrer la dimention de la matrice (n%cn) : ", 215);
  106.     scanf("%d", &dimention);
  107.  
  108.     int matrice[dimention][dimention], resultat[dimention][dimention];
  109.  
  110.     remplir_matrice(matrice, dimention);
  111.  
  112.     printf("veillez entrer votre puissance : ");
  113.     scanf("%d", &puissance);
  114.  
  115.     matrice_puissance(matrice, resultat, dimention, puissance);
  116.  
  117.     system("cls"); //clean the screen - nettoyer l'Γ©cran
  118.     afficher_matrice(resultat, dimention);
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement