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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <iomanip>
using namespace std;
constexpr size_t MAXROW {5}, MAXCOL {5};
using Array = int[MAXROW][MAXCOL];
void display(const Array nums)
{
for (size_t r = 0; r < MAXROW; ++r) {
for (size_t c = 0; c < MAXCOL; ++c)
std::cout << std::setw(6) << nums[r][c];
std::cout << '\n';
}
std::cout << '\n';
}
void repneg(Array nums, size_t rp, int rep)
{
for (size_t c = 0; c < MAXCOL; ++c)
if (nums[rp][c] < 0)
nums[rp][c] = rep;
}
size_t cntpos(const Array nums)
{
size_t cnt {};
for (size_t r = 0; r < MAXROW; ++r)
for (size_t c = 0; c < MAXCOL; ++c)
cnt += nums[r][c] > 0;
return cnt;
}
int main()
{
Array numArray
{
{ 2, -5 },
{ 3, 6, 1, -4, 2 },
{ 8 },
{ 9, 2, 1, 2, 9 },
{ 4, -6 }
};
std::cout << "At start\n";
display(numArray);
std::cout << "Replace neg in r2\n";
repneg(numArray, 1, 50);
display(numArray);
std::cout << "There are " << cntpos(numArray) << " pos numbers\n";
}
| |