The following C++ code uses the bubble sort method to take an unordered 25 integ
ID: 3757163 • Letter: T
Question
The following C++ code uses the bubble sort method to take an unordered 25 integer list, and present bth its ascending and descending order. I'd like to see however, if there's anything that can be added to find the median and mode of the array. The code is down below.
#include <iostream>
#ifdef _WIN32
#define WINPAUSE system("pause")
#endif
/*
Print out the original unsorted data
Print out the data sorted (ascending)
Print out the data sorted (descending) */
using namespace std;
void input(int ulist[26], int &n);
void BubblesortA(int ulist[26], int salist[26], int n);
void BubblesortB(int ulist[26], int sdlist[26], int n);
void print(int list[26], int n);
void printB(int list[26], int n);
double average;
int n, sum;
int main()
{
int ulist[26], salist[26], sdlist[26], ch;
input(ulist, n);
cout << "Unsorted";
print(ulist, n); //prints the unsorted list
cout << "Ascended Sorted";
BubblesortA(ulist, salist, n); //prints ascended list
print(salist, n);
cout << "Descended Sorted";
BubblesortB(ulist, sdlist, n); //prints descended list
print(sdlist, n);
return 0;
}
void input(int ulist[26], int &n)
{
int i = 0;
int value;
cout << "Please enter your integers : ";
cin >> value;
while (i < 25 && value != '#')
{
i++;
ulist[i] = value;
if (i < 25)
{
cin >> value;
}
}
n = i;
}
void BubblesortA(int unlist[26], int sortlist[26], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= n - j; i++)
{
if (sortlist[i] > sortlist[i + 1])
{
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
}
}
}
void print(int list[26], int n)
{
int i;
cout << " list of integers are: ";
for (i = 1; i <= n; ++i)
{
cout << list[i] << ' ';
}
}
void BubblesortB(int unlist[26], int sortlist[26], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= n - j; i++) {
if (sortlist[i] < sortlist[i + 1])
{
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
}
}
}
void printB(int list[26], int n)
{
int i;
cout << " list of integers are: ";
for (i = 1; i <= n; ++i)
{
cout << list[i] << ' ';
printf("Press ENTER key to Continue ");
getchar();
}
system("pause");
}
#include <iostream>
#ifdef _WIN32
#define WINPAUSE system("pause")
#endif
/*
Print out the original unsorted data
Print out the data sorted (ascending)
Print out the data sorted (descending) */
using namespace std;
void input(int ulist[26], int &n);
void BubblesortA(int ulist[26], int salist[26], int n);
void BubblesortB(int ulist[26], int sdlist[26], int n);
void print(int list[26], int n);
void printB(int list[26], int n);
double average;
int n, sum;
int main()
{
int ulist[26], salist[26], sdlist[26], ch;
input(ulist, n);
cout << "Unsorted";
print(ulist, n); //prints the unsorted list
cout << "Ascended Sorted";
BubblesortA(ulist, salist, n); //prints ascended list
print(salist, n);
cout << "Descended Sorted";
BubblesortB(ulist, sdlist, n); //prints descended list
print(sdlist, n);
return 0;
}
void input(int ulist[26], int &n)
{
int i = 0;
int value;
cout << "Please enter your integers : ";
cin >> value;
while (i < 25 && value != '#')
{
i++;
ulist[i] = value;
if (i < 25)
{
cin >> value;
}
}
n = i;
}
void BubblesortA(int unlist[26], int sortlist[26], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= n - j; i++)
{
if (sortlist[i] > sortlist[i + 1])
{
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
}
}
}
void print(int list[26], int n)
{
int i;
cout << " list of integers are: ";
for (i = 1; i <= n; ++i)
{
cout << list[i] << ' ';
}
}
void BubblesortB(int unlist[26], int sortlist[26], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= n - j; i++) {
if (sortlist[i] < sortlist[i + 1])
{
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
}
}
}
void printB(int list[26], int n)
{
int i;
cout << " list of integers are: ";
for (i = 1; i <= n; ++i)
{
cout << list[i] << ' ';
printf("Press ENTER key to Continue ");
getchar();
}
system("pause");
}
Explanation / Answer
If you have any doubts, please give me comment...
#include <iostream>
#include <cstdlib>
#ifdef _WIN32
#define WINPAUSE system("pause")
#endif
/*
Print out the original unsorted data
Print out the data sorted (ascending)
Print out the data sorted (descending) */
using namespace std;
void input(int ulist[26], int &n);
void BubblesortA(int ulist[26], int salist[26], int n);
void BubblesortB(int ulist[26], int sdlist[26], int n);
void print(int list[26], int n);
void printB(int list[26], int n);
double median(int list[26], int n);
void mode(int list[26], int n);
double average;
int n, sum;
int main()
{
int ulist[26], salist[26], sdlist[26], ch;
input(ulist, n);
cout << "Unsorted";
print(ulist, n); //prints the unsorted list
cout << "Ascended Sorted";
BubblesortA(ulist, salist, n); //prints ascended list
print(salist, n);
cout << "Descended Sorted";
BubblesortB(ulist, sdlist, n); //prints descended list
print(sdlist, n);
cout<<"Median of list is: "<<median(sdlist, n)<<endl;
cout<<"Mode of list is: "<<endl;
mode(sdlist, n);
return 0;
}
void input(int ulist[26], int &n)
{
int i = 0;
int value;
cout << "Please enter your integers : ";
cin >> value;
while (i < 25 && value != '#')
{
i++;
ulist[i] = value;
if (i < 25)
{
cin >> value;
}
}
n = i;
}
void BubblesortA(int unlist[26], int sortlist[26], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++)
{
for (i = 1; i <= n - j; i++)
{
if (sortlist[i] > sortlist[i + 1])
{
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
}
}
}
void print(int list[26], int n)
{
int i;
cout << " list of integers are: ";
for (i = 1; i <= n; ++i)
{
cout << list[i] << ' ';
}
}
void BubblesortB(int unlist[26], int sortlist[26], int n)
{
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++)
{
for (i = 1; i <= n - j; i++)
{
if (sortlist[i] < sortlist[i + 1])
{
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
}
}
}
void printB(int list[26], int n)
{
int i;
cout << " list of integers are: ";
for (i = 1; i <= n; ++i)
{
cout << list[i] << ' ';
cout<<"Press ENTER key to Continue "<<endl;
getchar();
}
system("pause");
}
double median(int list[26], int n){
if(n%2!=0)
return (double)list[n/2];
return (double)(list[(n-1)/2] + list[n/2])/2.0;
}
void mode(int list[], int n){
int maxValue, maxCount = 0, count= 0;
for(int i=0; i<n; i++){
if(list[i]!=list[i-1]){
if(count>maxCount)
maxCount = count;
count =0;
}
else{
count++;
}
}
count = 0;
for(int i=0; i<n; i++){
if(list[i]!=list[i-1]){
if(count==maxCount)
cout<<list[i-1]<<endl;
count = 0;
}
else
count++;
}
cout<<endl;
}