Guest User

trrt

a guest
Dec 8th, 2019
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<math.h>
  4. #include<conio.h>
  5.  
  6.  
  7. float** Alocare(int n, int m) {
  8. int i;
  9. float** tab = (float**)malloc(sizeof(float*) * n);
  10. for (i = 0; i < n; i++)
  11. tab[i] = (float*)malloc(sizeof(float) * m);
  12. return tab;
  13. }
  14.  
  15. void Introducere_tast(float** tab, int n, int m)
  16. {
  17. int i;
  18. int j;
  19. for (i = 0; i < n; i++) {
  20. for (j = 0; j < m; j++) {
  21. printf("Introduceti valoarea de pe pozitia [%d][%d]: ", i, j);
  22. scanf_s("%f", &tab[i][j]);
  23. }
  24. }
  25. }
  26. void Introducere_rand(float** tab, int n, int m)
  27. {
  28. int i;
  29. int j;
  30. for (i = 0; i < n; i++) {
  31. for (j = 0; j < m; j++) {
  32. tab[i][j] = rand();
  33. }
  34. }
  35. }
  36.  
  37. void Afisare(float** tab, int n, int m)
  38. {
  39. int i = 0;
  40. int j = 0;
  41.  
  42. for (i = 0; i < n; i++) {
  43. for (j = 0; j < m; j++) {
  44. printf("%f ", tab[i][j]);
  45. }
  46. printf("\n");
  47. }
  48. }
  49.  
  50. void Eliberarea_memoriei(float** tab, int n)
  51. {
  52.  
  53. int i = 0;
  54.  
  55. for(i = 0; i < n; i++)
  56. free(tab[i]);
  57. free(tab);
  58. }
  59. void schimb(float* a, float* b)
  60. {
  61.  
  62. float aux;
  63. aux = *a;
  64. *a = *b;
  65. *b = aux;
  66.  
  67. }
  68. float** Sortare(float** tab, int n, int m)
  69. {
  70.  
  71. int i;
  72. int j;
  73. int k;
  74. int p;
  75. i = 0;
  76. while (i < n)
  77. {
  78.  
  79. j = 0;
  80. while (j < m)
  81. {
  82. k = j;
  83. p = k;
  84. while (++p < m)
  85. {
  86. if (tab[k][j] > tab[p][j]) { k = p; }
  87. }
  88. schimb(&tab[k][j], &tab[i][j]);
  89. j = j + 2;
  90. }
  91. i = i + 1;
  92. }
  93. return(tab);
  94. }
  95. void menu()
  96. {
  97. printf("-------------------------------Menu------------------------\n");
  98. printf("[1] - Alocarea tabloului de n linii si m coloane\n");
  99. printf("[2] - Introducerea datelor de la tastatura\n");
  100. printf("[3] - Introducerea unor date random in tablou\n");
  101. printf("[4] - Sortarea tabloului\n");
  102. printf("[5] - Afisarea tabloului\n");
  103. printf("[6] - Eliberarea memoriei\n");
  104. printf("[0] - Iesire din program\n");
  105. printf("-------------------------------------------------------------\n");
  106. }
  107.  
  108. int main()
  109. {
  110.  
  111. int q;
  112. int n;
  113. int m;
  114. float** tab = 0;
  115.  
  116. while (1)
  117. {
  118. menu();
  119. printf("Alegeti setarea :");
  120. scanf_s("%d", &q);
  121. switch (q)
  122. {
  123. case(1):
  124. printf("Ce marime va avea tabloul?:\n");
  125. scanf_s("%d %d", &n, &m);
  126. tab = Alocare(n, m);
  127. break;
  128. case(2):
  129. Introducere_tast(tab, n, m);
  130. break;
  131. case(3):
  132. Introducere_rand(tab, n, m);
  133. break;
  134. case(4):
  135. Sortare(tab, n, m);
  136. break;
  137. case(5):
  138. Afisare(tab, n, m);
  139. break;
  140. case(6):
  141. Eliberarea_memoriei(tab, n);
  142. break;
  143. case(0):
  144. printf("Ati iesit cu succes din program\n");
  145. return(0);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. return(0);
  152. }
RAW Paste Data