Write a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two dimensional array with row and column as int type and maxValue as double type. The class also contains a constructor Location(row, column, maxValue) for creating an instance with the specified row, column, and maxValue.
Write the following function that returns the location of the largest element in a two-dimensional array. Assume that the column size is fixed.
```
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
Location locateLargest(const double a[][COLUMN_SIZE]);
```
The return value is an instance of Location.
Write a test program that prompts the user to enter a 3 by 4 two-dimensional array and displays the location of the largest element in the array. Here is a sample run:
```
```
#include
using namespace std;
class Location
{
public:
Location(int r, int c, double m)
{
row = r;
column = c;
maxValue = m;
}
int row;
int column;
double maxValue;
};
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
Location locateLargest(const double a[][COLUMN_SIZE])
{
int row = 0;
int column = 0;
double maxValue = a[0][0];
for (int i = 0; i < ROW_SIZE; i++)
for (int j = 0; j < COLUMN_SIZE; j++)
if (maxValue < a[i][j])
{
row = i;
column = j;
maxValue = a[i][j];
}
return Location(row, column, maxValue);
}
int main()
{
double p[ROW_SIZE][COLUMN_SIZE];
cout << "Enter a 3 by 4 two-dimensional array:";
for (int i = 0; i < ROW_SIZE; i++)
for (int j = 0; j < COLUMN_SIZE; j++)
cin >> p[i][j];
Location loc = locateLargest(p);
cout << "The location of the largest element is " <<
loc.maxValue << " at (" << loc.row << ", " << loc.column << ")" << endl;
return 0;
}
(b)
#include
#include
using namespace std;
bool isConsecutiveFour(vector
{
for (int i = 0; i < values.size() - 3; i++)
{
bool isConsecutive = true;
for (int j = i; j < i + 3; j++)
{
if (values[j] != values[j + 1])
{
isConsecutive = false;
break; // No consecutive sequence starting at i
}
}
if (isConsecutive) return true;
}
return false;
}
int main()
{
cout << "Enter the number of values: ";
int size;
cin >> size;
vector
cout << "Enter the values: ";
for (int i = 0; i < values.size(); i++)
cin >> values[i];
cout << (isConsecutiveFour(values) ? "true" : "false") << endl;
return 0;
}
bool isConsecutiveFour(vector
{
int count = 0;
for (int i = 1; i < values.size(); i++)
{
if (values[i - 1] == values[i])
count++;
else
count = 0;
if (count == 4) return true;
}
return false;
}
```
You might also like to view...
Since its launch in 2006, Twitter has become the number-two social media site preceded by LinkedIn
Indicate whether the statement is true or false
________ Software allows you to forensically search for data on your entire network using nothing more than keywords or phrases
Fill in the blank(s) with correct word