【问题标题】:Why the array is not sorted correctly?为什么数组没有正确排序?
【发布时间】:2021-12-31 13:52:55
【问题描述】:

问题是

按升序排列数组 X (n, m) 的列。

我自己用 c++ 编写了这样的代码,但最后它并没有正确地对所有列进行排序。如果有人知道,请帮忙。

#include <iostream>
using namespace std;
int main() {
    int n, m;
    cout << "array size: n = ";
    cin >> n;
    cout << "array size: m = ";
    cin >> m;
    int array[n][m];
    int arrayS[n];
    for (int z = 0; z < n; z++) {
        for (int a = 0; a < m; a++) {
            cin >> array[z][a];
        }
    }
    for (int i = 0; i < n; i++) {
        arrayS[i] = array[i][0];
    }
    for (int y = 0; y < n; y++) {
        for (int i = 0; i < m; i++) {
            cout << array[y][i] << "    ";

        }
        cout << endl; //endline
    }
    cout << "************************************\n";
    cout << "massivin sutunlari\n";
    int enk = 0;

    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
        {
            if (arrayS[j] > arrayS[j + 1])
            {
                enk = arrayS[j];
                arrayS[j] = arrayS[j + 1];
                arrayS[j + 1] = enk;
            }
        }
    for (int i = 0; i < n; i++)
    {
        cout << arrayS[i] << "  ";
    }
}

【问题讨论】:

  • #include 使用命名空间标准; int main() { int n,m; cout>n; cout>米;整数数组[n][m];整数数组S[n]; for(int z=0; z>array[z][a]; } } for(int i = 0; i
  • coutarrayS[j+1]) { enk=arrayS[j];数组S[j]=数组S[j+1];数组S[j+1]=enk; } } for (int i=0;i
  • 请不要在cmets中发布代码或代码的图片。这对我们帮助您没有用...相反,只需将代码复制并粘贴到您的问题中,选择它并单击问题上方的“代码”按钮{}。此外,在您这样做之前,请告诉您的代码编辑器格式化您的代码,以便正确缩进。这让我们和您更容易阅读。
  • 查看console.log() 示例,了解如何轻松格式化文本代码块。
  • 一些真诚的建议:命名你的变量zyaijnm只有在你从不期望时才合适其他人可以查看您的代码。如果其他人需要查看此类代码,他们可能会建议您使代码变得不必要地难以理解。

标签: c++ arrays sorting


【解决方案1】:

首先,我们需要使您的代码可编译。您正在使用所谓的 VLA(可变长度数组),例如 int array[n][m];。 Rhese VLA 不是 C++ 语言的一部分,不会编译。我用std::vector 替换了它们,它的工作方式相同。然后我更正了代码标识(我还编辑了你的问题),然后就可以编译了。

然后它会像下面这样(但当然仍然不工作)

#include <iostream>
#include <vector>

int main() {
    int n, m;
    std::cout << "array size: n = ";
    std::cin >> n;
    std::cout << "array size: m = ";
    std::cin >> m;

    std::vector<std::vector<int>> array(n, std::vector<int>(m, 0));
    std::vector<int> arrayS(n);

    for (int z = 0; z < n; z++) {
        for (int a = 0; a < m; a++) {
            std::cin >> array[z][a];
        }
    }
    for (int i = 0; i < n; i++) {
        arrayS[i] = array[i][0];
    }
    for (int y = 0; y < n; y++) {
        for (int i = 0; i < m; i++) {
            std::cout << array[y][i] << "    ";

        }
        std::cout << std::endl; //endline
    }
    std::cout << "************************************\n";
    std::cout << "massivin sutunlari\n";
    int enk = 0;

    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
        {
            if (arrayS[j] > arrayS[j + 1])
            {
                enk = arrayS[j];
                arrayS[j] = arrayS[j + 1];
                arrayS[j + 1] = enk;
            }
        }
    for (int i = 0; i < n; i++)
    {
        std::cout << arrayS[i] << "  ";
    }
}

一些基本提示。请使用“说话”的变量名称并编写许多 cmets,最好是每行代码一个多于一个注释。那么你自己就已经看到了问题。

您的代码中存在一些很可能会使您的程序崩溃的严重错误。在你排序的地方,你使用幻数 9(无论出于何种原因),因此超出了定义数组的范围。

此外,您对冒泡排序的实现是错误的。你可以在网上查很多很多例子。

然后,您只对一列进行排序。您需要为矩阵的每一列实现冒泡排序。

下一步我将使用更好的变量名并编写 cmets。该代码仍然无法正常工作。但是看到并理解其中的区别。

#include <iostream>
#include <vector>

int main() {

    // Here we will define the dimensions for our matrix
    int numberOfRows{}, numberOfColumns{};

    // Inform user what to do. Enter number of rows of the matrix
    std::cout << "\nPlease enter the number of rows: ";

    // Read the number of rows from the user via std::cin
    std::cin >> numberOfRows;

    // Inform user what to do. Enter number of columns of the matrix
    std::cout << "\nPlease enter the number of columns: ";

    // Read the number of rows from the user via std::cin
    std::cin >> numberOfColumns;

    // Define the array for the complete 2d matrix
    std::vector<std::vector<int>> array(numberOfRows, std::vector<int>(numberOfColumns, 0));

    // We can store one column here
    std::vector<int> arrayS(numberOfRows);

    // Inform user what to do. Enter all data for the matrix
    std::cout << "\nPlease enter all data for the matrix";

    // Now read all data, for all rows and columns from the user, via std::cin
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cin >> array[row][col];
        }
    }
    // Copy the first column in a separate array
    for (int i = 0; i < numberOfRows; i++) {
        arrayS[i] = array[i][0];
    }

    // Show current data in our matrix
    std::cout << "\n\nThe matrix:\n\n";
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cout << array[row][col] << "    ";

        }
        std::cout << '\n'; //endline
    }

    // SHow some status message
    std::cout << "\n\n\n************************************\n";
    std::cout << "Sorted first column\n";

    // This is a temporary variable needed to swap 2 values
    int tempValue = 0;

    // Try to do bubble sort
    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
        {
            if (arrayS[j] > arrayS[j + 1])
            {
                tempValue = arrayS[j];
                arrayS[j] = arrayS[j + 1];
                arrayS[j + 1] = tempValue;
            }
        }

    // Show the result of the sorting
    for (int i = 0; i < numberOfRows; i++)
    {
        std::cout << arrayS[i] << "  ";
    }
}

接下来,我们修复一列的排序。

只需要小改动:

#include <iostream>
#include <vector>

int main() {

    // Here we will define the dimensions for our matrix
    int numberOfRows{}, numberOfColumns{};

    // Inform user what to do. Enter number of rows of the matrix
    std::cout << "\nPlease enter the number of rows: ";

    // Read the number of rows from the user via std::cin
    std::cin >> numberOfRows;

    // Inform user what to do. Enter number of columns of the matrix
    std::cout << "\nPlease enter the number of columns: ";

    // Read the number of rows from the user via std::cin
    std::cin >> numberOfColumns;

    // Define the array for the complete 2d matrix
    std::vector<std::vector<int>> array(numberOfRows, std::vector<int>(numberOfColumns, 0));

    // We can store one column here
    std::vector<int> arrayS(numberOfRows);

    // Inform user what to do. Enter all data for the matrix
    std::cout << "\nPlease enter all data for the matrix\n";

    // Now read all data, for all rows and columns from the user, via std::cin
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cin >> array[row][col];
        }
    }
    // Copy the first column in a separate array
    for (int i = 0; i < numberOfRows; i++) {
        arrayS[i] = array[i][0];
    }

    // Show current data in our matrix
    std::cout << "\n\nThe matrix:\n\n";
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cout << array[row][col] << "    ";

        }
        std::cout << '\n'; //endline
    }

    // SHow some status message
    std::cout << "\n\n\n************************************\n";
    std::cout << "Sorted first column\n";

    // This is a temporary variable needed to swap 2 values
    int tempValue = 0;

    // Try to do bubble sort
    // Go over all entries-1 in the column array.
    // And compare the current value with the next value
    for (int i = 0; i < numberOfRows-1; i++)
        for (int j = 0; j < numberOfRows - 1; j++)
        {
            // If this value is bigger than the following value, then swap them
            if (arrayS[j] > arrayS[j + 1])
            {
                // Swap
                tempValue = arrayS[j];
                arrayS[j] = arrayS[j + 1];
                arrayS[j + 1] = tempValue;
            }
        }

    // Show the result of the sorting
    for (int i = 0; i < numberOfRows; i++)
    {
        std::cout << arrayS[i] << "  ";
    }
}

这已经可以了。但是,我的猜测是所有列都应该排序。

然后,最后,它看起来像这样:

#include <iostream>
#include <vector>

int main() {

    // Here we will define the dimensions for our matrix
    int numberOfRows{}, numberOfColumns{};

    // Inform user what to do. Enter number of rows of the matrix
    std::cout << "\nPlease enter the number of rows: ";

    // Read the number of rows from the user via std::cin
    std::cin >> numberOfRows;

    // Inform user what to do. Enter number of columns of the matrix
    std::cout << "\nPlease enter the number of columns: ";

    // Read the number of rows from the user via std::cin
    std::cin >> numberOfColumns;

    // Define the array for the complete 2d matrix
    std::vector<std::vector<int>> array(numberOfRows, std::vector<int>(numberOfColumns, 0));

    // Inform user what to do. Enter all data for the matrix
    std::cout << "\nPlease enter all data for the matrix\n";

    // Now read all data, for all rows and columns from the user, via std::cin
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cin >> array[row][col];
        }
    }

    // Show current data in our matrix
    std::cout << "\n\nThe matrix:\n\n";
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cout << array[row][col] << "    ";

        }
        std::cout << '\n'; //endline
    }

    // SHow some status message
    std::cout << "\n\n\n************************************\n";
    std::cout << "Matrix with sorted column\n";

    // This is a temporary variable needed to swap 2 values
    int tempValue = 0;

    // Try to do bubble sort
    // Go over all entries-1 in the column array.
    // And compare the current value with the next value
    // Do for all columns
    for (int col = 0; col < numberOfColumns; ++col)
        for (int i = 0; i < numberOfRows-1; ++i)
            for (int row = 0; row < numberOfRows - 1; ++row)
            {
                // If this value is bigger than the following value, then swap them
                if (array[row][col] > array[row + 1][col])
                {
                    // Swap
                    tempValue = array[row][col];
                    array[row][col] = array[row + 1][col];
                    array[row + 1][col] = tempValue;
                }
            }

    // Show the result of the sorting
    for (int row = 0; row < numberOfRows; ++row) {
        for (int col = 0; col < numberOfColumns; ++col) {
            std::cout << array[row][col] << "    ";
        }
        std::cout << '\n'; //endline
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多