Lesson 15 | Dynamic multidimensional arrays |
Objective | C++ findmax() function |
Examine function of C++ dynamic multidimensional array
The following is a class in C++20 that utilizes the findmax function you've provided. Here's how we can structure this:
#include <vector>
#include <stdexcept>
// Assuming 'twod' is a structure or class for a 2D array.
// Here's a simple implementation for our purposes:
struct twod {
std::vector<std::vector<double>> base;
int row_size;
int column_size;
twod(int rows, int cols) : row_size(rows), column_size(cols) {
base.resize(rows, std::vector<double>(cols, 0.0)); // Initialize with zeros
}
// Accessor for elements
double& operator()(int row, int col) {
if (row < 0 || row >= row_size || col < 0 || col >= column_size) {
throw std::out_of_range("Matrix indices out of range");
}
return base[row][col];
}
};
// The function to find the maximum value in the matrix
double findmax(const twod& m) {
double max = m.base[0][0];
for (int i = 0; i < m.row_size; ++i) {
for (int j = 0; j < m.column_size; ++j) {
if (m.base[i][j] > max) {
max = m.base[i][j];
}
}
}
return max;
}
// Class that makes use of the findmax function
class MatrixOperations {
private:
twod matrix;
public:
// Constructor
MatrixOperations(int rows, int cols) : matrix(rows, cols) {}
// Method to set a value in the matrix
void set(int row, int col, double value) {
matrix(row, col) = value;
}
// Method to get the maximum value from the matrix using findmax
double getMax() const {
return findmax(matrix);
}
// Method to print the matrix (for verification)
void printMatrix() const {
for (int i = 0; i < matrix.row_size; ++i) {
for (int j = 0; j < matrix.column_size; ++j) {
std::cout << matrix.base[i][j] << " ";
}
std::cout << std::endl;
}
}
};
// Example usage in main function
#include <iostream>
int main() {
MatrixOperations mat(3, 3);
mat.set(0, 0, 1.0);
mat.set(1, 1, 5.0);
mat.set(2, 2, 3.0);
std::cout << "Matrix:" << std::endl;
mat.printMatrix();
std::cout << "Maximum value in the matrix: " << mat.getMax() << std::endl;
return 0;
}
Explanation:
- twod Structure: I've defined twod as a structure that uses std::vector to represent a 2D matrix. This gives us dynamic sizing capabilities.
- MatrixOperations Class:
- It has a twod member for storing the matrix.
- set method allows you to input values into the matrix.
- getMax uses the findmax function to find the maximum value in the matrix.
- printMatrix is added for demonstration purposes to show the matrix contents.
This setup leverages the findmax function to perform operations on a matrix encapsulated within the MatrixOperations class. Remember, due to the nature of C++ and the lack of runtime execution here, the output of printMatrix and getMax would need to be imagined or tested in an actual C++ environment.
Program that finds the maximum array element
The findmax()
function is a canonical routine for processing such dynamic two-dimensional arrays.
double findmax(const twod& m){
int i, j;
double max = m.base[0][0];
for (i = 0; i < m.column_size; ++i)
for (j = 0; j < m.row_size; ++j)
if (m.base[i][j] > max)
max = m.base[i][j];
return (max);
}
C++ How to Program
This function works on arbitrary two-dimensional arrays. It finds the maximum element within a given two-dimensional array.
The findmax()
function is not tied to a particular linear layout of memory, which would be the case for two-dimensional arrays declared automatically on the stack.
Getting the Minimum and Maximum Values for a Numeric Type
Problem: You need to know the largest or smallest representable value for your platform for a numeric type, such as an int or double.
Solution: Use the numeric_limits class template in the <limits> header to get, among other
things, the largest and smallest possible values for a numeric type (see Example 4-10).
Example 4-10. Getting numeric limits
#include <iostream>
#include <limits>
using namespace std;
template<typename T>
void showMinMax( ) {
cout << "min: " << numeric_limits<T>::min( ) << endl;
cout << "max: " << numeric_limits<T>::max( ) << endl;
cout << endl;
}
int main( ) {
cout << "short:" << endl;
showMinMax<short>( );
cout << "int:" << endl;
showMinMax<int>( );
cout << "long:" << endl;
showMinMax<long>( );
cout << "float:" << endl;
showMinMax<float>( );
cout << "double:" << endl;
showMinMax<double>( );
cout << "long double:" << endl;
showMinMax<long double>( );
cout << "unsigned short:" << endl;
showMinMax<unsigned short>( );
cout << "unsigned int:" << endl;
showMinMax<unsigned int>( );
cout << "unsigned long:" << endl;
showMinMax<unsigned long>( );
}
Finally, let us look at the
main()
function that ties all the functions we have looked at together.