找到具有最大负矩阵元素的列和具有最小元素的列

Find the column with the maximum negative matrix element and the column with the minimum element

我想知道具有最大负矩阵元素的列和具有最小元素的列,以便我可以重新排列它们。更具体地说,我对 The column with the maximum negative element:The column with the minimum element: 的 return 值很感兴趣,但是大约有一半的时间,关于哪些列的结果 return 是完全错误的。有趣的是,返回的结果并不总是错误的。我做错了什么?

#include <iostream>
#include <time.h>
#include <cmath>

using namespace std;

int main(void) {

    // random number for rand()
    srand(time(0));

    int m = 5;  // row count
    int n = 5;  // column count

    // declaration of a dynamic array of pointers
    double **arr = (double**) malloc(n * sizeof(double));

    // filling the array with pointers
    for (int i = 0; i < n; i++)
        arr[i] = (double*) malloc(m * sizeof(double));

    // array initialization with random numbers
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            arr[i][j] = (double) (rand() % 400 - 199) / 2.0;    // (-100.0; 100.0)

    // matrix output
    cout << "\n3[92mOriginal array:3[94m" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            printf("%5.1f ", arr[i][j]);
        cout << endl;
    }



    // array for the sums of modules of the row elements
    float *sumOfAbsolutes = (float*) malloc(m * sizeof(float));

    // Initializing the array with zeros
    for (int i = 0; i < m; i++) sumOfAbsolutes[i] = 0;

    // filling the array with the sums of element modules
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            sumOfAbsolutes[i] += abs(arr[i][j]);

    // output
    cout << "\n3[92mSums of modules of array row elements:" << endl;
    for (int i = 0; i < m; i++) 
        cout << "3[92m" << i << ": 3[94m"<< sumOfAbsolutes[i] << "   ";
    cout << "\n\n";


    // sorting
    for (int i = 0; i < (m - 1); i++)
        for (int j = i; j < m; j++)
            if (sumOfAbsolutes[i] > sumOfAbsolutes[j]) {
                double tmp = sumOfAbsolutes[i];
                sumOfAbsolutes[i] = sumOfAbsolutes[j];
                sumOfAbsolutes[j] = tmp;

                double *tmp2 = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp2;
            }

    // matrix output
    cout << "3[92mSorted array:3[94m" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            printf("%5.1f ", arr[i][j]);
        cout << endl;
    }


    int columnWithMaxNegNum = 0;        // the column with the maximal negative element
    int minNumber = 0;                  // the column with the minimum element

    // search for the maximal negative element
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] < 0 && arr[i][j] > arr[i][columnWithMaxNegNum])
                columnWithMaxNegNum = j;
    
    // minimum element search
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            if (arr[i][j] < arr[i][minNumber]) minNumber = j;


    cout << "\n3[92mThe column with the maximum negative element: 3[94m" << columnWithMaxNegNum << endl;
    cout << "3[92mThe column with the minimum element: 3[94m" << minNumber << endl;

    // rearrangement of columns
    for (int i = 0; i < m; i++) {
        double temp = arr[i][columnWithMaxNegNum];
        arr[i][columnWithMaxNegNum] = arr[i][minNumber];
        arr[i][minNumber] = temp;
    }

    cout << "\n3[92mRearrangement of columns:" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            printf("3[94m%5.1f ", arr[i][j]);
        cout << "\n3[0m";
    }

    // memory cleanup
    free(sumOfAbsolutes);
    for (int i = 0; i < n; i++)
        free(arr[i]);
    free(arr);
}

我认为这是某种作业,因为通常情况下,我们会使用向量和算法来编写更短的代码。

以下循环未找到最大负数:

// search for the maximal negative element
for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
        if (arr[i][j] < 0 && arr[i][j] > arr[i][columnWithMaxNegNum])
            columnWithMaxNegNum = j;

因为搜索依赖于行。你需要去:

double maxNegNum = -100.0;   // tbd
for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
        if (arr[i][j] < 0 && arr[i][j] > maxNegNum) {
            columnWithMaxNegNum = j;
            maxNegNum = arr[i][j]; 
        }

同样适用于最小值:

double minN=100.0;
for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
        if (arr[i][j] < minN) {
            minNumber = j;
            minN = arr[i][j];
        }

不相关:如果您选择 C++,请忘记 malloc 和 free,而是使用 new/delete 和 new[]/delete[]。这里是第一次尝试:Online demo

Caution/Limitations:

  • 如果找不到负数,则最大负值不准确。
  • 如果找到多个相等的最大负值和最小值,则只考虑第一个(从左到右再到下)。