peterzig

macierztranspozycja

Dec 13th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <float.h>
  5.  
  6.  
  7. float **stworzTablice(int n, int m) {
  8.     float **tab = (float **)malloc(sizeof(float *) * n);
  9.     for (int i = 0; i < n; ++i) {
  10.         tab[i] = (float *)malloc(sizeof(float) * m);
  11.     }
  12.     return tab;
  13. }
  14.  
  15. void zwolnijPamiec(float **tab, int n) {
  16.     for (int i = 0; i < n; ++i) {
  17.         free(tab[i]);
  18.     }
  19.     free(tab);
  20. }
  21.  
  22. void wyswietl(float **tab, int n, int m) {
  23.     for (int i = 0; i < n; ++i) {
  24.         printf("");
  25.         for (int j = 0; j < m; ++j) {
  26.             printf("%.2f ", tab[i][j]);
  27.         }
  28.         printf("\n");
  29.     }
  30. }
  31. void transpozycja(float **tab, int n, int m) {
  32.     for (int i = 0; i < m; ++i) {
  33.         printf("");
  34.         for (int j = 0; j < n; ++j) {
  35.             printf("%.2f ", tab[j][i]);
  36.         }
  37.         printf("\n");
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. int main() {
  44.     int n, m;
  45.     float min = FLT_MAX, max = FLT_MIN;
  46.     int minIndeks = 0, maxIndeks = 0; //indeks pierwszej tablicy (i)
  47.     int minIndeks2 = 0, maxIndeks2 = 0; //potrzebujemy zapisac gdzies jeszcze drugi indeks tablicy (j)
  48.  
  49.     do {
  50.         printf("Podaj liczbe wierszy: ");
  51.         scanf("%d", &n);
  52.     } while (n<1);
  53.  
  54.     do {
  55.         printf("Podaj liczbe kolumn: ");
  56.         scanf("%d", &m);
  57.     } while (m<1);
  58.  
  59.     float **tab2 = stworzTablice(n, m);
  60.  
  61.     for (int i = 0; i < n; ++i) {
  62.         for (int j = 0; j < m; ++j) {
  63.             printf("T[%d, %d] = ", i + 1, j + 1);
  64.             scanf("%f", &tab2[i][j]);
  65.  
  66.             if (tab2[i][j] < min) {
  67.                 min = tab2[i][j];
  68.                 minIndeks = i;
  69.                 minIndeks2 = j;
  70.             }
  71.             if (tab2[i][j] > max) {
  72.                 max = tab2[i][j];
  73.                 maxIndeks = i;
  74.                 maxIndeks2 = j;
  75.             }
  76.         }
  77.     }
  78.  
  79.     wyswietl(tab2, n, m);
  80.     printf("\n");
  81.     transpozycja(tab2, n, m);
  82.  
  83.     printf("Wartosc najmniejsza = %lf na pozycji [%d, %d].\n", min, minIndeks + 1, minIndeks2 + 1);
  84.     printf("Wartosc najwieksza = %lf na pozycji [%d, %d].\n", max, maxIndeks + 1, maxIndeks2 + 1);
  85.  
  86.     zwolnijPamiec(tab2, n);
  87.  
  88.     _getch();
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment