Advertisement
Chom

MatriceTraspostaNxN

Dec 30th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define nMAX 12
  5.  
  6. typedef struct {
  7.     int valori[nMAX][nMAX];
  8.     int righe;
  9.     int colonne;
  10. } Matr;
  11.  
  12. Matr m1;
  13.  
  14. void trasposta(Matr m1);
  15.  
  16. void swap(int i, int j);
  17.  
  18. void stampa(Matr m1);
  19.  
  20. int main() {
  21.     int i;
  22.     int j;
  23.     int a;
  24.     int b;
  25.     do {
  26.         printf("Inserire la dimensione della matrice (MAX 12): ");
  27.         scanf("%d", &b);
  28.     } while (b > 12 && b < 1);
  29.  
  30.     m1.righe = b;
  31.     m1.colonne = b;
  32.  
  33.     i = 0;
  34.     while (i < m1.righe) {
  35.         j = 0;
  36.  
  37.         while (j < m1.colonne) {
  38.             printf("Inserire il valore: ");
  39.             scanf("%d", &a);
  40.             m1.valori[i][j] = a;
  41.             j = j + 1;
  42.         }
  43.  
  44.         i = i + 1;
  45.     }
  46.     stampa(m1);
  47.     trasposta(m1);
  48.     stampa(m1);
  49.     return 0;
  50. }
  51.  
  52. void swap(int i, int j) {
  53.     int tmp = m1.valori[i][j];
  54.     m1.valori[i][j] = m1.valori[j][i];
  55.     m1.valori[j][i] = tmp;
  56. }
  57.  
  58. void trasposta(Matr m1) {
  59.     int i;
  60.     int j;
  61.     for (i = 0; i < m1.righe; i++)
  62.         for (j = i; j < m1.colonne; j++)
  63.             swap(i, j);
  64. }
  65.  
  66. void stampa(Matr m1) {
  67.     int i = 0;
  68.     int j = 0;
  69.     int a = 0;
  70.  
  71.     printf("La matrice contiene i seguenti valori: \n");
  72.  
  73.     while (i < m1.righe) {
  74.         j = 0;
  75.  
  76.         while (j < m1.colonne) {
  77.  
  78.             a = m1.valori[i][j];
  79.             printf("%d", a);
  80.             printf(" ");
  81.  
  82.             j = j + 1;
  83.         }
  84.  
  85.         printf("\n");
  86.         i = i + 1;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement