Write a c++ program to replace all negative numbers in a matrix to positive numbers and display the resultant matrix in a matrix form
#include <iostream>
int main() {
const size_t N = 3;
int matrix[N][N] = {
{ 1, -3, 4}
, {-5, 3, 0}
, {-1, 2, -5}
};
std::cout << "Before:\n";
for(auto const& x: matrix) {
for(auto y: x) {
std::cout << y << ' ';
}
std::cout << "\n";
}
// replace
for(auto& x: matrix) {
for(auto& y: x) {
if (y < 0) y = -y;
}
}
std::cout << "After:\n";
for(auto const& x: matrix) {
for(auto y: x) {
std::cout << y << ' ';
}
std::cout << "\n";
}
return 0;
}
Comments
Leave a comment