Hi guys I'm trying to get this program to sort my array in the second option and i think i have the bubble sort written correctly but it doesn't do anything. Any ideas
//This program takes 20 inputs from the user, and gives the choice to show sorted data,
//sort the data into descending order, display the sorted data, and display the address of the first element.
Code:#include <iostream> using namespace std; #include <iomanip> #include <algorithm> int main() { int i,num[20],n,j,choice,tmp; cout<< "Please enter 20 integers"; for (i=0; i<20; i++) { cout<<"\nEnter next value:"; cin>>num[i]; } cout<<"\n1.Display original data.\n"; cout<<"2.Sort the data into descending order\n"; cout<<"3.Display the sorted data (Only if you've already sorted)\n"; cout<<"4.Get the address of the first element of array.\n\n"; cin>>choice; if (choice==1){ for (i=0; i<20; i++){ cout<<"\n"<<num[i]<<endl; } if (choice==2){ for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++) if (num[j+1] < num[j]) { /* compare the two neighbors */ tmp = num[j]; /* swap a[j] and a[j+1] */ num[j] = num[j+1]; num[j+1] = tmp; cout<<"Here are your numbers:"<<tmp<<endl; } } }}}



LinkBack URL
About LinkBacks


