Advertisement
Mancolo

Untitled

Oct 30th, 2022 (edited)
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10.     int array, *row;
  11.     int n, m, i,j, ansi, maxx, maxy, minx, miny;
  12.     bool ans = false;
  13.     cout << "n= ";
  14.     cin >> n;
  15.  
  16.     cout << "m= ";
  17.     cin >> m;
  18.  
  19.     array = (int)malloc(n * sizeof(int*));
  20.     row = (int*)malloc(m * sizeof(int));
  21.  
  22.     int max1 = -1000, max2 = -1000, min = 1000, sum = 0;
  23.  
  24.     for (i = 0; i < n; i++)
  25.     {
  26.         array[i] = (int*)malloc(m * sizeof(int));
  27.  
  28.         for (j = 0; j < m; j++) {
  29.             array[i][j] = rand() % 200 - 100;
  30.             cout << setw(5) << array[i][j];
  31.  
  32.             sum += array[i][j];
  33.  
  34.             if (array[i][j] > max1) {
  35.                 max2 = max1;
  36.                 max1 = array[i][j];
  37.                 maxx = i;
  38.                 maxy = j;
  39.             }
  40.  
  41.             if (array[i][j] < min) {
  42.                 min = array[i][j];
  43.                 minx = i;
  44.                 miny = j;
  45.             }
  46.         }
  47.         cout << endl;
  48.     }
  49.  
  50.     for (i = 0; i < n; i++) {
  51.         bool frst = false, sec = false;
  52.  
  53.         for (j = 0; j < m; j++) {
  54.             if (array[i][j] == max1) {
  55.                 if (frst)
  56.                     sec = true;
  57.                 frst = true;
  58.             }
  59.         }
  60.  
  61.         if (frst && sec) {
  62.             ans = true;
  63.             ansi = i;
  64.         }
  65.     }
  66.  
  67.     cout << endl << "# 1" << endl << endl;
  68.  
  69.     cout << "Max value " << max1 << endl;
  70.  
  71.     if (ans) {
  72.         cout << "Row with two max values " << ansi << endl;
  73.     }
  74.     else {
  75.         cout << "No row with two max values" << endl;
  76.     }
  77.  
  78.     cout << endl << "# 2" << endl << endl;
  79.  
  80.     cout << "SUM of all: " << sum << endl;
  81.     cout << "Count of all: " << n * m << endl;
  82.     cout << "AVG of all: " << (float)sum / n / m << endl;
  83.     cout << "Min " << min << endl;
  84.     cout << "Max " << max1 << endl;
  85.     cout << "AVG of min and max " << (min + max1) / 2 << endl;
  86.     cout << "Answer: " << (float)sum / n / m - (min + max1) / 2 << endl;
  87.  
  88.     cout << endl << "# 3" << endl << endl;
  89.  
  90.     int minrowsum = 1000000000000, maxrow = -1000, minrowi = 0;
  91.  
  92.     for (i = 0; i < n; i++) {
  93.         int s = 0;
  94.         for (j = 0; j < m; j++) {
  95.             s += array[i][j];
  96.         }
  97.         if (s < minrowsum) {
  98.             minrowsum = s;
  99.             minrowi = i;
  100.         }
  101.     }
  102.  
  103.     for (j = 0; j < m; j++) {
  104.         if (maxrow < array[minrowi][j])
  105.             maxrow = array[minrowi][j];
  106.     }
  107.  
  108.     cout << "Min sum row " << minrowi << endl;
  109.     cout << "Max value in min sum row " << maxrow << endl;
  110.  
  111.     cout << endl << "# 4" << endl << endl;
  112.  
  113.     bool forth = false;
  114.  
  115.     for (j = 0; j < m; j++) {
  116.         bool col = true;
  117.         for (i = 1; i < n; i++) {
  118.             if (array[i][j] == array[i - 1][j]) {
  119.                 col = true;
  120.                 break;
  121.             }
  122.         }
  123.         if (col) {
  124.             forth = true;
  125.             break;
  126.         }
  127.     }
  128.  
  129.     if (forth){
  130.         cout << "There is column with all equal values " << j << endl;
  131.     } else {
  132.         cout << "There is no column with all equal values" << endl;
  133.     }
  134.  
  135.     cout << endl << "# 5" << endl << endl;
  136.  
  137.     ans = false;
  138.  
  139.     for (i = 0; i < n; i++) {
  140.         int c = 0;
  141.         for (j = 0; j < m; j++) {
  142.             if (array[i][j] < 0) {
  143.                 c++;
  144.             }
  145.         }
  146.         if (c == 2) {
  147.             ans = true;
  148.             break;
  149.         }
  150.     }
  151.  
  152.     if (ans) {
  153.         cout << "There is row with 2 neg values " << i << endl;
  154.     }
  155.     else {
  156.         cout << "There is no row with 2 neg values" << endl;
  157.     }
  158.  
  159.     cout << endl << "# 6" << endl << endl;
  160.  
  161.     ans = false;
  162.  
  163.     for (i = 0; i < n; i++) {
  164.         int n = 0, p = 0;
  165.         for (j = 0; j < m; j++) {
  166.             if (array[i][j] < 0) {
  167.                 n++;
  168.             }
  169.             if (array[i][j] > 0) {
  170.                 p++;
  171.             }
  172.         }
  173.         if (p > n) {
  174.             ans = true;
  175.             break;
  176.         }
  177.     }
  178.  
  179.     if (ans) {
  180.         cout << "There is row with more pos values " << i << endl;
  181.     }
  182.     else {
  183.         cout << "There is no row with more pos values" << endl;
  184.     }
  185.  
  186. Адиль, [28.10.2022 13:21]
  187. cout << endl << "# 7" << endl << endl;
  188.  
  189.     int maxrowsum = -1000, maxi=0, mina = 1000;
  190.  
  191.     for (i = 0; i < n; i++) {
  192.         int s = 0;
  193.         for (j = 0; j < m; j++) {
  194.             s += array[i][j];
  195.         }
  196.         if (s > maxrowsum) {
  197.             maxrowsum = s;
  198.             maxi = i;
  199.         }
  200.     }
  201.  
  202.     for (j = 0; j < m; j++) {
  203.         if (mina> array[maxi][j])
  204.             mina = array[maxi][j];
  205.     }
  206.  
  207.     cout << "Max sum row " << maxi << endl;
  208.     cout << "Min abs " << mina << endl;
  209.  
  210.     cout << endl << "# 8" << endl << endl;
  211.  
  212.     ans = false;
  213.  
  214.     for (j = 0; j < m; j++) {
  215.         int n = 0, p = 0;
  216.         for (i = 0; i < n; i++) {
  217.             if (array[i][j] < 0) {
  218.                 n++;
  219.             }
  220.             if (array[i][j] > 0) {
  221.                 p++;
  222.             }
  223.         }
  224.         if (p == n) {
  225.             ans = true;
  226.             break;
  227.         }
  228.     }
  229.  
  230.     if (ans) {
  231.         cout << "There is column with equal pos and neg values " << j << endl;
  232.     }
  233.     else {
  234.         cout << "There is no column with equal pos and neg values " << endl;
  235.     }
  236.  
  237.     cout << endl << "# 9" << endl << endl;
  238.     ans = false;
  239.  
  240.     cout << "Rows with only pos values: " << endl;
  241.  
  242.     for (i = 0; i < n; i++) {
  243.         int p = 0;
  244.         int maxvrow = -1000;
  245.         for (j = 0; j < m; j++) {
  246.             if (array[i][j] > 0) {
  247.                 p++;
  248.             }
  249.             if (maxvrow < array[i][j]) {
  250.                 maxvrow = array[i][j];
  251.             }
  252.         }
  253.         if (p == m) {
  254.             cout << "Row: " << i << endl;
  255.             cout << "Max in this row: " << maxvrow << endl;
  256.             ans = true;
  257.         }
  258.     }
  259.  
  260.     if (!ans) {
  261.         cout << "No rows";
  262.     }
  263.  
  264.     cout << endl << "# 10" << endl << endl;
  265.  
  266.     int sf = 0, sl = 0;
  267.     for (i = 0; i < n; i++) {
  268.         sf += array[i][0];
  269.         sl += array[i][m-1];
  270.     }
  271.  
  272.     int sfc = 0, slc = 0;
  273.     for (j = 0; j < m; j++) {
  274.         sfc += array[0][j];
  275.         slc += array[n-1][j];
  276.     }
  277.  
  278.     cout << "AVG of first row " << (float)sf / n << endl;
  279.     cout << "AVG of last row " << (float)sl / n << endl;
  280.     cout << "AVG of first column " << (float)sfc / m << endl;
  281.     cout << "AVG of last column " << (float)slc / m << endl;
  282.  
  283.     cout << endl << "# 11" << endl << endl;
  284.  
  285.     int maxabs = max1;
  286.     if (abs(maxabs) < abs(min))
  287.         maxabs = min;
  288.    
  289.     for (i = 0; i < n; i++)
  290.     {
  291.         for (j = 0; j < m; j++) {
  292.             cout << setw(10) << (float)array[i][j] / maxabs;
  293.         }
  294.         cout << endl;
  295.     }
  296.  
  297.     cout << endl << "# 12" << endl << endl;
  298.  
  299.     int starti, startj, endi, endj;
  300.  
  301.     if (maxx < minx) {
  302.         starti = maxx;
  303.         startj = maxy;
  304.  
  305.         endi = minx;
  306.         endj = miny;
  307.     }
  308.     else {
  309.         endi = maxx;
  310.         endj = maxy;
  311.  
  312.         starti = minx;
  313.         startj = miny;
  314.     }
  315.  
  316.     int s = 0;
  317.  
  318.     for (i = 0; i < n; i++) {
  319.         for (j = 0; j < m; j++) {
  320.             if ((i > starti && i < endi)
  321.                 (i == starti && j >= startj)
  322.                 (i == endi && j <= startj))
  323.                 s += array[i][j];
  324.         }
  325.     }
  326.  
  327.     cout << "Sum of elements that is between min and max value " << s << endl;
  328.  
  329.     cout << endl << "# 13" << endl << endl;
  330.  
  331.     int anses[1000], id=0;
  332.  
  333.     for (i = 0; i < n; i++)
  334.     {
  335.         for (j = 0; j < m; j++) {
  336.             int v = rand() % 100;
  337.             cout << setw(5) << v;
  338.  
  339.             if (v == i * j) {
  340.                 anses[id++] = v;
  341.             }
  342.         }
  343.         cout << endl;
  344.     }
  345.  
  346.     for (i = 0; i < id; i++) {
  347.         cout << anses[i] << " ";
  348.     }
  349.  
  350.     cout << endl << "# 14" << endl << endl;
  351.  
  352.     int* sumcols, * sumrows;
  353.  
  354.     sumcols = (int*)malloc(m * sizeof(int));
  355.     sumrows = (int*)malloc(n * sizeof(int));
  356.  
  357.     for (i = 0; i < m; i++) {
  358.         sumcols[i] = 0;
  359.     }
  360.     for (i = 0; i < n; i++) {
  361.         sumrows[i] = 0;
  362.     }
  363.  
  364.     for (i = 0; i < n; i++)
  365.     {
  366.         for (j = 0; j < m; j++) {
  367.             sumrows[i] += array[i][j];
  368.             sumcols[j] += array[i][j];
  369.         }
  370.     }
  371.  
  372. Адиль, [28.10.2022 13:21]
  373. int mincol=0, maxrowi=0;
  374.     for (i = 0; i < m; i++) {
  375.         if (sumcols[i] < sumcols[mincol])
  376.             mincol = i;
  377.     }
  378.     for (i = 0; i < n; i++) {
  379.         if (sumrows[i] > sumrows[maxrowi])
  380.             maxrowi = i;
  381.     }
  382.  
  383.     cout << "Max row " << maxrowi << endl;
  384.     cout << "Min col " << mincol << endl;
  385.  
  386.     cout << endl << "# 15" << endl << endl;
  387.  
  388.     for (i = 0; i < n; i++)
  389.     {
  390.         int minrows = 0, maxrows = 0, temp;
  391.         for (j = 0; j < m; j++) {
  392.             if (array[i][maxrows] < array[i][j]) {
  393.                 maxrows = j;
  394.             }
  395.         }
  396.         temp = array[i][maxrows];
  397.         array[i][maxrows] = array[i][m-1];
  398.         array[i][m-1] = temp;
  399.     }
  400.  
  401.  
  402.     for (i = 0; i < n; i++)
  403.     {
  404.         int minrows = 0, maxrows = 0, temp;
  405.         for (j = 0; j < m; j++) {
  406.             if (array[i][minrows] > array[i][j]) {
  407.                 minrows = j;
  408.             }
  409.         }
  410.  
  411.         temp = array[i][minrows];
  412.         array[i][minrows] = array[i][0];
  413.         array[i][0] = temp;
  414.     }
  415.  
  416.  
  417.     for (i = 0; i < n; i++)
  418.     {
  419.         for (j = 0; j < m; j++) {
  420.             cout << setw(5) << array[i][j];
  421.         }
  422.         cout << endl;
  423.     }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement