// array double, bubble sort, average<br>function : เรียงเลขแบบ bubble sort
/* http://www.thaiall.com/tc */
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void sortprocess();
void displayresult();
double n[6]={22,4,6,33,29,18};
int i,j,tot,num,tmp;
void main() {
clrscr();
num = 6;
tot = 0;
sortprocess();
displayresult();
cout << "Total = " << tot << "\n";
cout << "Average = " << tot/num << "\n";
getch();
}
void sortprocess() {
for (i=0;i<num-1;i++) {
for (j=num-1;j>i;j--) {
if (n[j-1] > n[j]) {
tmp = n[j];
n[j] = n[j-1];
n[j-1] = tmp;
}
}
}
}
void displayresult() {
cout << "Result after sorting : " << "\n";
for (i=0;i<num;i++) {
cout << i+1 << " : " << n[i] << "\n";
tot = tot + n[i];
}
}
| |