Boolean Variable. PLEASE HELP!!!!
May 16, 2016 at 6:55pm UTC
I need some help. I have rewritten my code a dozen time but I cant figure out how to include the following.
Converts the bubble sort, changing the outer loop from a for loop to a while loop, so that an early exit is possible when the film names list becomes sorted.
Uses a Boolean variable to control the outer loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}
#pragma endregion
private : System::Void listViewMovies_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
}
private : Void Sort(array<String^>^ v , int n, bool ascending) {
String^ aux;
bool sorted = false ;
while (sorted == false ) {
sorted = true ;
for (int i=0; i<n-1; i++) {
if (ascending) {
if (String::Compare(v[i], v[i+1])>0) {
aux = v[i];
v[i] = v[i+1];
v[i+1] = aux;
sorted = false ;
}
} else {
if (String::Compare(v[i], v[i+1])<0) {
aux = v[i];
v[i] = v[i+1];
v[i+1] = aux;
sorted = false ;
}
}
}
}
}
private : System::Void comboBoxSort_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
String^ aux;
int n = this ->listViewMovies->Items->Count;
array<String^>^ v = gcnew array<String^>(n);
for (int i=0; i<n; i++) {
v[i] = this ->listViewMovies->Items[i]->Text;
}
if (this ->comboBoxSort->SelectedIndex == 0)
this ->Sort(v, n, true );
else
this ->Sort(v, n, false );
for (int i=0; i<n; i++) {
this ->listViewMovies->Items[i]->Text = v[i];
}
if (this ->comboBoxSort->SelectedIndex == 0)
MessageBox::Show(L"List of movies sorted ascending!" );
else
MessageBox::Show(L"List of movies sorted descending!" );
this ->Refresh();
}
private : System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ movieName = this ->textBoxMovie->Text;
System::Windows::Forms::ListViewItem^ listViewItem = (gcnew System::Windows::Forms::ListViewItem(movieName));
this ->listViewMovies->Items->AddRange(gcnew cli::array< System::Windows::Forms::ListViewItem^ >(1) {listViewItem});
this ->textBoxMovie->Text = L"" ;
this ->listViewMovies->EnsureVisible(this ->listViewMovies->Items->Count-1);
MessageBox::Show(L"Movie name added to list!" );
}
private : System::Void buttonDelete_Click(System::Object^ sender, System::EventArgs^ e) {
ListView::SelectedIndexCollection^ sic = this ->listViewMovies->SelectedIndices;
//MessageBox::Show(sic[0].ToString());
ListViewItem^ lvi = this ->listViewMovies->Items[sic[0]];
this ->listViewMovies->Items->Remove(lvi);
MessageBox::Show(L"Movie name deleted from list!" );
this ->Refresh();
}
};
}
Last edited on May 16, 2016 at 7:08pm UTC
Topic archived. No new replies allowed.