Finding Largest Even Row in 2D Array

Beginner

Introduction

In this lab, we will be writing a C++ program that finds the row in an array that includes the greatest amount of even numbers. We will be using a 2-Dimensional array.

Declare necessary variables

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

We will begin by declaring the necessary variables, which include a, n, l, i, j, and count.

int a[10][10], n, l, i, j, count = 0;

Get size of matrix and row number

Next, we will get the size of the matrix and the row number from the user. We will use the cin function to get user input values.

cout << "Enter matrix size(l * l): ";
cin >> l;
cout << "Enter row number of matrix: ";
cin >> n;

Check validity of user input

We will check the validity of the user input by checking if n is within the limits of the matrix size. If it is not, we will display an error message and exit the program.

if (n < 0 || n > l) {
  cout << "Error: Row exceeds matrix size. Please enter a value > 0 and less than or equal to the size of matrix." << endl;
  exit(0);
}

Get matrix values

We will use nested for loops to get the matrix values from the user.

cout << "Enter the matrix values: " << endl;

for (i = 1; i <= l; i++) {
  for (j = 1; j <= l; j++) {
    cin >> a[i][j];
  }
}

Find row with greatest amount of even numbers

We will use a for loop to iterate through the row and count the amount of even numbers. If the current number is even, we will increment the count variable.

for (i = 1; i <= l; i++) {
  if (a[n][i] % 2 == 0) {
    count++;
  }
}

Output results

We will output the row values and the amount of even numbers in that row.

cout << "\nRow is given below:\n";

for (i = 1; i <= l; i++) {
  cout << a[n][i] << " ";
}

cout << "\n\nNo. of even numbers in row is: " << count << endl;

Compile and run the program

We will compile the program using the g++ compiler and run it in the terminal.

$ g++ main.cpp -o main
$ ./main
Full code
#include<iostream>
using namespace std;

int main() {
  int a[10][10], n, l, i, j, count = 0;

  cout << "Enter matrix size(l * l): ";
  cin >> l;
  cout << "Enter row number of matrix: ";
  cin >> n;

  if (n < 0 || n > l) {
    cout << "Error: Row exceeds matrix size. Please enter a value > 0 and less than or equal to the size of matrix." << endl;
    exit(0);
  }

  cout << "Enter the matrix values: " << endl;

  for (i = 1; i <= l; i++) {
    for (j = 1; j <= l; j++) {
      cin >> a[i][j];
    }
  }

  for (i = 1; i <= l; i++) {
    if (a[n][i] % 2 == 0) {
      count++;
    }
  }

  cout << "\nRow is given below:\n";

  for (i = 1; i <= l; i++) {
    cout << a[n][i] << " ";
  }

  cout << "\n\nNo. of even numbers in row is: " << count << endl;

  return 0;
}

Summary

In this lab, we learned how to implement a program in C++ to find the row in a 2-Dimensional array that includes the greatest amount of even numbers. We achieved this by creating a set of variables and using loops to iterate through the array. We used conditional statements to output the results of the program.

Other Tutorials you may like