-
bubble sort with strings
Code:
#include <iostream>
#include <string>
using namespace std;
void sort(int array[], string months[], int size);
int main()
{
string months[] = {"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
int rainfall[12];
cout << "Enter the monthly rainfall for each month" << endl;
for (int i = 0; i < 12; i++)
{
cout << months [i] << ": ";
cin >> rainfall[i];
while (cin.fail() || rainfall[i] < 0)
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid entry" << endl;
cout << "Re-enter" << endl;
cout << months [i] << ": ";
cin >> rainfall[i];
}
}
sort(rainfall, months, 12);
cout << "Here are the months with the highest"
<< " to lowest rainfall:" << endl;
for (i = 0; i < 12; i++)
cout << months[i] << endl;
return 0;
}
void sort(int array[], string months[], int size)
{
int hold;
string hold2;
int finish;
do
{
finish = 0;
for (int i = 0; i < size - 1; i++)
{
if (array[i] > array[i + 1])
{
hold2 = months[i];
months[i] = months[i + 1];
months[i + 1] = hold2;
hold = array[i];
array[i] = array[i + 1];
array[i + 1] = hold;
}
}
finish = 1;
}while (finish != 0);
}
After all of the data has been entered, the program doesn't do anything. I think the months array is causing the problem.
-
Code:
do
{
....
....
finish = 1;
}while (finish != 0);
How many times will that loop execute?
-
Uh...infinite times...:D :D