简体   繁体   中英

C++ Find min and max in each row and column in two-dimensional array

I am trying to find min (by row) and max (by column) element in two-dimensional (4,4) array and then store them in new array (5,5).

That is how it should look for new array (5,5):

1 2 3 4 min
5 6 7 8 min
4 4 4 5 min
3 5 5 6 min
m m m m  0

*m - max

Here it is the entire code:

#include <iostream>
using namespace std;
int main() {
    int A[4][4];/*First array*/
    int i, j;


    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++) {
            cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
            cin >> A[i][j];
        }

    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++)
            cout << A[i][j] << "\t";

        cout << "\n";
    }
    {
        int min[4];/* find min on each row*/
        for (i = 0; i < 4; i++) {
            min[i] = A[0][i];
            for (j = 1; j < 4; j++) {
                if (min[i] > A[i][j])
                    min[i] = A[i][j];
            }
        }
        int newarr[5][5];/* here i create the new array 5,5)*/
        int max[5] = { 1,2,3,4,5 };
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                newarr[i][j] = A[i][j];
                newarr[i][5] = max[i];
            }
        }

        for (j = 0; j < 4; j++)
            newarr[5][j] = min[j];
        cout << newarr[5][j] << "\t";
        cout << "\n";
}

}

I put random elements to max. Because so far I only test. But once I started my program it show correct only the first array. And where should be the new array it shows zero. Here it is the outcome of the debugging:

5   4   3   1   
5   6   7   9   
4   2   3   9   
4   8   4   6   
0   

How to fix it? And how to put zero in the last element (as you can see in the first table for the new array).

You can do it in a single pass over all the elements:

// returns a (rows+1, cols+1) matrix
int* make_min_max_vectors(const int* arr, size_t rows, size_t cols) {
    size_t out_size = (rows+1) * (cols+1);
    int* res = malloc(out_size * sizeof(int));

    // set up initial values in the right/bottom vectors
    res[out_size - 1] = 0;

    for (size_t col = 0; col < cols; ++col)
        res[rows*(cols+1) + col] = INT_MIN;

    for (size_t row = 0; row < rows; ++row)
        res[row*(cols+1) + cols] = INT_MAX;

    for (size_t row = 0; row < rows; ++row)
        for (size_t col = 0; col < cols; ++col) {
            const int* cell = &arr[row*cols + col];

            res[row*(cols+1) + col] = *cell; // copy
            if (*cell < res[row*(cols+1) + cols]) // min
                res[row*(cols+1) + cols] = *cell;
            if (*cell < res[rows*(cols+1) + col]) // max
                res[rows*(cols+1) + col] = *cell;
        }
    }
    return res;
}

That is, you simply run over all the input elements once, copying each one to the output plus checking if each one is less than its row minimum or greater than its column maximum. You don't need temporary vectors for min and max, and you don't need to run over the entire input twice.

John Swincks answer is awesome (+1'd) and I would definitely recommend his answer simply for future proofing your code. I am relatively new to programming myself and understand how intimidating the above code can seem (especially malloc ). Because of this I have written the a version of your code that is very similar however gets rid of the need to have the original 4x4 matrix and instead uses a 5x5. The code is shown below, let me know if you have any issue with it. The code can be compiled as is and will demonstrate what you are trying to acheive.

#include <iostream>

int main()
{
    // Initialised inline to simplify example.
    int A[5][5] = {0};

    // Only uses 4x4 of the 5x5 array meaning that you don't need to use two arrays.
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            std::cout << "Enter in value for row " << x << ", column " << y << ".\n";
            std::cin >>  A[x][y];
        }
    }

    std::cout << "Input:" << std::endl;
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            std::cout << A[x][y] << "\t";
        }
        std::cout << "\n";
    }

    // Finds the max down each column in the array
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            if (A[x][4] < A[x][y])
                A[x][4] = A[x][y];
        }
    }

    // Finds the min across the array (along row) 
    for (int y = 0; y < 4; ++y)
    {
        for (int x = 0; x < 4; ++x)
        {
            // Assign the min to a value in that row.
            A[4][y] = A[1][y];
            if (A[4][y] > A[x][y])
                A[4][y] = A[x][y];
        }
    }

    std::cout << "Output:" << std::endl;
    for (int x = 0; x < 5; ++x)
    {
        for (int y = 0; y < 5; ++y)
        {
            std::cout << A[x][y] << "\t";
        }
        std::cout << "\n";
    }

    std::cin.get();
    std::cin.get();
    return 0;
}

Edit: Sample input and output for clarification.

Input:
1       2       3       4
5       62      4       6
8       9       1       2
4       6       8       9
Output:
1       2       3       4       1
5       62      4       6       4
8       9       1       2       1
4       6       8       9       4
8       62      8       9       0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM