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
|
#include <iostream>
using namespace std;
void input(char unlisted[80], int& n);
void bubblesort(char unlisted[80], char sortedlist[80], int n);
void minMax(char sortedlist[80], int n);
void frequency(char sortedlist[80], char freq[80], int n);
void print(char list[80], int n);
void printReversed(char list[80], int n);
void main(){
char unlisted[80], sortedlist[80], freq[80];
int n;
input(unlisted, n);
cout << "Unsorted";
print(unlisted, n);
cout << "Sorted";
bubblesort(unlisted, sortedlist, n);
print(sortedlist, n);
cout << "Reversed Sorted";
printReversed(sortedlist, n);
minMax(sortedlist, n);
frequency(sortedlist, freq, n);
cin >> n;
}
void input(char unlisted[80], int& n){
int i = 0;
char value;
cout << "Enter characters: \n";
cin >> value;
while (i < 80 && value != '#'){
i++;
unlisted[i] = value;
if (i < 80){
cin >> value;
}
}
n = i;
}
void bubblesort(char unlisted[80], char sortedlist[80], int n){
int i, j, temp;
for (i = 1; i <= n; i++){
sortedlist[i] = unlisted[i];
}
for (j = 1; j <= n -1; j++){
for (i = 1; i <= n - j; i++){
if (sortedlist[i] > sortedlist[i + 1]){
temp = sortedlist[i];
sortedlist[i] = sortedlist[i + 1];
sortedlist[i + 1] = temp;
}
}
}
}
void minMax(char sortedlist[80], int n){
char min = sortedlist[1];
char max = sortedlist[n];
cout << "\nLargest:" << min << "\n\n";
cout << "Smallest:" << max << "\n\n";
}
void frequency(char sortedlist[80], char freq[80], int n){
int i;
int j;
for (i = 1; i <= n; i++){
for(j = 1; j <= n-i; j++){
if (sortedlist[i] == sortedlist[i + 1]){
freq[j] = sortedlist[i];
}
}
}
for (i = 1; i <= n; i++)
cout << freq[i];
}
void print(char list[80], int n){
int i;
cout << " list of characters by ASCII code are: \n ";
for (i = 1; i <= n; i++){
cout << list[i] << '\n';
}
}
void printReversed(char list[80], int n){
int i;
cout << " list of characters by ASCII code are: \n ";
for (i = n; i >= 1; i--){
cout << list[i] << '\n';
}
}
|