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

发布于 2025-01-17 06:54:38 字数 3879 浏览 0 评论 0原文

我想知道具有最大负矩阵元素的列和具有最小元素的列,以便我可以重新排列它们。更具体地说,我对 具有最大负元素的列:具有最小元素的列: 的返回值感兴趣,但大约一半的情况下,结果约为它们返回的列完全错误。有趣的是,结果并不总是错误的。我做错了什么?

#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 << "\n\033[92mOriginal array:\033[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 << "\n\033[92mSums of modules of array row elements:" << endl;
    for (int i = 0; i < m; i++) 
        cout << "\033[92m" << i << ": \033[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 << "\033[92mSorted array:\033[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 << "\n\033[92mThe column with the maximum negative element: \033[94m" << columnWithMaxNegNum << endl;
    cout << "\033[92mThe column with the minimum element: \033[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 << "\n\033[92mRearrangement of columns:" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            printf("\033[94m%5.1f ", arr[i][j]);
        cout << "\n\033[0m";
    }

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

I want to know the column with the maximum negative matrix element and the column with the minimum element so that I can rearrange them. More specifically, I'm interested in return values of The column with the maximum negative element: and The column with the minimum element: But about half the time, the results about which columns they are return completely wrong. The interesting thing is that the results don't always come back wrong. What am I doing wrong?

#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 << "\n\033[92mOriginal array:\033[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 << "\n\033[92mSums of modules of array row elements:" << endl;
    for (int i = 0; i < m; i++) 
        cout << "\033[92m" << i << ": \033[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 << "\033[92mSorted array:\033[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 << "\n\033[92mThe column with the maximum negative element: \033[94m" << columnWithMaxNegNum << endl;
    cout << "\033[92mThe column with the minimum element: \033[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 << "\n\033[92mRearrangement of columns:" << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            printf("\033[94m%5.1f ", arr[i][j]);
        cout << "\n\033[0m";
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2025-01-24 06:54:38

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

以下循环找不到最大负值:

// 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[] 代替。这里是第一次尝试:在线演示

注意/限制:

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

I assume that this is some kind of homework, since normally, we'd use vectors and algorithms to write much shorter code.

The following loop doesn't find the maximal negative:

// 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;

Because the the search is dependent of the line. You'd need to go for:

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

The same applies for the minimum :

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

Not related: if you go for C++ forget malloc and free, and use new/delete and new[]/delete[] instead. Here a first attempt: Online demo

Caution/Limitations:

  • If by no negative number are found, the maximum negative value would be inaccurate.
  • If several equal maximum negative values and minimum values are found, only the first one( from left to right and to bottom) will be considered.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文