-2
- 嘗試使用包含10個元素的數組構建一個程序並初始化它,獲得這些數字的最大值,最小值,平均值和總和。打印結果。
- 構建一個函數,使用冒泡排序算法或其他排序算法對其進行升序排序。打印結果。
- 將上面的代碼修改爲OOP風格:構建一個類,將上面的所有函數整合到類中。製作一個或兩個對象來打印結果。
這是我對這個問題的嘗試:構建一個包含10個元素的數組
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubble_sorting(int a1[]);
int _tmain(int argc, _TCHAR* argv[])
{
int size=10;
int data[100]={11, 13, 15, 17, 19, 20, 18, 16, 14, 12};
int min, max, avg, sum;
// cout<<"Enter the array size:"<<endl;
// cin>>size;
for(int i1=0;i1<size;i1++)
{
// cin>>data[i1];
// data[i1]=rand();
if(i1==0)
{
min=data[i1];
max=data[i1];
sum=data[i1];
}
else
{
if(data[i1]<min)min=data[i1];
if(data[i1]>max)max=data[i1];
sum=sum+data[i1];
}
}
cout<<"The Min number is:"<<min<<endl;
cout<<"The Max number is:"<<max<<endl;
cout<<"The average number is:"<<sum/size<<endl;
cout<<"The sum number is:"<<sum<<endl;
bubble_sorting(data);
return 0;
}
void bubble_sorting(int a1[])
{
int i2;
int t1;
for(int i1=0;i1<10;i1++)
{
i2=1;
for(i2=1;i2<10;i2++)
{
if(a1[i2]<a1[i2-1])
{
t1=a1[i2];
a1[i2]=a1[i2-1];
a1[i2-1]=t1;
}
}
}
cout<<"The data after sorting is:"<<endl;
for(int i1=0;i1<10;i1++)
{
cout<<a1[i1]<<" ";
}
cout<<endl;
}
什麼是構建冒泡排序的最佳方式?
而你的問題是什麼? – user657267
您可以添加您的問題 –
什麼是構建冒泡排序的最佳方式? – user3567164