Mình cứ nghĩ, lập trình thì chỉ có đúng hoặc sai. Bây giờ mới gặp đúng trong trường hợp này nhưng sai trong trường hợp khác. Quá đau đầu với những lỗi thế này.
Em chạy thuật toán sắp xếp với mảng 5 phần tử thì được, 10 phần tử lúc được lúc không, 100 phần tử thì botay.com. Báo lỗi has triggered a breakpoint. Em không hiểu luôn.
Mọi người kiểm tra đoạn code dùm em. Em không biết đặt hàm delete []mang ở vị trí nào là đúng.
#include <iostream>
#include <time.h>
using namespace std;
void KiemTraTrangThai(int a[], int n);
void DocFile();
void SelectionSort(int a[], int n);
void XuatMang(int a[], int N);
void RandomCreate(char* tenFile, int total);
int* ReadFile(int& SoPhanTu);
void WriteFile(char* tenFile, int n, int t, int a[]);
void main()
{
RandomCreate("input.txt", 100);
int n;
int* a = ReadFile(n);
while (true)
{
// Tao menu chuc nang
cout <<"\n----------------------------------\n";
cout << "1. Doc tap tin va cho biet tinh trang ban dau cua day so\n";
cout << "2. Sap xep day so tang dan bang sap xep chon\n";
cout << "3. Sap xep day so tang dan bang sap vun dong\n";
cout << "4. Sap xep day so tang dan bang sap xep nhanh\n";
cout << "5. Sap xep day so tang dan bang sap xep tron\n";
cout << "6. Sap xep day so tang dan bang sap xep theo co so\n";
cout << "7. Sap xep day so giam dan bang sap xep vun dong\n";
cout << "8. Thoat chuong trinh\n\n";
cout << "Moi ban chon chuc nang: ";
int chuc_nang;
cin >> chuc_nang;
if (chuc_nang ==2)
{
cout << "\nSap xep day so tang dan bang sap xep chon\n";
clock_t t = clock();
SelectionSort(a, n);
t = clock() - t;
t = ((float)t)*1000/CLOCKS_PER_SEC;
XuatMang(a, n);
cout << "Thoi gian thuc hien: " << t << endl;
WriteFile("SelectionSort.txt", n, t, a);
}
else
{
break;
}
}
system("pause");
//delete []a;
}
bool KiemTraTang(int a[], int n)
{
bool kt = false;
for (int i = 0; i < n-1; i++)
{
if (a[i] <= a[i+1])
{
kt = true;
}
else
{
kt = false;
break;
}
}
return kt;
}
bool KiemTraGiam(int a[], int n)
{
bool kt = false;
for (int i = 0; i < n-1; i++)
{
if (a[i+1] <= a[i])
{
kt = true;
}
else
{
kt = false;
break;
}
}
return kt;
}
bool KiemTraBang(int a[], int n)
{
bool kt = false;
for (int i = 0; i < n-1; i++)
{
if (a[i+1] == a[i])
{
kt = true;
}
else
{
kt = false;
break;
}
}
return kt;
}
void KiemTraTrangThai(int a[], int n)
{
cout << "Trang thai hien tai cua day la: ";
if (KiemTraTang(a, n))
{
cout << "Day tang dan" << endl;
}
else if (KiemTraGiam(a, n))
{
cout << "Day giam dan" << endl;
}
else if (KiemTraBang(a, n))
{
cout << "Day bang nhau" << endl;
}
else
{
cout << "Day ngau nhien" << endl;
}
}
void HoanVi(int a, int b)
{
int c = a;
a = b;
b = c;
}
void Swap(int &a, int &b)
{
int c = a;
a = b;
b = c;
}
void SelectionSort(int a[], int n)
{
int min;
for (int i = 0; i < n-1; i++)
{
min = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
if (min != i)
{
swap(a[min], a[i]);
}
}
}
void XuatMang(int a[], int N)
{
cout << "Mang hien tai co " << N << " phan tu:" << endl;
for (int i = 0; i < N; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void RandomCreate(char* tenFile, int total)
{
//Mo file de ghi
FILE* f;// = fopen(tenFile, "wt");
errno_t err = fopen_s(&f, tenFile, "wt");
if( err )
printf_s( "The file input.txt was not opened\n" );
else
{
int iSecret;
/* initialize random seed: */
//srand((unsigned int)time(NULL));
srand(static_cast<unsigned int>(time(NULL)));
//Ghi du lieu ra file
fprintf(f, "%d\n", total);
for (int i = 0; i < total; i++)
{
iSecret = (rand()%10) + 10 * (rand()%10) + 100 * (rand()%10) + 1000 * (rand()%10) + 10000 * (rand()%10) + 100000 * (rand()%10);
fprintf(f, "%d ", iSecret);
}
}
//Dong file
fclose(f);
printf("\nDa tao xong file txt voi %d phan tu.", total);
}
int* ReadFile(int& SoPhanTu)
{
int * a = new int[];
//Mo file de doc
FILE* f; //= fopen(tenFile, "r+");
errno_t err = fopen_s(&f, "input.txt", "r+");
if( err )
printf_s( "The file input.txt was not opened\n" );
else
{
//Doc du lieu tu file
fscanf_s(f, "%d", &SoPhanTu);
//printf("%d\n", n);
while (!feof(f)) // Kiem tra da het file hay chua
{
for (int i = 0; i < SoPhanTu; i++)
{
fscanf_s(f, "%d ", &a[i]);
}
}
//XuatMang(a, n);
}
//Dong file
return a;
fclose(f);
}
void WriteFile(char* tenFile, int n, int t, int a[])
{
//Mo file de ghi
FILE* f;// = fopen(tenFile, "wt");
errno_t err = fopen_s(&f, tenFile, "wt");
if( err )
printf_s( "The file input.txt was not opened\n" );
else
{
//Ghi du lieu ra file
fprintf(f, "%d\n", n);
fprintf(f, "%d\n", t);
for (int i = 0; i < n; i++)
{
fprintf(f, "%d ", a[i]);
}
}
//Dong file
fclose(f);
printf("Da tao xong file\n");
}