Mình có 1 BTL môn KTLT,mình đã viết code rùi nhưng khi chạy,nhập xong mảng giá trị và chọn hàm để thự hiện thì xuất hiện lỗi Floating Point:invalid.
Mong các Pro chỉ giúp
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void swap(double *max,int i,int j);
class Array
{
private:
double *max;
int count;
public:
void show()
{
int count;
for(int i=0;i<count;i++)
cout<<max[i];
}
void nhap_gt(double *max,int count);
void bubble_sort(double *max,int count);
void insertion_sort(double *max,int count);
void selection_sort(double *max,int count);
double find_max(double *max,int count);
double find_min(double *max,int count);
double gt_tb(double *max,int count);
double dolech_2pt(double *max,int count);
double dolech_tb(double *max,int count);
int sequential_search(double *max,int count,double key);
};
void swap(double *max,int i,int j)
{
double tg;
tg=max[i];
max[i]=max[j];
max[j]=tg;
}
//Gan gia tri cho cac phan tu
void Array::nhap_gt(double *max,int count)
{
cout<<"\nso luong phan tu trong mang:";
cin>>count;
cout<<"\nDanh sach phan tu trong mang:";
for(int i=0;i<count;i++)
{
cout<<"\n max["<<i<<"]=";
cin>>max[i];
}
}
// Bubble sort
void Array::bubble_sort(double *max,int count)
{
for(int i=0;i<count-1;i++){
for(int j=count;j>i;j--) {
if(max[j-1]>max[j])
swap(max,j,j-1); }
}
}
// Insertion sort
void Array::insertion_sort(double *max,int count)
{
double tg;
int i,j;
for(i=0;i< count;i++)
{
tg=max[i];
j=i-1;
while((j>=0)&&(max[j]>tg))
{
max[j+1]=max[j];
j--;
}
max[j+1]=tg;
}
}
// Selection sort
void Array::selection_sort(double *max,int count)
{
int min=0;
for(int i=0;i<count-1;i++)
min=i;
for(int j=i+1;j<count;j++)
{
if(max[j]<max[min])
{
min=j;
}
}
if(min!=i)
swap(max,i,min);
}
//tim gia tri lon nhat va dua ra vi tri
double Array::find_max(double *max,int count)
{
double gt_max=max[0];
for(int i=0;i<count;i++)
if(max[i]>gt_max)
{
gt_max=max[i];
}
return max[i];;
}
// tim gia tri nho nhat va dua ra vi tri
double Array::find_min(double *max,int count)
{
double gt_min=max[0];
for(int i=0;i<count;i++)
if(max[i]<gt_min)
{
gt_min=max[i];
}
return gt_min;
}
// tim gia tri trung binh
double Array::gt_tb(double *max,int count)
{
double tb=0.0;
for(int i=0;i<count;i++)
{
tb+=max[i];
}
tb/=count;
return tb;
}
//tim do lech giua 2 phan tu
double Array::dolech_2pt(double *max,int count)
{
double s=0;
for(int i=0;i<count;i++)
for(int j=0;j<count;j++)
{
s+=fabs(max[i]-max[j]);
}
return s;
}
// do lech trung binh giua cac gia tri trong mang
double Array::dolech_tb(double *max,int count)
{
double h[100],do_lech=0.0,tb=0.0;
for(int i=0;i<count;i++)
{
tb+=max[i];
}
tb/=count;
for(i=0;i<count;i++)
{
h[i]=fabs(max[i]-tb);
do_lech+=h[i];
}
do_lech/=count;
return do_lech;
}
// tim kiem tuan tu
int Array::sequential_search(double *max,int count,double key)
{
for(int i=0;i<count;i++)
{
if(max[i]==key)
return i;
}
return -1;
}
void main()
{
Array d;
double *max,key;
int count,i,j,ss;
max=new double[count+1];
d.nhap_gt(max,count);
cout<<"\nnhan phim 1 de thuc hien Buuble_sort";
cout<<"\nnhan phim 2 de thuc hien insertion_sort";
cout<<"\nnhan phim 3 de thuc hien selection_sort";
cout<<"\nnhan phim 4 de dua ra vi tri va gia tri lon nhat";
cout<<"\nnhan phim 5 de dua vi tri va gia tri nho nhat";
cout<<"\nnhan phim 6 de tinh gia tri trung binh cua mang";
cout<<"\nnhan phim 7 de tinh do lech giua hai phan tu";
cout<<"\nnhan phim 8 de tinh do lech trung binh giua cac phan tu";
cout<<"\nnhan phim 9 de thuc hien sequential_search";
cout<<"\nban da chon phim:";
cin>>ss;
switch (ss)
{
case 1:
{
cout<<"sap xep theo giai thuat bubble sort";
d.bubble_sort(max,count);
d.show();
}
case 2:
{
cout<<"sap xep theo giai thuat insertion sort";
d.insertion_sort(max,count);
d.show();
}
case 3:
{
cout<<"sap xep theo giai thuat selection sort";
d.selection_sort(max,count);
d.show();
}
case 4:
{
double gt_min;
cout<<"gia tri va vi tri cua phan tu nho nhat trong mang";
d.find_max(max,count);
for(i=0;i<count;i++)
if(max[i]=gt_min)
cout<<max[i]<<i;
}
case 5:
{
double gt_max;
cout<<"gia tri va vi tri cua phan tu lon nhat trong mang";
d.find_min(max,count);
for(i=0;i<count;i++)
if(max[i]=gt_max)
cout<<max[i]<<i;
}
case 6:
{
cout<<"gia tri trung binh cua cac phan tu:";
d.gt_tb(max,count);
}
case 7:
{
for(int i=0;i<count;i++)
for(int j=0;j<count;j++)
cout<<"do lech cua 2 phan tu "<<max[i]<<" va "<<max[j];
d.dolech_2pt(max,count);
d.show();
}
case 8:
{
cout<<"do lech trung binh cua cac gia tri:";
d.dolech_tb(max,count);
d.show();
}
case 9:
{
cout<<"gia tri can tim kiem:";
cin>>key;
cout<<"sap xep sequential_search:";
d.sequential_search(max,count,key);
d.show();
}
}
getch();
}
Bookmarks