I am trying to make a C++ program for Bubblesorting. That sorts an array of 25 i
ID: 3757044 • Letter: I
Question
I am trying to make a C++ program for Bubblesorting. That sorts an array of 25 integers in Ascending AND Descending order, but I can't get my code to work. Here's the current code, and an output example will be below it. Prefferably, I'd like the original code mostly in tact so I can see where I went wrong.
#include <iostream>
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;
void main()
{
int ulist[26], salist[26], sdlist[26], ch;
input(ulist, n);
cout << "Unsorted";
print(ulist, n); //prints the unsorted list
cout << "Ascended Sorted";
print(ulist, n);
BubblesortA(ulist, salist, n); //prints ascended list
print(salist, n);
cin >> ch;
cout << "Descended Sorted";
BubblesortB(ulist, sdlist, n); //prints descended list
print(sdlist, n);
cin >> ch;
}
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] << ' ';
}
}
______________________________________________________________
Here's the output:
Please enter your integers :
1
12
32
2
4
6
79
54
7
34
74
13
45
23
12
56
75
3
9
8
7
6
56
3
4
Unsorted list of integers are:
1
12
32
2
4
6
79
54
7
34
74
13
45
23
12
56
75
3
9
8
7
6
56
3
4
Ascended Sorted list of integers are:
1
12
32
2
4
6
79
54
7
34
74
13
45
23
12
56
75
3
9
8
7
6
56
3
4
list of integers are:
1
2
3
3
4
4
6
6
7
7
8
9
12
12
13
23
32
34
45
54
56
56
74
75
79
Explanation / Answer
If you have any doubts, please give me comment...
Need to enter character after printing ascending list of elements. I am hilighted that statement, you can remove that statement also. check once....
#include <iostream>
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);
cin >> ch;
cout << "Descended Sorted";
BubblesortB(ulist, sdlist, n); //prints descended list
print(sdlist, n);
cin >> ch;
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] << ' ';
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.