Help with bubble sort

/**Hi there, i need help with a bubble sort program that sorts employee last names alphabetically. I know there is much more better ways to use a bubble sort but this is the way our teacher wants to do it, with a while loop with a control variable char sort that has a nested for and a one legged if statement to compare. I cant seem to get it to run right. Please help. Thanks for any help*/

#include
#include
using namespace std;
#include
#include
#include

const int FLSA_hours_limit = 40;
const double premium = 1.5;

int main(int argc, char *argv[]){
string employee_first_name[5];
string employee_last_name[5];
string temp; // TEMPORARY LOCATION
char sorted; //LOOP CONTROL
double pay_rate;
double gross_pay[5];
int hours_worked;

for(int i = 0; i < 5; i++){
cout << "Enter employee first name: ";
cin >> employee_first_name[i];
cout << "Enter employee last name: ";
cin >> employee_last_name[i];
cout << "Enter employee's pay rate: ";
cin >> pay_rate;
cout << "Enter employee's hours worked: ";
cin >> hours_worked;

if(hours_worked > FLSA_hours_limit){
gross_pay[i] = FLSA_hours_limit * pay_rate + (hours_worked - FLSA_hours_limit) * pay_rate * premium;
}else{
gross_pay[i] = hours_worked * pay_rate;
}
cout << "
";
}

cout << "
";
cout << "First Name";
cout.width(20);
cout << "Last Name";
cout.width(20);
cout << "Gross Pay
";

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

//Where sorting and my problems begin and what need fixed
while(sorted = 'n'){
sorted = 'y';
for (int i = 0; i < 5; i++){
//SWITCHES ORDER
if(employee_last_name[i] > employee_last_name[i + 1]){
temp = employee_last_name[i];
employee_last_name[i] = employee_last_name[i + 1];
employee_last_name[i + 1] = temp;
sorted = 'n';
}
}
}

for(int i = 0; i < 5; i++){
cout << employee_first_name[i];
cout.width(20);
cout << employee_last_name[i];
cout.width(20);
cout << "$" << gross_pay[i];
cout << "
";
}

system("PAUSE");
return EXIT_SUCCESS;
}

Comments

  • : [code]
    : #include
    : #include
    : using namespace std;
    : #include
    : #include
    : #include
    :
    : const int FLSA_hours_limit = 40;
    : const double premium = 1.5;
    :
    : int main(int argc, char *argv[]){
    : string employee_first_name[5];
    : string employee_last_name[5];
    : string temp; // TEMPORARY LOCATION
    : char sorted; //LOOP CONTROL
    : double pay_rate;
    : double gross_pay[5];
    : int hours_worked;
    :
    : for(int i = 0; i < 5; i++){
    : cout << "Enter employee first name: ";
    : cin >> employee_first_name[i];
    : cout << "Enter employee last name: ";
    : cin >> employee_last_name[i];
    : cout << "Enter employee's pay rate: ";
    : cin >> pay_rate;
    : cout << "Enter employee's hours worked: ";
    : cin >> hours_worked;
    :
    : if(hours_worked > FLSA_hours_limit){
    : gross_pay[i] = FLSA_hours_limit * pay_rate +
    : (hours_worked - FLSA_hours_limit) * pay_rate * premium;
    : }else{
    : gross_pay[i] = hours_worked * pay_rate;
    : }
    : cout << "
    ";
    : }
    :
    : cout << "
    ";
    : cout << "First Name";
    : cout.width(20);
    : cout << "Last Name";
    : cout.width(20);
    : cout << "Gross Pay
    ";
    :
    : cout.setf(ios::fixed);
    : cout.setf(ios::showpoint);
    : cout.precision(2);
    :
    : //Where sorting and my problems begin and what need fixed
    : [color=Red]sorted = 'n'; /* uninitialized var used!*/[/color]
    : while(sorted =[color=Red]=[/color] 'n'){ [color=Red]/* get a better compiler */[/color]
    : sorted = 'y';
    : for (int i = 0; i < [color=Red]4[/color]; i++){ [color=Red]/* i+1 will be over array size */[/color]
    : //SWITCHES ORDER
    : if(employee_last_name[i] > employee_last_name[i + 1]){
    : temp = employee_last_name[i];
    : employee_last_name[i] = employee_last_name[i + 1];
    : employee_last_name[i + 1] = temp;
    : sorted = 'n';
    : }
    : }
    : }
    :
    : for(int i = 0; i < 5; i++){
    : cout << employee_first_name[i];
    : cout.width(20);
    : cout << employee_last_name[i];
    : cout.width(20);
    : cout << "$" << gross_pay[i];
    : cout << "
    ";
    : }
    :
    : system("PAUSE");
    : return EXIT_SUCCESS;
    : }
    : [/code]
    :
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion