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

Alright, I have this code that asks the user for a height and width and it will

ID: 3537009 • Letter: A

Question

Alright, I have this code that asks the user for a height and width and it will create a randomly generated grid in height and width. The program will also see the greatest product in 4 adjacent numbers. All I need help on is some quick error handling.


1. Print an error message if the user doesn%u2019t supply a number value for any one of the

options

2. Print an error message if the user enters <= 3 for height and width command line

arguments.

3. Print an error message if the user doesn%u2019t enter an integer value for the command

line arguments.

4. Print an error message if the user doesn%u2019t supply a number for the rows and cols

5. Print the grid on the output.


#include <iostream>

14 #include <fstream>

15 #include <cstdlib>

16

17 using namespace std;

18

19 #define NUMBER_OF_CONSECUTIVE_DIGITS 4

20

21 int main()

22 {

23 int index = 0;

24 int WIDTH,HEIGHT;

25 cout<<"Enter Width"<<endl;

26 cin>>WIDTH;

27 cout<<"Enter Height"<<endl;

28 cin>>HEIGHT;

29 int grid[ WIDTH ][ HEIGHT ];

30 double temp = 1;

31 int width = 0;

32 int height = 0;

33 int max_start_index_x = 0;

34 int max_start_index_y = 0;

35 double max_product = 0;

36 int what_type_of_consecutive = 0; // 1 = horizontal, 2 = vertical

37 // 3 = diaganol-down-right, 4 = diaganol-up-right

38

39 int i = 0;

40 int x = 0;

41 int y = 0;

42 int j=0;

43 //filling with random values less than 100

44 for(i=0;i<WIDTH;i++)

45 {

46 for(j=0;j<HEIGHT;j++)

47 {

48 grid[ i ][ j ] = rand()%100;

Explanation / Answer

Hi,

A couple of requests, never paste a code with line numbers its really painful to remove them,

I have added the input error handling, there is a doubt when you say about rows and cols, but there are no inputs for them, so look into it. you can easily copy the error handling code, comment if you have any doubts, i will paste the code below.





#include <iostream>

#include <fstream>

#include <cstdlib>


using namespace std;


#define NUMBER_OF_CONSECUTIVE_DIGITS 4


int main()

{

int index = 0;

int WIDTH,HEIGHT;

//int widthflag = 1, heightflag = 1;


do{

cout<<"Enter Width"<<endl;

cin>>WIDTH;

if(cin.fail()){

cin.clear(); cin.ignore(1000,' ');

cout << "Please enter a valid integer ";

continue;

}

if(WIDTH <= 3) {

cout <<"Please enter a width > 3 ";

}else{

break;

}

}while(1);


do{

cout<<"Enter Height"<<endl;

cin>>HEIGHT;

if(cin.fail()){

cin.clear(); cin.ignore(1000,' ');

cout << "Please enter a valid integer ";

continue;

}

if(HEIGHT <= 3) {

cout <<"Please enter a height > 3 ";

}else{

break;

}

}while(1);

  

int grid[ WIDTH ][ HEIGHT ];

double temp = 1;

int width = 0;

int height = 0;

int max_start_index_x = 0;

int max_start_index_y = 0;

double max_product = 0;

int what_type_of_consecutive = 0; // 1 = horizontal, 2 = vertical

// 3 = diaganol-down-right, 4 = diaganol-up-right


int i = 0;

int x = 0;

int y = 0;

int j=0;

//filling with random values less than 100

for(i=0;i<WIDTH;i++)

{

for(j=0;j<HEIGHT;j++)

{

grid[ i ][ j ] = rand()%100;

}

}

i=0;

j=0;



//Searches from left to right.

for ( y = 0; y < HEIGHT; y++ )

{

for (x = 0; x <= WIDTH - NUMBER_OF_CONSECUTIVE_DIGITS; x++)

{

for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++)

{

temp *= grid [x + i ] [ y ];

}


if ( temp > max_product )

{

max_product = temp;

max_start_index_x = x;

max_start_index_y = y;

what_type_of_consecutive = 1;

}


temp = 1;

}

}


temp = 1;


//Searches from top-bottom

for ( x = 0; x < WIDTH; x++ )

{

for ( y = 0; y <= HEIGHT - NUMBER_OF_CONSECUTIVE_DIGITS; y++ )

{

for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )

{

temp *= grid[ x ][ y + i ];

}


if ( temp > max_product )

{

max_product = temp;

max_start_index_x = x;

max_start_index_y = y;

what_type_of_consecutive = 2;

}


temp = 1;

}

}


temp = 1;

//Diagonal Down-right

for ( x = 0; x <= WIDTH - NUMBER_OF_CONSECUTIVE_DIGITS; x++ )

{

for ( y = 0; y <= HEIGHT - NUMBER_OF_CONSECUTIVE_DIGITS; y++ )

{

for (i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )

{

temp *= grid[ x + i][ y + i ];

}


if ( temp > max_product )

{

max_product = temp;

max_start_index_x = x;

max_start_index_y = y;

what_type_of_consecutive = 3;

}


temp = 1;

}

}


temp = 1;

//Diagonal up-right

for ( x = 0; x <= WIDTH - NUMBER_OF_CONSECUTIVE_DIGITS; x++ )

{

for ( y = HEIGHT - 1; y >= NUMBER_OF_CONSECUTIVE_DIGITS - 1; y-- )

{

for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )

{

temp *= grid[ x + i ][ y - i ];

}


if ( temp > max_product)

{

max_product = temp;

max_start_index_x = x;

max_start_index_y = y;

what_type_of_consecutive = 4;

}


temp = 1;

}

}


if ( what_type_of_consecutive != 0 )

{

cout.setf( ios_base::fixed, ios_base::floatfield );

cout.precision( 0 );


cout << " The greatest product for " << NUMBER_OF_CONSECUTIVE_DIGITS << " consecu tive digits is: " << max_product << " ";

cout << "It starts at point: (" << max_start_index_x + 1 << ", " << max_start_index_y + 1 << ") and the digits are in ";


switch( what_type_of_consecutive )

{

case 1:

cout << "horizontal order. ";

cout << "The digits are: ";


for ( i = max_start_index_x; i < max_start_index_x + NUMBER_OF_CONSECUTIVE_DIGITS; i++ )

{

cout << grid[ i] [ max_start_index_y ] << " ";

}

cout << " ";


break;


case 2:

cout << "vertical order. ";

cout << "The digits are: ";


for ( i = max_start_index_y; i < max_start_index_y + NUMBER_OF_CONSECUTIVE_DIGITS; i++ )

{

cout << grid[ max_start_index_x ][ i ] << " ";

}

cout << " ";

break;


case 3:

cout << "diagonal-down-right order. ";

cout << "The digits are: ";


for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++ )

{

cout << grid[ max_start_index_x + i ][ max_start_index_y - i ] << " ";

}

cout << " ";

break;


case 4:

cout << "diagonal-up-right order. ";

cout << "The digits are: ";


for ( i = 0; i < NUMBER_OF_CONSECUTIVE_DIGITS; i++)

{

cout << grid[ max_start_index_x + i ][ max_start_index_y - i ] << " ";

}

cout << " ";

break;

}

}

else

{

cout << " The grid is not big enough to find a number with " << NUMBER_OF_CONSECUTIVE_DIGITS;

cout << " consecutive digits. ";

}

cout << " ";

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