我的代码正在复制二维数组元素。代码有什么问题?

My code is making duplicate copies of 2D array elements. What is wrong in the code?

我的代码主要检查 3xn 数组中的特定三角形,并按升序排列,一个三角形接一个三角形。但我现在面临的问题是,我的代码显然在创建重复项并覆盖数据。请帮我找出错误。 干杯。

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<fstream>
#include<time.h>

using namespace std;

void main()
{
system("cls");
int col;
int d = 30000;
int cons = 1;
A:
cout << endl;
int a[3][40000];//Initializing the array to 0
cout << "enter no. of columns:";//No. of Columns for the 3xN matrix
cin >> col;
int noe = col * 3;//Total No. of Elements

//Code to accept the constant multiplier
cout << "Enter the constant multiplier:";
cin >> cons;

//Code to generate a list of random integers and store it in the array 
for (int i = 0; i < col; i++)
for (int j = 0; j < 3; j++)
{
    d--;
    a[j][i] = d;
}

cout << "Elements before sort:" << endl;

for (int i = 0; i < col; i++)
for (int j = 0; j < 3; j++)
{
    cout << a[j][i] << " ";
}

int k = 0;
int e = col - 1;
int temp=0;
int temp1 = 0;//Temporary Storage


clock_t begin, end;
double time_spent;

begin = clock();

//Sorting algorithm 
for (int n = 0; n <= noe*cons; n++)
{



    if (k < col - 2)
    {
        //Sorting Upper Triangle
        //3 row check
        if (a[0][k]>a[0][k + 1])
        {
            temp = a[0][k + 1];
            a[0][k + 1] = a[0][k];
            a[0][k] = temp;
        }

        if (a[1][k] > a[1][k + 1])
        {
            temp = a[1][k + 1];
            a[1][k + 1] = a[1][k];
            a[1][k] = temp;
        }

        if (a[2][k] > a[0][k + 2])
        {
            temp = a[0][k + 2];
            a[0][k + 2] = a[2][k];
            a[2][k] = temp;
        }

        //First Column Check
        {
            if (a[0][k] > a[1][k])
            {
                temp = a[1][k];
                a[1][k] = a[0][k];
                a[0][k] = temp;
            }

            if (a[1][k] > a[2][k])
            {
                temp = a[2][k];
                a[2][k] = a[1][k];
                a[1][k] = temp;
            }

            if (a[0][k] > a[1][k])
            {
                temp = a[1][k];
                a[1][k] = a[0][k];
                a[0][k] = temp;
            }

        }
        //Second Column Check
        {
            if (a[0][k + 1] > a[1][k + 1])
            {
                temp = a[1][k + 1];
                a[1][k + 1] = a[0][k + 1];
                a[0][k + 1] = temp;
            }

            if (a[1][k + 1] > a[0][k + 2])
            {
                temp = a[0][k + 2];
                a[0][k + 2] = a[1][k + 1];
                a[1][k + 1] = temp;
            }

            if (a[0][k + 1] > a[1][k + 1])
            {
                temp = a[1][k + 1];
                a[1][k + 1] = a[0][k + 1];
                a[0][k + 1] = temp;
            }
        }

        //3 Diagonal Checks
        if (a[0][k + 1] < a[1][k])
        {
            temp = a[1][k];
            a[1][k] = a[0][k + 1];
            a[0][k + 1] = temp;
        }
        if (a[2][k] > a[1][k + 1])
        {
            temp = a[1][k + 1];
            a[1][k + 1] = a[2][k];
            a[2][k] = temp;
        }
        if (a[2][k] > a[0][k + 1])
        {
            temp = a[0][k + 1];
            a[0][k + 1] = a[2][k];
            a[2][k] = temp;
        }
        //Upper Triangle Sorted
        k++;
    }
    else k = 0;

    if (e >1)
    {
        //Sorting Lower Triangle
        //3 row check
        if (a[2][e - 2]>a[0][e])
        {
            temp1 = a[0][e];
            a[0][e] = a[2][e - 1];
            a[2][e - 2] = temp1;
        }

        if (a[1][e - 1] > a[1][e])
        {
            temp1 = a[1][e];
            a[1][e] = a[1][e - 1];
            a[1][e - 1] = temp1;
        }

        if (a[2][e - 1] > a[2][e])
        {
            temp1 = a[2][e];
            a[2][e] = a[2][e - 1];
            a[2][e - 1] = temp1;
        }

        //First Column Check
        {
            if (a[2][e - 2] > a[1][e - 1])
            {
                temp1 = a[1][e - 1];
                a[1][e - 1] = a[2][e - 2];
                a[2][e - 2] = temp1;
            }

            if (a[1][e - 1] > a[2][e - 1])
            {
                temp1 = a[2][e - 1];
                a[2][e - 1] = a[1][e - 1];
                a[1][e - 1] = temp1;
            }

            if (a[2][e - 2] > a[1][e - 1])
            {
                temp1 = a[1][e - 1];
                a[1][e - 1] = a[2][e - 2];
                a[2][e - 2] = temp1;
            }

        }

        //Second Column Check
        {
            if (a[0][e] > a[1][e])
            {
                temp1 = a[1][e];
                a[1][e] = a[0][e];
                a[0][e] = temp1;
            }

            if (a[1][e] > a[2][e])
            {
                temp1 = a[2][e];
                a[2][e] = a[1][e];
                a[1][e] = temp1;
            }

            if (a[0][e] > a[1][e])
            {
                temp1 = a[1][e];
                a[1][e] = a[0][e];
                a[0][e] = temp1;
            }
        }

        //3 Diagonal Checks
        if (a[0][e] < a[1][e - 1])
        {
            temp1 = a[1][e - 1];
            a[1][e - 1] = a[0][e];
            a[0][e] = temp1;
        }
        if (a[2][e - 1] > a[1][e])
        {
            temp1 = a[1][e];
            a[1][e] = a[2][e - 1];
            a[2][e - 1] = temp1;
        }
        if (a[2][e - 1] > a[0][e])
        {
            temp1 = a[0][e];
            a[0][e] = a[2][e - 1];
            a[2][e - 1] = temp1;
        }
        //Lower Triangle Sorted
        e--;

    }
    else e = col - 1;



}

end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;


//Code to print the sorted elements
cout << "Sorted Elements:" << endl;

for (int i = 0; i < col; i++)
for (int j = 0; j < 3; j++)
{
    cout << a[j][i] << " ";
}

//Code to check if the elements are sorted or not
int l = 0;
int s = a[0][0];
for (int i = 0; i < col; i++)
for (int j = 0; j < 3; j++)
{
    if (s > a[j][i])l++;
    s = a[j][i];
}

if (l == 0)cout << "\nSorted Array!\n";
else cout << "\nUnsorted!\n";

cout << time_spent;
cout << endl;

system("pause");
goto A;

}

我不明白您的订购需求,但至少您应该使用 std::swap() 而不是所有手动交换,例如:

#include <algorithm>
...

    if (a[0][k] > a[0][k + 1]) std::swap(a[0][k], a[0][k + 1]);

这将使您的代码更短、更易于阅读,更重要的是修复您手动交换中的任何错误。索引错误太容易了,而且很难发现它。