| Pludge (3) | |
|
Hi. I am dealing with an assignment that requires me to have Bubblesort perform a few tasks. original unsorted data data sorted (ascending) data sorted (descending) mean Maximum and Minimum and the range median mode data having a frequency greater than 1 variance Below is the given prompt #include <iostream> using namespace std; void input (int ulist[26], int& n); void Bubblesort (int ulist[26], int slist[26], int n); void print(int list[26], int n); double average;int n,sum; void main() { int ulist[26], slist[26]; input(ulist, n); cout << "Unsorted" ; print(ulist, n); cout << "Sorted"; Bubblesort (ulist,slist,n); print(slist,n); } void input(int ulist[26], int& n) { int i(0),value; cout << "enter value : \n"; cin >> value ; while (i < 25 && value != -999) { i++; ulist[i] = value; if (i<25) { cin >> value ; } } n=i; } void Bubblesort(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; sum = 0; cout << " list of numbers are : \n"; for (i=1; i<=n; ++i) { cout << list[i] << '\n'; sum = sum + list[i]; } average = sum/n; cout << "\n\n"; cout << "Average is "<< average << "\n"; } I understand how it sorts the numbers in ascending order but am having difficulty getting it sort in descending. Also, do i create separate functions for each other assignment? If so, how many more functions? Finally, at the top of the program it writes the function void Bubblesort (int ulist[26], int slist[26], int n); HOWEVER, towards the middle of the actual program the function is written as void Bubblesort(int unlist[26], int sortlist[26], int n). How does it run correctly in this case? any help is greatly appreciated. Thank you. | |
|
|
|
| Peter87 (3687) | ||||
Just change the comparison from > to <.
It's the parameter types that matters. As you can see, the types are the same in both places. | ||||
|
|
||||
| Pludge (3) | |
|
I have tried that but i can not get it to print out in descending order, yet the code runs. Here it is updated. I put in bold everything i just updated. #include <iostream> using namespace std; void input (int ulist[26], int& n); void Bubblesort (int ulist[26], int slist[26], int n); void Bubblesort2 (int ulist[26], int slistr[26], int n); void print(int list[26], int n); double average;int n,sum; void main() { int ulist[26], slist[26], slistr[26]; input(ulist, n); cout << "Unsorted" ; print(ulist, n); cout << "Sorted"; Bubblesort (ulist,slist,n); print(slist,n); cout<<"Sorted in reverse order"; Bubblesort2 (ulist, slistr,n); print(slistr,n); } void input(int ulist[26], int& n) { int i(0),value; cout << "enter value : \n"; cin >> value ; while (i < 25 && value != -999) { i++; ulist[i] = value; if (i<25) { cin >> value ; } } n=i; } void Bubblesort(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 Bubblesort2 (int ulist[26], int slistr[26], int n); { int i,j,temp; for (i=1;i<=n;i++) slistr[i] = unlist [i]; for (j=1;j<=n-1;j++) for (i=1;i<=n-j;i++) if (slistr[i] < slistr[i+1]) { temp = slistr[i]; slistr[i] = slistr[i+1]; slistr[i+1] = temp; } } void print(int list[26], int n) { int i; sum = 0; cout << " list of numbers are : \n"; for (i=1; i<=n; ++i) { cout << list[i] << '\n'; sum = sum + list[i]; } average = sum/n; cout << "\n\n"; cout << "Average is "<< average << "\n"; } | |
|
|
|
| andyoucantoo (1) | |
|
Delete comma on void Bubblesort2 (int ulist[26], int slistr[26], int n); <- here { int i,j,temp; for (i=1;i<=n;i++) slistr[i] = unlist [i]; <- change this to ulist instead of unlist for (j=1;j<=n-1;j++) for (i=1;i<=n-j;i++) if (slistr[i] < slistr[i+1]) { temp = slistr[i]; slistr[i] = slistr[i+1]; slistr[i+1] = temp; } } in your main function put in system("PAUSE"); so you can see what is going on in your program void main() { int ulist[26], slist[26], slistr[26]; input(ulist, n); cout << "Unsorted" ; print(ulist, n); cout << "Sorted"; Bubblesort (ulist,slist,n); print(slist,n); cout<<"Sorted in reverse order"; Bubblesort2 (ulist, slistr,n); print(slistr,n); system("PAUSE"); <- Here would be a good place } I tested your program with those changes and it does ascending and descending. Nice Job | |
|
|
|