数组中插入元素并采用冒泡排序进行数组排序,然后再删除一个元素,打印数组元素。
using namespace std;
int main() {
int a[11];
int insertNum;
int i;
int j;
int min;
int minIndex;
int temp;
int num;
int deleteIndex;
cout << "输入数组:";
for (int i = 0; i < 10; i++) {
cin >> a[i];
}
for (int i = 0; i < 10; i++) {
cout << a[i] << '\t';
}
cout << endl;
//插入
cout << "请输入一个数:";
cin >> insertNum;
a[10] = insertNum;
//插值重排
for (i = 0; i < 11; i++) {
min = a[i];
minIndex = i;
for (j = i + 1; j < 11; j++) {
if (a[j] < min) {
min = a[j];
minIndex = j;
}
}
if(minIndex > i) {
temp = a[minIndex];
a[minIndex] = a[i];
a[i] = temp;
}
}
cout << "插入后:";
for (i = 0; i < 11; i++) {
cout << a[i] << " ";
}
cout << endl;
//删除
cout << "请输入一个数:";
cin >> num;
for (int i = 0; i < 11; i++) {
if (num == a[i]) {
deleteIndex = i;
break;
}
}
for (i = deleteIndex; i < 10; i++){
a[i] = a[i + 1];
}
cout << "删除后:";
for (i = 0; i < 10; i++){
cout << a[i] << " ";
}
cout << endl;
system("pause");
return 0;
}