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.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
constexprint ROWS{ 2 };
constexprint 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.
#include <iostream>
#include <random>
#include <climits>
int main()
{
constexprint ROWS { 2 };
constexprint 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.
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.
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.
#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 (constauto& r : E) {
constauto 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';
}
}