Check my code, please!

Nov 11, 2020 at 10:36pm
I wrote the code but it doesn't work properly. Please check and correct mistakes or suggest your solutions.
I will be grateful!
I also add a task
A two-dimensional array is given, in which it is necessary to calculate the arithmetic mean of each column and the maximum and minimum of each row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    constexpr int ROWS{ 2 };
    constexpr int COLS{ 5 };

    srand((unsigned)time(NULL));
    int col = 0, row = 0;
    int E[ROWS][COLS];

    cout << "Matz - " << ROWS << " x " << COLS << "\n\n";

    for (int row{ }; row < ROWS; row++)
    {
        for (int col{ }; col < COLS; col++)
        {
            E[row][col] = rand() % 10;

            cout << E[row][col] << ' ';
        }
        cout << '\n';
    }
    cout << '\n';

    cout << "characteristic of the first line:" << endl;
    int a = 0;
    int sum{ };
    for (int col{ }; col < COLS; col++)
    {
        sum += E[0][col];
            a = sum / COLS ;
    }
  

    cout << a << '\n';

    int min = E[0][col];
    for (int col = 0; col < COLS; col++) {

        if (min > E[0][col])
        {
            min = E[0][col];
        }
    }
    cout << min << endl;

    int max = E[0][col];
    for (int col = 0; col < COLS; col++) {

        if (max < E[0][col])
        {
            max = E[0][col];
        }
    }
    cout << max << endl;

    cout << "characteristic of the second line:" << endl;
    int dev = { };
    for (int row=0; row < COLS; row++)
   {
      for (int col=0; col < COLS; col++)
       {
           dev += E[0][col];
       }
   }
   int b{ dev / COLS };

   cout << b << '\n';
    
    int min1 = E[0][col];
   for (int row=0; row < ROWS; row++)
    {
        for (int col = 0; col < COLS; col++)
     {
        if (min1 > E[row][col])
        {
            min1 = E[row][col];
        }
      }
    }
    cout << min1 << endl;

    int max1 = E[0][col];
   
       for (int row=0; row < ROWS; row++)
     {
           for (int col = 0; col < COLS; col++)
        { 
           
            if (max1 < E[row][col])
            {
                max1 = E[row][col];
            }
        }
    }
    cout << max1 << endl;

}  Put the code you need help with here.
Nov 11, 2020 at 11:04pm
How to find min and max for each row:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <random>
#include <climits>

int main()
{
   constexpr int ROWS { 2 };
   constexpr int COLS { 5 };

   int matrix[ROWS][COLS];

   std::cout << "Matrix - " << ROWS << " x " << COLS << ":\n";

   for (auto& row : matrix)
   {
      for (auto& col : row)
      {
         static std::uniform_int_distribution<int> dist (0, 9);
         static std::random_device rng;

         col = dist(rng);
         std::cout << col << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // the maximum and minimum of each row
   for (size_t row { }; row < ROWS; row++)
   {
      int min { INT_MAX };
      int max { INT_MIN };

      for (auto& itr: matrix[row])
      {
         if (itr < min) { min = itr; }

         if (itr > max) {max = itr; }
      }

      std::cout << "Row " << row << ", min: " << min << ", max: " << max << '\n';
   }
}

It doesn't help you to simply copy and paste code you don't really understand, adding on bits of new code. I recognize code I wrote earlier which you simply grafted your attempts onto.
Nov 11, 2020 at 11:27pm
Now that you explained what the code your wrote earlier is supposed to do writing code to do the wanted calculations is easy:

43
44
45
46
47
48
49
50
51
52
53
54
   // the arithmetic mean (average) of each column
   for (size_t col { }; col < COLS; col++)
   {
      int sum { };

      for (size_t row { }; row < ROWS; row++)
      {
         sum += matrix[row][col];
      }

      std::cout << "Column " << col << " arithmetic mean (average): " << sum / ROWS << '\n';
   }

Integer division can give less than accurate results.
Nov 12, 2020 at 12:35am
Okay, thanks for the help with the code. There is little work left to do.
I just sat down to master this profession and discover a lot of new things. And every experience will be valuable to me.
Nov 12, 2020 at 1:09am
Using regular arrays is rather restrictive, if you want a different size 2D arrays you have to change size constants and recompile.

C++ has std::vector, a container that is dynamic sized, you don't have to recompile to have a different size.

Add #include <vector> to the include list in the code I gave you earlier, and change lines 7-10 in the code to this:
7
8
9
10
11
12
13
14
15
16
   std::cout << "Enter # of matrix rows: ";
   size_t ROWS { };
   std::cin >> ROWS;

   std::cout << "Enter # of matrix columns: ";
   size_t COLS { };
   std::cin >> COLS;
   std::cout << '\n';

   std::vector<std::vector<int>> matrix(ROWS, std::vector<int>(COLS));

The rest of the code will work the same, std::vector or regular array. Now you can change the dimensions with each run without having to recompile.
Nov 12, 2020 at 10:21am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <random>
#include <iostream>
#include <algorithm>
#include <numeric>

int main()
{
	const size_t noRow {2};
	const size_t noCol {5};

	auto rand {std::mt19937 {std::random_device {}()}};
	auto randNo {std::uniform_int_distribution<size_t> {0, 9}};

	int E[noRow][noCol] {};

	for (auto& r : E)
		for (auto& e : r)
			e = randNo(rand);

	// Min/max per row

	size_t rw {};
	for (const auto& r : E) {
		const auto minmax {std::minmax_element(std::begin(r), std::end(r))};
		std::cout << "Row " << rw++ << " Minimum: " << *minmax.first << ", maximum: " << *minmax.second << '\n';
	}

	// Average of each column

	for (size_t c = 0; c < noCol; ++c) {
		int sum {};

		for (size_t r = 0; r < noRow; ++r)
			sum += E[r][c];

		std::cout << "Col " << c << " average (int): " << sum / noRow << '\n';
	}
}

Topic archived. No new replies allowed.