我的代码基本上检查3xn数组中的特定三角形并按升序排序,三角形除以三角形。但我现在面临的问题是,我的代码显然是创建重复数据并覆盖数据。请帮我弄清楚这个bug。 干杯。
#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;
}
答案 0 :(得分:1)
I don't understand your ordering needs but at the very least you should be using std::swap()
instead of all your manual swaps, like:
#include <algorithm>
...
if (a[0][k] > a[0][k + 1]) std::swap(a[0][k], a[0][k + 1]);
It will make your code much shorter, easier to read, and more importantly fix any mistakes in your manual swaps. It is far too easy to get an index wrong and far to hard to spot it.