Answer to Question #338841 in C++ for Ashir

Question #338841

Write a C++ program that displays the row numbers of a matrix containing at least two even


numbers. First, the program should create a 2D array (5 rows, 5 columns). Then, ask the user to


input values in the matrix or 2D array. After that, program should display row number (or index)


which has less than 2 prime numbers. If no row (of the matrix) contains at least two prime


numbers then the program should display the message “No row found containing two prime


numbers”.

1
Expert's answer
2022-05-09T14:55:10-0400
#include <iostream>

using namespace std;

const int MATRIX_SIZE = 5;

bool isPrime(int n) {
    if (n <= 1) return false;

    for (int i = 2; i < n; i++) {
        if (n % i == 0) return false;
    }

    return true;
}

void displayRowsWithTwoPrimes(int matrix[MATRIX_SIZE][MATRIX_SIZE]) {
    int primeCounter;
    bool rowsFound = false;

    for (int i = 0; i < MATRIX_SIZE; i++) {
        primeCounter = 0;

        for (int j = 0; j < MATRIX_SIZE; j++) {
            if (isPrime(matrix[i][j])) primeCounter++;
        }

        if (primeCounter >= 2) rowsFound = true;
        else cout << "Row with index " << i << " containing less than two prime numbers" << endl;
    }

    if (!rowsFound) {
        cout << "No row found containing two prime numbers" << endl;
    }
}

int main() {
    int matrix[MATRIX_SIZE][MATRIX_SIZE];

    cout << "Enter square matrix "
        << MATRIX_SIZE << "x" << MATRIX_SIZE << ":"
        << endl;
    for (int i = 0; i < MATRIX_SIZE; i++) {
        for (int j = 0; j < MATRIX_SIZE; j++) {
            cin >> matrix[i][j];
        }
    }

    displayRowsWithTwoPrimes(matrix);

    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