如何将三个功能合并为一个?

How can I merge three functions into one?

我写了下一个代码,但是 3 个函数必须用 1 个替换,我不知道如何替换。

程序创建了 3 个数组,但只有 1 个函数必须计算每列的负数并找到每列中的最大元素。这是代码:

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;

void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
    float** array = new float* [m];
    for (int l = 0; l < m; l++) {
        array[l] = new float[k];
    }
    // заполнение массива
    srand(time(0));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < k; j++) {
            array[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < k; j++) {
            cout << setprecision(2) << setw(4) << array[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The amount of negative elements in each column: ";
    calc(array, i, j);  // FUNCTION !!!
    float** arr = new float* [b];
    for (int l = 0; l < b; l++) {
        arr[l] = new float[b];
    }
    // заполнение массива
    srand(time(0));
    for (int i = 0; i < b; i++) {
        for (int j = 0; j < b; j++) {
            arr[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < b; i++)
    {
        for (int j = 0; j < b; j++) {
            cout << setprecision(2) << setw(4) << arr[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The amount of negative elements in each column: ";
    calc(arr, i, j); // FUNCTION !!!
    float** ar = new float* [u];
    for (int l = 0; l < u; l++) {
        ar[l] = new float[u];
    }
    // заполнение массива
    srand(time(0));
    for (int i = 0; i < u; i++) {
        for (int j = 0; j < u; j++) {
            ar[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < u; i++)
    {
        for (int j = 0; j < u; j++) {
            cout << setprecision(2) << setw(4) << ar[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The amount of negative elements in each column: ";
    calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
    int max = array[0][0];
    for (int j = 0; j < k; j++)
    {
        max = array[0][0];
        for (int i = 0; i < k; i++) {
            if (array[i][j] > max)
                max = array[i][j];
            if (array[i][j] < 0) {
                n += 1;
            }
        }
        cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
        cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
    }

}
void calc1(float** arr, int i, int j) {
    int max = arr[0][0];
    for (int j = 0; j < b; j++)
    {
        max = arr[0][0];
        for (int i = 0; i < b; i++) {
            if (arr[i][j] > max)
                max = arr[i][j];
            if (arr[i][j] < 0) {
                n += 1;
            }
        }
        cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
        cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
    }

}
void calc2(float** ar, int i, int j) {
    int max = ar[0][0];
    for (int j = 0; j < u; j++)
    {
        max = ar[0][0];
        for (int i = 0; i < u; i++) {
            if (ar[i][j] > max)
                max = ar[i][j];
            if (ar[i][j] < 0) {
                n += 1;
            }
        }
        cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
        cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
    }

}

calc()的参数应该是数组的行数和列数。然后它应该使用这些作为 for 循环中的限制。

此外,由于您要计算每列的总负值和最大值,因此每次通过列循环都必须重置这些变量。

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;

const int m = 3, k = 3, b = 4, u = 5;

void calc(float** array, int rows, int cols);

int main()
{
    float** array = new float* [m];
    for (int l = 0; l < m; l++) {
        array[l] = new float[k];
    }
    // заполнение массива
    srand(time(0));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < k; j++) {
            array[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < k; j++) {
            cout << setprecision(2) << setw(4) << array[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The amount of negative elements in each column: ";
    calc(array, m, k);  // FUNCTION !!!
    
    float *arr = new float* [b];
    for (int l = 0; l < b; l++) {
        arr[l] = new float[b];
    }
    // заполнение массива
    srand(time(0));
    for (int i = 0; i < b; i++) {
        for (int j = 0; j < b; j++) {
            arr[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < b; i++)
    {
        for (int j = 0; j < b; j++) {
            cout << setprecision(2) << setw(4) << arr[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The amount of negative elements in each column: ";
    calc(arr, b, b); // FUNCTION !!!

    float** ar = new float* [u];
    for (int l = 0; l < u; l++) {
        ar[l] = new float[u];
    }
    // заполнение массива
    srand(time(0));
    for (int i = 0; i < u; i++) {
        for (int j = 0; j < u; j++) {
            ar[i][j] = rand() % 21 - 10;
        }
    }
    cout << "The initial array is: " << endl << endl;
    for (int i = 0; i < u; i++)
    {
        for (int j = 0; j < u; j++) {
            cout << setprecision(2) << setw(4) << ar[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl << "The amount of negative elements in each column: ";
    calc(ar, u, u); // FUNCTION !!!
}

void calc(float** array, int rows, int cols) {

    for (int j = 0; j < cols; j++)
    {
        int n = 0;
        int max = array[0][j];
        for (int i = 1; i < rows; i++) {
            if (array[i][j] > max)
                max = array[i][j];
            if (array[i][j] < 0) {
                n += 1;
            }
        }
        cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
        cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
    }

}