Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <stdio_ext.h>
  6.  
  7. int **m_realloc(int **m, size_t rows, size_t columns){
  8.  
  9. m=realloc(m, rows*sizeof(int*));
  10.  
  11. for(size_t y=0; y<rows; y++){
  12.  
  13. m[y]=realloc(m[y], columns*sizeof(int));
  14. }
  15.  
  16. return m;
  17. }
  18.  
  19. void m_fill(int **m, size_t rows, size_t columns, size_t or, size_t oc, int max){
  20.  
  21. srand((unsigned)time(NULL));
  22.  
  23. size_t aux_y=0, aux_x=0;
  24.  
  25. if(rows!=or && columns!=oc){
  26.  
  27. aux_y=or;
  28. aux_x=0;
  29.  
  30. for(size_t y=aux_y; y<rows; y++){
  31.  
  32. for(size_t x=aux_x; x<columns; x++){
  33.  
  34. m[y][x]=rand()%max;
  35. }
  36. }
  37.  
  38. aux_y=0;
  39. aux_x=oc;
  40.  
  41. for(size_t y=aux_y; y<(rows-1); y++){
  42.  
  43. for(size_t x=aux_x; x<columns; x++){
  44.  
  45. m[y][x]=rand()%max;
  46. }
  47. }
  48.  
  49. }else{
  50.  
  51. if(rows!=or){
  52.  
  53. aux_y=or;
  54. aux_x=0;
  55.  
  56. }else{
  57.  
  58. aux_y=0;
  59. aux_x=oc;
  60. }
  61.  
  62. for(size_t y=aux_y; y<rows; y++){
  63.  
  64. for(size_t x=aux_x; x<columns; x++){
  65.  
  66. m[y][x]=rand()%max;
  67. }
  68. }
  69. }
  70. }
  71.  
  72. void m_show(int **m, size_t rows, size_t columns){
  73.  
  74. printf("n");
  75.  
  76. for(size_t y=0; y<rows; y++){
  77.  
  78. for(size_t x=0; x<columns; x++){
  79.  
  80. printf("[%d]t", m[y][x]);
  81. }
  82.  
  83. printf("n");
  84. }
  85.  
  86. printf("n");
  87. }
  88.  
  89. void m_free(int **m, size_t rows){
  90.  
  91. for(size_t i=0; i<rows; i++){
  92.  
  93. free(m[i]);
  94. }
  95.  
  96. free(m);
  97. }
  98.  
  99. int main(void){
  100.  
  101. int max=0;
  102.  
  103. size_t or=0, oc=0;
  104. ssize_t rows=0, columns=0;
  105.  
  106. bool decreased=false;
  107.  
  108. unsigned char choice='';
  109.  
  110. int **m=NULL;
  111.  
  112. do{
  113.  
  114. do{
  115.  
  116. printf("nLinhas >");
  117. scanf("%ld", &rows);
  118.  
  119. printf("Colunas >");
  120. scanf("%ld", &columns);
  121.  
  122. if(rows<=0 || columns<=0){
  123.  
  124. printf("n* Número de linhas ou colunas inválidos!n");
  125.  
  126. }else{
  127.  
  128. if(rows<or){
  129.  
  130. decreased=true;
  131.  
  132. }else if(columns<oc){
  133.  
  134. decreased=true;
  135.  
  136. }else if(rows==or && columns==oc){
  137.  
  138. decreased=true;
  139.  
  140. }else{
  141.  
  142. decreased=false;
  143. }
  144. }
  145.  
  146. }while(rows<=0 || columns<=0);
  147.  
  148. if(decreased==false){
  149.  
  150. if(or!=0 && oc!=0){
  151.  
  152. printf("nValor máximo dentro de cada um dos novos campos >");
  153. scanf("%d", &max);
  154.  
  155. }else{
  156.  
  157. printf("nValor máximo dentro de cada campo >");
  158. scanf("%d", &max);
  159. }
  160. }
  161.  
  162. m=m_realloc(m, rows, columns);
  163.  
  164. if(decreased==false){
  165.  
  166. m_fill(m, rows, columns, or, oc, max);
  167. }
  168.  
  169. m_show(m, rows, columns);
  170.  
  171. __fpurge(stdin);
  172.  
  173. or=rows;
  174. oc=columns;
  175.  
  176. printf("Deseja redimensionar a matriz [Y/n]?");
  177. choice=getchar();
  178.  
  179. }while(choice=='y' || choice=='Y');
  180.  
  181. printf("n");
  182.  
  183. m_free(m, rows);
  184.  
  185. m=NULL;
  186.  
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement