Hello, I have this program and I need to add a struct to my program, but I just
ID: 3537306 • Letter: H
Question
Hello,
I have this program and I need to add a struct to my program, but I just don't know how to.
For example:
struct largest_product_info {
int value;
int WIDTH;
int LENGTH;
string shape;
};
Code and explanation would be much appreciated :)
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#define NUMBER_OF_CONSECUTIVE_DIGITS 4
int main()
{
int index = 0;
int WIDTH,HEIGHT;
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_DIG ITS; 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_DIG ITS; 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_CONSECUTI VE_DIGITS;
cout << " consecutive digits. ";
}
cout << " ";
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
#define NUMBER_OF_CONSECUTIVE_DIGITS 4
#define HEIGHT 20
#define WIDTH 20
int main()
{
int index = 0;
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;
ifstream number_file( "grid.dat" );
// read all the characters into an int array
while( !number_file.eof() )
{
if ( number_file.peek() == ' ' )
{
number_file.get();
height++;
width = 0;
}
else
{
grid[ width++ ][ height ] = (number_file.get() - '0') * 10 + (number_file.get() - '0');
if ( number_file.peek() == ' ' )
{
number_file.get();
}
}
}
number_file.close();
// 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.