Home > Software engineering >  The programm works well but i need to use 1 function instead of 3
The programm works well but i need to use 1 function instead of 3

Time:10-28

Written on C . I have written next code but 3 functions must be replaced by 1 and i dk how to.
The programm creates 3 arrays but only 1 function must calculate negative numbers of each column and find the max element in each column. Heres the code:

#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;
    }

}

Thats all. FUTILE INFORMATION It looks like your post is mostly code; please add some more details.

CodePudding user response:

The parameters to calc() should be the number of rows and columns in the array. Then it should use these as the limits in the for loops.

Also, since you're calculating total negative and maximum for each column, you must reset these variables each time through the column loop.

#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;
    }

}
  • Related