无法将排序应用于此程序

时间:2019-06-02 22:45:05

标签: c++ visual-studio sorting multidimensional-array

该程序的目标是生成一个大小可变的多维数组,列数是政党的数目,行数是我们所在的“回合”,第一个条目该行是每个政党获得的票数,然后进入第二排第二轮,将第一行中的所有值除以2,在第三轮中将第一行中的值除以3,然后同样的事情有多少行。 我完成了所有的工作,但是在这些操作结束之后,我希望能够找到N个最大的元素(n是行数),并将这些元素放在向量中,出于某种原因,我似乎找不到,当我运行它时,控制台上什么都没有显示,当我到达要它对元素​​进行排序的部分时,它会变黑,然后崩溃。

我试图改变周围的事物,我真的不认为问题出在算法本身,因为我尝试使用随机填充的静态数组来进行尝试,并且对它们进行了很好的排序。就像我说的那样,我真的不知道问题出在哪里,所以我将展示很大一部分,我已经删除了所有不相关的内容。

 double** ELEITORAL;
 void bubble_sort(double** ELEITORAL)
    {
        int x, y;
        double tItem;
        int PassCount;
        bool Mudou;
for (PassCount = 0; PassCount < (MAX_rows * MAX_columns); PassCount++)       
{
        //orders the rows
        for (y = 0; y < MAX_columns; y++)
         {
            Mudou = true;
            while (Mudou)
            {
            Mudou = false;
        for (x = 1; x < MAX_rows; x++)
            {
            if (ELEITORAL[x - 1][y] > ELEITORAL[x][y])
                {
                Mudou = true;
                tItem = ELEITORAL[x - 1][y];
                ELEITORAL[x - 1][y] = ELEITORAL[x][y];
                ELEITORAL[x][y] = tItem;

                }
            }
        }
}
        //ORDERS THE COLUMNS 
        for (x = 0; x < MAX_rows; x++)
        {
            Mudou = true;
            while (Mudou)
                {
                Mudou = false;
                for (y = 1; y < MAX_columns; y++)
                {
                if (ELEITORAL[x][y - 1] > ELEITORAL[x][y])
                    {
                        Mudou = true; 
                    tItem = ELEITORAL[x][y - 1];
                    ELEITORAL[x][y - 1] = ELEITORAL[x][y];
                    ELEITORAL[x][y] = tItem; 
                    cout << "entrei";
                    system("pause");
                        }

                    }
                }
            }
        }
    }
    void DisplayTheArray(double** ELEITORAL)
    {
        for (int y = 0; y < MAX_columns; y++)
        {
            for (int x = 0; x < MAX_rows; x++)
            {
                cout.width(5);
                cout << ELEITORAL[x][y];
            }
            cout << endl;
        }
        cout << endl;
    }
void fill(double **p, int rowsize, int colsize) {

      std::cout << std::fixed;
      std::cout << std::setprecision(1);
 printf("\n  Introduza o n%cmero de votos nas listas por ordem \n", 163);
    for (int col = 0; col < colsize; col++)
        {
         cin >> p[row][col];
        }
    cout << endl; 
    for (int row = 1; row < rowsize; row++)  
        {                                               
        for (int col = 0; col < colsize; col++) 
                {
                    p[row][col] = (p[0][col]/(row + 1));
                }
                cout << endl; //preenche as linhas 
            }
    }//FILL OUT THE ARRAY

   void print(double **p, int rowsize, int colsize) 
    {
    for (int i = 0; i < header.size(); i++) { //HEADER IS TO STORE THE 
           //NAMES OF THE POLITICAL PARTIES
            cout << setw(9) << header[i] << " ";
        }
        cout << endl;
        for (row = 0; row < rowsize; row++) //IMPRIME A MATRIZ EM SI
        {
            for (col = 0; col < colsize; col++)
               {
                   cout << setw(10) << p[row][col];

               }
            cout << endl;

     }
   }//PRINTS THE ARRAY

    int MATRIZ()
    {
        std::cout << std::fixed;
        std::cout << std::setprecision(1);
        double rows, columns;
        printf("\n Qual o n%cmero de candidatos a eleger? \n", 163);
        cin >> rows;
        printf("\n Qual o n%cmero de listas candidatas? \n", 163);
        cin >> columns;
        for (i = 0; i < columns; i++)
            {
                cout << " Qual o nome da lista " << i+1;
                cout << endl;
                cin >> nomL;
                header.push_back(nomL);
            }
        cout << endl;
        system("cls");
        ELEITORAL = new double*[rows];
        for (int row = 0; row < rows; row++)
         {
            ELEITORAL[row] = new double[columns];
         }
         fill(ELEITORAL, rows, columns);
         cout << endl;
         //After this I have a switch, in case 5 I call the functions 
         //giving me trouble
case '5':
        {
            bubble_sort(ELEITORAL);
            DisplayTheArray(ELEITORAL);
            break;
        }

1 个答案:

答案 0 :(得分:1)

您的显示函数是粗略的,因为您假设数组的尺寸为MAX。所以我敢打赌,由于内存访问冲突,您的程序段错误。请不要使用new,而要使用vector。您不必将长度作为单独的参数传递,在这种情况下,您不会弄错它。同样,double也不适合用于索引编制,请打开编译器警告并加以修正。

下面是一个简单的示例,如何使用vector来创建矩阵:

#include <iostream>
#include <vector>


using row_t = std::vector<double>;
using matrix_t = std::vector<row_t>;
//matrix_t = std::vector<std::vector<double>>;  Vector of vectors of double


//Pass by reference to avoid copy
//Pass by const if the matrix should be read-only
void printMatrix(const matrix_t& mat)
{
    for(auto& row: mat)
        for(auto& val: row)
            std::cout<<val << ' ';
        std::cout<<'\n';//Use this instead of endl if you want just a new line
}

void doubleMatrix(matrix_t& mat)
{
    //If you need access to indices during iterating
    for(std::size_t r = 0; r < mat.size();++r)
        for(std::size_t c = 0; c < mat[r].size();++r)
            mat[r][c] *=2.0;
}
int main()
{
    std::size_t rows, columns;

    std::cin >> rows;
    std::cin >> columns;

    double fill_value = 0.0;

    matrix_t matrix(rows);
    //Resize rows to given column width.
    for(auto& row: matrix)
        row.resize(columns, fill_value);

    printMatrix(matrix);
    doubleMatrix(matrix);
    printMatrix(matrix);
}

最后我注意到,如果要交换两个值,请使用std::swap