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

I need help creating a program where you input the number of columns and rows fr

ID: 3536937 • Letter: I

Question

I need help creating a program where you input the number of columns and rows from the command line and creates an array on the heap in a function called create_array(); Then, I have to create a function that finds the largest product of any 4 adjacent numbers in the array. They can be up,down, left, right, diagonal, zig-zag, in a box or L formation in the grid. Need find_greatest_product() which takes the array argument and returns the max product, starting position and the shape/direction of the 4 numbers. Each function, including int main() must contain less than 15 lines of code. Thank you, This is greatly appreciated, and if you can explain what you are doing, that would be great, still a basic programmer.

Explanation / Answer

/* i'm not sure this can be done using 15 line fiunctions..it will require too many functions..*/

/* i have filled the array with random numbers less than 100 , you can use input if you want*/


#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

#define NUMBER_OF_CONSECUTIVE_DIGITS 4


int main()

{

int index = 0;

int WIDTH,HEIGHT;

cout<<"Enter Width"<<endl;

cin>>WIDTH;

cout<<"Enter Height"<<endl;

cin>>HEIGHT;

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

//you can take input if you want

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

{

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

{

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

}

}

i=0;

j=0;


// search 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;

// search top down

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;

// diaganol 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;

// diaganol 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 << " consecutive 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 << "diaganol-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 << "diaganol-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 huge 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