C/C++

Подсчитайте количество столбцов массива, которые не содержат отрицательных значений.

#include <iostream>
#include <random>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;

class RNG {
private:
random_device rd;
default_random_engine re;
public:
RNG() { re.seed(this->rd()); }
double randreal() {
return uniform_real_distribution<>{-1, 2}(this->re);
}
};
int main() {
int k = 0;
double nc, nr;
cout << "Please, enter number of rows: ";
cin >> nr;

if (!cin || nr < 0) {
cout << "Error";
return 0;
}

cout << "Please, enter number of columns: ";
cin >> nc;

if (!cin || nc < 0) {
cout << "Error";
return 0;
}

cout << endl;
double** array = new double* [nr];

for (int i = 0; i < nr; i++) {
array[i] = new double[nc];
for (int j = 0; j < nc; j++) {
RNG gen{};
array[i][j] = gen.randreal();
cout << array[i][j] << " ";
}
cout << endl;
}
//вот тут нужно сделать вывод (кол-во столбцов)
}
Sasha Kruglikov
Sasha Kruglikov
89
#include <iostream>
#include <random>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;

class RNG {
private:
random_device rd;
default_random_engine re;
public:
RNG() { re.seed(this->rd()); }
double randreal() {
return uniform_real_distribution<> { -1, 2 } (this->re);
}
} ;
int main() {
int k = 0;
int nc, nr;
cout << "Please, enter number of rows: ";
cin >> nr;

if (!cin || nr < 0) {
cout << "Error";
return 0;
}

cout << "Please, enter number of columns: ";
cin >> nc;

if (!cin || nc < 0) {
cout << "Error";
return 0;
}

cout << endl;
double** array = new double* [nr];

for (int i = 0; i < nr; i++) {
array[i] = new double[nc];
for (int j = 0; j < nc; j++) {
RNG gen { } ;
array[i][j] = gen.randreal();
cout << array[i][j] << "\t";
}
cout << endl;
}
//вот тут нужно сделать вывод (кол-во столбцов)
int count = 0;
for (int i = 0; i < nc; i++) {
int nonminus = 1;
for (int j = 0; j < nr; j++) {
if(array[j][i] < 0.0)
{
nonminus = 0;
break;
}
}
if(nonminus) count++;
}
cout << endl << count << endl;
}
НШ
Николай Шанаурин
93 488
Лучший ответ
Николай Шанаурин Обратите внимание, что nr и nc должны иметь тип int

Похожие вопросы