fork download
  1. #include <stdio.h>
  2.  
  3. #include <malloc.h>
  4.  
  5.  
  6. typedef struct Line{
  7.  
  8. int n;
  9.  
  10. double *a;
  11.  
  12. } Line;
  13.  
  14.  
  15. typedef struct Matrix{
  16.  
  17. int lines;
  18.  
  19. Line *matr;
  20.  
  21. } Matrix;
  22.  
  23.  
  24. int getInt(int *);
  25.  
  26. int getDouble(double *);
  27.  
  28. int input(Matrix *a);
  29.  
  30. void output(const char *msg, Matrix a);
  31.  
  32. void erase(Matrix *a);
  33.  
  34. double minmax(Matrix a);
  35.  
  36. double max(double a[], int m);
  37.  
  38. double min(double a[], int m);
  39.  
  40. double mm(double a[], int m, int flag);
  41.  
  42.  
  43.  
  44. int main()
  45.  
  46. {
  47.  
  48. Matrix matr = {0, NULL};
  49.  
  50. double res;
  51.  
  52. if (input(&matr) == 0){
  53.  
  54. printf("%s\n", "End of file occured");
  55.  
  56. return 1;
  57.  
  58. }
  59.  
  60. res = minmax(matr);
  61.  
  62. output("Source matrix", matr);
  63.  
  64. printf("Result: %f\n", res);
  65.  
  66. erase(&matr);
  67.  
  68. return 0;
  69.  
  70. }
  71.  
  72.  
  73. int getInt(int *a)
  74.  
  75. {
  76.  
  77. int n;
  78.  
  79. do{
  80.  
  81. n = scanf_s("%d", a, sizeof(int));
  82.  
  83. if (n < 0)
  84.  
  85. return 0;
  86.  
  87. if (n == 0){
  88.  
  89. printf("%s\n", "Error! Repeat input");
  90.  
  91. scanf_s("%*c", 0);
  92.  
  93. }
  94.  
  95. } while (n == 0);
  96.  
  97. return 1;
  98.  
  99. }
  100.  
  101.  
  102. int getDouble(double *a)
  103.  
  104. {
  105.  
  106. int n;
  107.  
  108. do{
  109.  
  110. n = scanf_s("%lf", a, sizeof(double));
  111.  
  112. if (n < 0)
  113.  
  114. return 0;
  115.  
  116. if (n == 0){
  117.  
  118. printf("%s\n", "Error! Repeat input");
  119.  
  120. scanf_s("%*c", 0);
  121.  
  122. }
  123.  
  124. } while (n == 0);
  125.  
  126. return 1;
  127.  
  128. }
  129.  
  130.  
  131. int input(Matrix *rm)
  132.  
  133. {
  134.  
  135. const char *pr = "";
  136.  
  137. int m;
  138.  
  139. int i, j;
  140.  
  141. double *p;
  142.  
  143.  
  144. do{
  145.  
  146. printf("%s\n", pr);
  147.  
  148. printf("Enter number of lines: --> ");
  149.  
  150. pr = "You are wrong; repeat, please!";
  151.  
  152. if(getInt(&m) == 0)
  153.  
  154. return 0;
  155.  
  156. } while (m < 1);
  157.  
  158. rm->lines = m;
  159.  
  160. rm->matr = (Line *)calloc(m, sizeof(Line));
  161.  
  162. for (i = 0; i < rm->lines; ++i){
  163.  
  164. pr = "";
  165.  
  166. do{
  167.  
  168. printf("%s\n", pr);
  169.  
  170. printf("Enter number of items in line %d: --> ", i + 1);
  171.  
  172. pr = "You are wrong; repeat, please!";
  173.  
  174. if (getInt(&m) == 0){
  175.  
  176. rm->lines = i;
  177.  
  178. erase(rm);
  179.  
  180. return 0;
  181.  
  182. }
  183.  
  184. } while (m < 1);
  185.  
  186. rm->matr[i].n = m;
  187.  
  188.  
  189.  
  190. p = (double *)malloc(sizeof(double)* m);
  191.  
  192. rm->matr[i].a = p;
  193.  
  194. printf("Enter items for matrix line #%d:\n", i + 1);
  195.  
  196. for (j = 0; j < m; ++j, ++p)
  197.  
  198. if (getDouble(p) == 0){
  199.  
  200. rm->lines = i + 1;
  201.  
  202. erase(rm);
  203.  
  204. return 0;
  205.  
  206. }
  207.  
  208. }
  209.  
  210. return 1;
  211.  
  212. }
  213.  
  214. void output(const char *msg, Matrix a)
  215.  
  216. {
  217.  
  218. int i, j;
  219.  
  220. double *p;
  221.  
  222. printf("%s:\n", msg);
  223.  
  224. for (i = 0; i < a.lines; ++i){
  225.  
  226. p = a.matr[i].a;
  227.  
  228. for (j = 0; j < a.matr[i].n; ++j, ++p)
  229.  
  230. printf("%10lf ", *p);
  231.  
  232. printf("\n");
  233.  
  234. }
  235.  
  236. }
  237.  
  238.  
  239. void erase(Matrix *a)
  240.  
  241. {
  242.  
  243. int i;
  244.  
  245. for (i = 0; i < a->lines; ++i)
  246.  
  247. free(a->matr[i].a);
  248.  
  249. free(a->matr);
  250.  
  251. a->lines = 0;
  252.  
  253. a->matr = NULL;
  254.  
  255. }
  256.  
  257. double minmax(Matrix pm)
  258.  
  259. {
  260.  
  261. double *s = (double *)malloc(sizeof(double)* pm.lines);
  262.  
  263. double res;
  264. double *p = s;
  265.  
  266. int i;
  267.  
  268. for (i = 0; i < pm.lines; ++i)
  269.  
  270. *p++ = max(pm.matr[i].a, pm.matr[i].n); // ȜȟȜ s[i] = mm(pm.matr[i].a,pm.matr[i].n, 1);
  271.  
  272. res = min(s, pm.lines); // ȜȟȜ res = mm(s, pm.lines, -1);
  273.  
  274. free(s);
  275.  
  276. return res;
  277.  
  278. }
  279.  
  280.  
  281. double max(double a[], int m)
  282.  
  283. {
  284.  
  285. double res = *a;
  286. for (; m-- > 0; ++a)
  287.  
  288. if (*a > res)
  289.  
  290. res = *a;
  291.  
  292. return res;
  293.  
  294. }
  295.  
  296. double min(double a[], int m)
  297.  
  298. {
  299.  
  300. double res = *a;
  301.  
  302. for (; m-- > 0; ++a)
  303.  
  304. if (*a < res)
  305.  
  306. res = *a;
  307.  
  308. return res;
  309.  
  310. }
  311.  
  312.  
  313.  
  314. double mm(double a[], int m, int flag)
  315.  
  316. {
  317.  
  318. double res = *a;
  319.  
  320. for (; m-- > 0; ++a)
  321.  
  322. if (flag > 0 ? *a > res : *a < res) // ȜȟȜ (*a * flag > res * flag)
  323.  
  324. res = *a;
  325.  
  326. return res;
  327.  
  328. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘getInt’:
prog.c:81:5: warning: implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
 n = scanf_s("%d", a, sizeof(int));
     ^~~~~~~
/home/9PoXrz/ccxBU9d5.o: In function `getInt':
prog.c:(.text+0x1e): undefined reference to `scanf_s'
prog.c:(.text+0x50): undefined reference to `scanf_s'
/home/9PoXrz/ccxBU9d5.o: In function `getDouble':
prog.c:(.text+0x8e): undefined reference to `scanf_s'
prog.c:(.text+0xc0): undefined reference to `scanf_s'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty