Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8.19 (Pattern recognition: four consecutive equal numbers) Write the following m

ID: 3847968 • Letter: 8

Question

8.19 (Pattern recognition: four consecutive equal numbers) Write the following method that tests whether a two-dimensional array has four consecutive numbers of the same value, either horizontally, vertically, or diagonally. public static boolean isConsecutiveFour(int[][] values) FIGURE Write a test program that prompts the user to enter the number of rows and columns of a two-dimensional array and then the values in the array and displays true if the array contains four consecutive numbers with the same value. Otherwise, display false. Here are some examples of the true cases:

| | | | 5621829 5621629 9621829 1361407 1361407 1391407 3333407 3633407 3339407 119177 16814 38111 9 062603 116933 009613 119177 602900 166 381613 005613 119177 602900 168144 062663 119177 602900 16814 38111 062663 11653 005613

Explanation / Answer

#include<iostream>
using namespace std;

int h;
int v;
int row;
int column;


bool isConsecutiveFour(int values[][7])
{
   bool cons = false;

   //tests horizontally

   for (row = 0; row < h; row++)
   {
       for (column = 0; column < v - 3; column++)
       {
           if (values[column][row] == values[column + 1][row] &&
               values[column][row] == values[column + 2][row] &&
               values[column][row] == values[column + 3][row])
           {
               cons = true;
           }
       }
   }

   //tests vertically
   for (row = 0; row < h - 3; row++)
   {
       for (column = 0; column < v; column++)
       {
           if (values[column][row] == values[column][row + 1] &&
               values[column][row] == values[column][row + 2] &&
               values[column][row] == values[column][row + 3])
           {
               cons = true;
           }
       }
   }

   //tests diagonally (going down and to the right)
   for (row = 3; row < h; row++)
   {
       for (column = 0; column < v - 3; column++)
       {
           if (values[column][row] == values[column + 1][row - 1] &&
               values[column][row] == values[column + 2][row - 2] &&
               values[column][row] == values[column + 3][row - 3])
           {
               cons = true;
           }
       }
   }

   //tests diagonally (going down and to the left)
   for (row = 0; row < h - 3; row++)
   {
       for (column = 0; column < v - 3; column++)
       {
           if (values[column][row] == values[column + 1][row + 1] &&
               values[column][row] == values[column + 2][row + 2] &&
               values[column][row] == values[column + 3][row + 3])
           {
               cons = true;
           }
       }
   }
   return cons;
}

int main()
{
   int i;
   int j;
   int matrix[10][7];

   cout <<"Enter number of rows and number of columns";
   cin >> h >> v;
   for (i = 0; i < h; i++)
   {
       for (j = 0; j < v; j++)
       {
           cin >> matrix[i][j];
       }
   }
   if (isConsecutiveFour(matrix) == true) cout << "true" << endl;
   else cout << "false" << endl;
   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote