### Bubblesort and print out help

Hi. SO i have most of the code for this program which is supposed to sort ascending and descending. However, when I enter the numbers, just as the program is supposed to display the output sorted, it crashes. Here is the code.

#include <iostream>
using namespace std;

void input (int ulist[30], int n);
void Bubblesort (int ulist[30], int sortlist[30], int n, int minnum);
void BubblesortD(int ulist[30], int sortlistD[30], int n, int maxnum);
void print(int list[30], int n);
double average;
int sum,n;
int minnum=0;
int maxnum=0;
void main()
{
int ulist[30], slist[30], sortlistD[30];
input(ulist, n);
cout << "Unsorted";
print(ulist, n);
cout << "Sorted";
Bubblesort (ulist,slist, n, minnum);
print(slist,n);
cout << "Sorted in reverse order";
slist[0]=minnum;
cout <<"Minimum number is:" << minnum << endl;
BubblesortD(ulist, sortlistD, n, maxnum);
print(sortlistD, n);
cout <<"Average is:" << average << endl;

}

void input(int ulist[30], int n)
{
int i(0),value;
cout << "Enter value : \n";
cin >> value ;
while (i < 29 && value != -999)
{
i++;
ulist[i] = value;
if (i<29)
{
cin >> value ;
}
}
n=i;
}

void Bubblesort(int ulist[30], int sortlist[30], int n, int minnum)
{
int i,j,temp;
for (i=1;i<=n;i++)
sortlist[i] = ulist [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 BubblesortD(int ulist[30], int sortlistD[30], int n, int maxnum)
{
int k,l,temp;
for (k=1;k<=n;k++)
sortlistD[k] = ulist [k];
for (l=1;l<=n-1;l++)
for (k=1;k<=n-l;k++)
if (sortlistD[k] < sortlistD[k+1])
{
temp = sortlistD[k];
sortlistD[k] = sortlistD[k+1];
sortlistD[k+1] = temp;

}
maxnum=sortlistD[1];
}

void print(int ulist[30], int n)
{
int i;
sum = 0;
cout << " list of numbers are : \n";
for (i=1; i<=n; ++i)
{
cout << ulist[i] << '\n';
sum = sum + ulist[i];
}
average = sum/n;

}

Topic archived. No new replies allowed.