Pointers/Memory Allocation   «Prev  Next»
Lesson 16 Dynamic multidimensional arrays
Objective C++ main() function

main() function of the C++ dynamic multidimensional array program.

Here is main(). Notice that findmax() is applied to different-sized dynamically allocated structures.
#include <iostream.h>
int main(){
  twod a, b;
  int  i, j;
  // ----- function call
  allocate(2, 3, a);
  allocate(4, 6, b);
  for (i = 0; i < a.column_size; ++i)
     for (j = 0; j < a.row_size; ++j)
        a.base[i][j] = i * j ;
  for (i = 0; i < b.column_size; ++i)
     for (j = 0; j < b.row_size; ++j)
        b.base[i][j] = i * j ;
  cout << findmax(a) <<
    " max in size 2 * 3 " << endl;
  cout << findmax(b) <<
    " max in size 4 * 6 " << endl;
}

Review of the main() function

int main()

This is called the function header, which identifies the function. Here, int is a type name that defines the type of value that the main() function returns when it finishes execution, namely an integer. In general, the parentheses following a name in a function definition enclose the specification for information to be passed to the function when you call it. There is nothing between the parentheses in this instance but there could be. You will learn how you specify the type of information to be passed to a function when it is executed. I will always put parentheses after a function name in the text to distinguish it from other things that are code. The executable code for a function is always enclosed between braces and the opening brace follows the function header.

C++ Program with main() function that calls findmax() function.

Below is an example of a C++ program with a `main()` function that calls a `findmax()` function. The `findmax()` function contains a dynamically sized multi-dimensional array and uses two nested `for` loops to find the maximum value in the array.
#include <iostream>
#include <vector>

// Function to find the maximum value in a dynamically sized 2D array
int findmax(const std::vector<std::vector<int>>& arr) {
    int maxVal = arr[0][0]; // Assume the first element is the maximum

    // Nested for loops to iterate through the 2D array
    for (size_t i = 0; i < arr.size(); ++i) {
        for (size_t j = 0; j < arr[i].size(); ++j) {
            if (arr[i][j] > maxVal) {
                maxVal = arr[i][j]; // Update maxVal if a larger value is found
            }
        }
    }
    return maxVal; // Return the maximum value
}

int main() {
    // Example dynamically sized 2D array (vector of vectors)
    std::vector<std::vector<int>> array = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    // Call the findmax() function and store the result
    int maxValue = findmax(array);

    // Output the result
    std::cout << "The maximum value in the array is: " << maxValue << std::endl;

    return 0;
}

Explanation:
  1. findmax() Function
    • Takes a 2D vector (std::vector>) as input.
    • Initializes maxVal with the first element of the array.
    • Uses two nested for loops to iterate through all elements of the array.
    • Updates maxVal if a larger value is found.
    • Returns the maximum value.
  2. main() Function
    • Defines a dynamically sized 2D array using a vector of vectors.
    • Calls the findmax() function and passes the array as an argument.
    • Prints the maximum value returned by findmax().

Output: For the given example array:
1 2 3
4 5 6
7 8 9
The program will output:
The maximum value in the array is: 9

Key Points:
  • The use of std::vector allows for dynamic sizing of the array.
  • The nested for loops ensure that all elements of the 2D array are checked.
  • The program is flexible and works for any valid 2D vector input.

SEMrush Software