Answer to Question #190359 in C++ for Aby

Question #190359

Write a c++ program to replace all negative numbers in a matrix to positive numbers and display the resultant matrix in a matrix form


1
Expert's answer
2021-05-07T09:40:09-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS