1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Includes
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
// Function Prototypes
int fillArray(double array[]);
void showArray(double array[], int n);
void bubbleSort(double array[], int n);
void swap(double array[], int i, int j);
int frequency (int array[], int n, int value[], int counts[]);
// Main
int main(void) {
// Prompt the user
cout << "This program will help the user to sort the entered values, and show their frequency." << endl;
cout << "Please follow the direction correctly." << endl;
cout << endl;
cout << endl;
cout << endl;
// Create an array of doubles
double dataArray[50];
int arraySize = 0;
// Fill the array
arraySize = fillArray(dataArray);
// print the array
showArray(dataArray, arraySize);
// Sort the array
bubbleSort(dataArray, arraySize);
// print the array
showArray(dataArray, arraySize);
system("pause");
return 0;
}
// Function Definitions
int fillArray(double array[]) {
// Function to fill an array with doubles
int n = -99; // Number of data point
// Prompt for the number of data points
cout << "How many values will you enter? ";
cin >> n;
// Loop to enter data
for(int i = 0; i < n; i++) {
// Enter the data point
cout << "Enter data point " << (i+1) << ": ";
cin >> array[i];
}
return n;
}
void bubbleSort (double array[], int n) {
// Function that sorts an array of doubles
// Nested loop to compare each element with those that follow it
for(int i = 0; i < n - 1; i++) {
for(int j = i+1; j < n; j++) {
// Comparison
if(array[j] < array[i]) {
swap(array, i, j);
}
} // End of inner loop
} // End of outer loop
return;
}
void swap (double array[], int i, int j) {
// Function to swap the elements at indicese i and j
// Swatching the two values by using an intermediate temporary variable
// remember the Tower of Hanoi puzzle
double temp;
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
int frequncy (int array[], int n, int value[], int counts[]) {
// Single for loops with an if-else block
for(int i = 0; i < n; i++) {
int value [50];
int counts [50];
if(array[i] != value[i]) {
// values are different
cout.width(6); cout << right << counts << endl;
}
else(array[i] == value[i]) {
// values are same
cout.width(6); cout << right << counts++ << endl;
}
} // End of for loop
} // End of function
void showArray(double array[], int n) {
// Function to print an array to the screen
// print a linefeed
cout << endl;
// Loop through the array printing the values
for(int i = 0; i < n; i++) {
// Print element i
cout.width(6); cout << left << array[i] << endl;
}
// print a linefeed
cout << endl;
}
|