home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
advfun1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
2KB
|
73 lines
// C++ program that sorts arrays using the Comb sort method
#include <iostream.h>
const int MAX = 10;
const int TRUE = 1;
const int FALSE = 0;
int obtainNumData()
{
int m;
do { // obtain number of data points
cout << "Enter number of data points [2 to "
<< MAX << "] : ";
cin >> m;
cout << "\n";
} while (m < 2 || m > MAX);
return m;
}
void inputArray(int *intArr, int n)
{
// prompt user for data
for (int i = 0; i < n; i++) {
cout << "arr[" << i << "] : ";
cin >> *(intArr + i);
}
}
void showArray(const int *intArr, int n)
{
for (int i = 0; i < n; i++) {
cout.width(5);
cout << *(intArr + i) << " ";
}
cout << "\n";
}
void sortArray(int *intArr, int n)
{
int offset, temp, inOrder;
offset = n;
do {
offset = (8 * offset) / 11;
offset = (offset == 0) ? 1 : offset;
inOrder = TRUE;
for (int i = 0, j = offset; i < (n - offset); i++, j++) {
if (intArr[i] > intArr[j]) {
inOrder = FALSE;
temp = intArr[i];
intArr[i] = intArr[j];
intArr[j] = temp;
}
}
} while (!(offset = 1 && inOrder == TRUE));
}
main()
{
int arr[MAX];
int n;
n = obtainNumData();
inputArray(arr, n);
cout << "Unordered array is:\n";
showArray(arr, n);
sortArray(arr, n);
cout << "\nSorted array is:\n";
showArray(arr, n);
return 0;
}