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

Program needs to be written in C++. Comments are appreciated. Will award points

ID: 3557830 • Letter: P

Question

Program needs to be written in C++. Comments are appreciated. Will award points tonight to best program.

(patterm recognition. consecutive four equal numbers) Write the following function tests whether a two-dimensional array has four consecutive numbers of the same value, either horizontally, vertically, or diagonally. bool isConsecutiveFour (int values[][7]) 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:

Explanation / Answer

#include<iostream>
#include<stdio.h>
using namespace std;
int m,n;
bool isConsecutiveFour(int values[][100]) {
bool cons = false;
int r, c;

//tests horizontally

for ( r=0; r < m; r++) {
for (c=0; c < n - 3; c++){
if (values[c][r] == values[c+1][r] &&
values[c][r] == values[c+2][r] &&
values[c][r] == values[c+3][r]) {
cons = true;
}
}
}

//tests vertically
for ( r=0; r < m - 3; r++) {
for ( c=0; c < n; c++){
if (values[c][r] == values[c][r+1] &&
values[c][r] == values[c][r+2] &&
values[c][r] == values[c][r+3]) {
cons = true;
}
}
}

//tests diagonally (going down and to the right)
for ( r=3; r < m; r++) {
for ( c=0; c < n - 3; c++) {
if (values[c][r] == values[c+1][r-1] &&
values[c][r] == values[c+2][r-2] &&
values[c][r] == values[c+3][r-3]) {
cons = true;
}
}
}

//tests diagonally (going down and to the left)
for ( r=0; r < m - 3; r++) {
for ( c=0; c < n - 3; c++) {
if (values[c][r] == values[c+1][r+1] &&
values[c][r] == values[c+2][r+2] &&
values[c][r] == values[c+3][r+3]) {
cons = true;
}
}
}
return cons;
}

int main()
{
   int i,j;
   int matrix[100][100];
   printf("Enter number of rows and number of columns");
   scanf("%d%d",&m,&n);
for (i=0;i<m;i++) {
for (j=0;j<n;j++){
scanf("%d",&matrix[i][j]);
}
}


if(isConsecutiveFour(matrix)==true) cout<<"true"<<endl;
else cout<<"false"<<endl;
system("pause");
}