I need to use arrays, insertion sort algorthm to come up with a program tht will sort 3 employees salaries in an ascending order after user has inputed data..thus

EMPLOYEE1:20000
EMPLOYEE2:15000
EMPLOYEE3:10000

CAN SOMEONE HELP WITHE THE FUNCTION TO MAKE MY PROGRAM DO THAT PLEASE!

## All 7 Replies

Here u go. Dont just use the code. Study the algorithm.

``````void insert_sort(double salary[],int n)  //salary - Array of Salaries of the Employees || n - Number of employees
{
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
{
if(salary[i]<salary[j])
{
double temp=salary[j];
salary[j]=salary[i];
for(int k=i;k>j;k--)
salary[k]=salary[k-1];
salary[j+1]=temp;
}
}
}``````

@OP:
The algorithm for insertion sort works by scanning through an array from 0 to (ARRAY_SIZE-1). As it does so it re-arranges the elements in the array so that the elements closer to Element 0 (the beginning) than the current position are "sorted" and those elements closer to the end are not sorted. To do this, it scans the sorted elements and compares them to the current unsorted element. When it detects where the unsorted element goes, it inserts the unsorted element into its current sorted position pushing the elements after it down 1 element.

Algorithm (for zero-based array):

``````for an index "[B]unsortedElement[/B]" that ranges from 1 to (ARRAY_SIZE-1) { //outer loop

create another index "[B]comparedElement[/B]" that ranges from (0 to [B]unsortedElement - 1[/B]) {  //inner loop

if the the element at index [B]unsortedElement[/B] should be before the element at index [B]comparedElement[/B] {
create a [B]temporary[/B] value to store the element at [B]unsortedElement[/B]

for an index "[B]slide[/B]" that ranges from [B]unsortedElement[/B] to ([B]comparedElement + 1[/B]) { //insertion loop
store the value of element at index ([B]slide-1[/B]) to the element at index [B]slide[/B]
}  //end insertion loop

store the [B]temporary[/B] value to the element at index [B]comparedElement[/B]
}  //end if
} //end inner loop
} //end outer loop``````

Here u go. Dont just use the code. Study the algorithm.

``````void insert_sort(double salary[],int n)  //salary - Array of Salaries of the Employees || n - Number of employees
{
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
{
if(salary[i]<salary[j])
{
double temp=salary[j];
salary[j]=salary[i];
for(int k=i;k>j;k--)
salary[k]=salary[k-1];
salary[j+1]=temp;
}
}
}``````

You do realize he's just going to C/P the code don't you? Most (as in 90%+) do.

You're far better off providing a generic example than you are an exact solution. Then they HAVE TO think about it and are more likely to learn it properly.

``````//variable uses:
// u/i - index of unsorted value / value being sorted
// d/j - index of compared value / destination of unsorted value
// s/k - index used to slide values during insertion phase
// temp - I think this is fairly obvious
// array - the values to be sorted, assume that relational operators are defined for the type
// SET_SIZE - a constant representing the number of elements in array
// T - a generic placeholder for the dataType of the elements in array

for (int u = 1; u < SET_SIZE; ++u) {
for (int d = 0; d < u; ++d) {
if (array[u] < array[d]) {
T temp = array[u];
for (int s = u; s > d; --s) {
array[s] = array[s-1];
} //end for loop s

array[d] = temp;

} //end if

} //end for loop d

} //end for loop u``````

thank you guys so much...en i wont copy paste the code will try en formulate it and come up with somthin that goes along with my program if i fail to run it ..i will then refer to the answers you gave me...thnx!

@OP:
The algorithm for insertion sort works by scanning through an array from 0 to (ARRAY_SIZE-1). As it does so it re-arranges the elements in the array so that the elements closer to Element 0 (the beginning) than the current position are "sorted" and those elements closer to the end are not sorted. To do this, it scans the sorted elements and compares them to the current unsorted element. When it detects where the unsorted element goes, it inserts the unsorted element into its current sorted position pushing the elements after it down 1 element.

Algorithm (for zero-based array):

``````for an index "[B]unsortedElement[/B]" that ranges from 1 to (ARRAY_SIZE-1) { //outer loop

create another index "[B]comparedElement[/B]" that ranges from (0 to [B]unsortedElement - 1[/B]) {  //inner loop

if the the element at index [B]unsortedElement[/B] should be before the element at index [B]comparedElement[/B] {
create a [B]temporary[/B] value to store the element at [B]unsortedElement[/B]

for an index "[B]slide[/B]" that ranges from [B]unsortedElement[/B] to ([B]comparedElement + 1[/B]) { //insertion loop
store the value of element at index ([B]slide-1[/B]) to the element at index [B]slide[/B]
}  //end insertion loop

store the [B]temporary[/B] value to the element at index [B]comparedElement[/B]
}  //end if
} //end inner loop
} //end outer loop``````

You do realize he's just going to C/P the code don't you? Most (as in 90%+) do.

You're far better off providing a generic example than you are an exact solution. Then they HAVE TO think about it and are more likely to learn it properly.

``````//variable uses:
// u/i - index of unsorted value / value being sorted
// d/j - index of compared value / destination of unsorted value
// s/k - index used to slide values during insertion phase
// temp - I think this is fairly obvious
// array - the values to be sorted, assume that relational operators are defined for the type
// SET_SIZE - a constant representing the number of elements in array
// T - a generic placeholder for the dataType of the elements in array

for (int u = 1; u < SET_SIZE; ++u) {
for (int d = 0; d < u; ++d) {
if (array[u] < array[d]) {
T temp = array[u];
for (int s = u; s > d; --s) {
array[s] = array[s-1];
} //end for loop s

array[d] = temp;

} //end if

} //end for loop d

} //end for loop u``````

K thanks for the advice. Im new here and i just wanted to help. From now on i wont just post the solution. But ill try to help those who need help.

Thanks man.

And OP: no problem dude.

hi guys suppose i wanted the progarm to come out in a format below what do i put where. am confused..this aint homework just my personal interests....

i want my code to look like this

// Lab 4: EmployeeTest.cpp
2 // Input employee info, and output their income in descending order
3 #include <iostream>
4 using namespace std;
5
6 #include "Employee.h" // include definition of class Employee
7
8 int EMPLOYEE_NUM = 3; //employee numbers
9 /* Input employee info */
10 void input (Employee employee[]);
11 /* Sort employee by their income */
12 void sort (Employee employee[]);
13 /* Output all the employee info */
14 void output (Employee employee[]);
15 // function main begins program execution
16 int main()
17 {
18! Employee employee[EMPLOYEE_NUM];
19 input(employee);
20 sort (employee);
21 output (employee);
23!
24 } //end main

what i have done so far is for my employees header file......

``````#include <iostream>
#include <string>

using namespace std;

class Employee
{
private:
int EmployeeNumber;
string FirstName;
string LastName;
float MonthlySalary;

public:
Employee(string fName, string lName, int mSalary)
{

setFirstName(fName);
setLastName(lName);
setMonthlySalary(mSalary);

}
void setFirstName(string name)
{
FirstName = name;
}

string getFirstName()
{
return FirstName;
}

void setLastName(string surname)
{
LastName = surname;
}

string getLastName()
{
return LastName;
}

void setMonthlySalary (int salary)
{
if (salary <0)
{
salary =0;
}
MonthlySalary = salary;
}

int getMonthlySalary()
{
return MonthlySalary;
}

int Income (int wMonths)

{    int income =0;

if (wMonths>10)

{     income=MonthlySalary*10+1.5*MonthlySalary*(wMonths-10);

}
else

{   income = (MonthlySalary*wMonths);

}
if ( income > 10000)
income = 10000 + (income - 10000) * 0.8;

return income;
}
};

[B]and for my main function........[/B]

#include"Employee.h"
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

int main()
{
int numberEmp=0,i;
cin>> numberEmp;
for (i=0;i<numberEmp;i++)
{
string firstName="";
string lastName="";
int monthSal=0,totalInc=0;
int workmonth=0;
cin>> firstName;
cin>> lastName;
cin>> monthSal;
cin>>workmonth;
Employee emp(firstName,lastName,monthSal);

totalInc=emp.Income(workmonth);
cout<<"Name:"<<firstName<<" "<<lastName<<"; Total Income:"<<totalInc<<endl<<endl<<endl;
}

}``````

how then do i ammend this program so that i get an output like this......

OUTPUT....

All employees’income:
Name: Susan Baker, income: 22000
Name: Jack Jones, income: 10000
Name: Bob Jones, income: 8000

i need a solution on

how to Implement a Permutation generation program using c++
Input examples: (1,3,5,7,9) or (a, b, c, d, e, f, g)

Result should be printed to a txt file.

Based on either switching method or inversion method

i would appreciate a full but understandable code as i am new in c++ programmingam currently studying iton my own

hi guys suppose i wanted the progarm to come out in a format below what do i put where. am confused..this aint homework just my personal interests....

i want my code to look like this

``````// Lab 4: EmployeeTest.cpp
// Input employee info, and output their income in descending order
#include <iostream>
using namespace std;

#include "Employee.h" // include definition of class Employee

int EMPLOYEE_NUM = 3; //employee numbers
/* Input employee info */
void input (Employee employee[]);
/* Sort employee by their income */
void sort (Employee employee[]);
/* Output all the employee info */
void output (Employee employee[]);
// function main begins program execution
int main()
{
Employee employee[EMPLOYEE_NUM];
input(employee);
sort (employee);
output (employee);

} //end main
``````

what i have done so far is for my employees header file......

``````#include <iostream>
#include <string>

using namespace std;

class Employee
{
private:
int EmployeeNumber;
string FirstName;
string LastName;
float MonthlySalary;

public:
Employee(string fName, string lName, int mSalary)
{

setFirstName(fName);
setLastName(lName);
setMonthlySalary(mSalary);

}
void setFirstName(string name)
{
FirstName = name;
}

string getFirstName()
{
return FirstName;
}

void setLastName(string surname)
{
LastName = surname;
}

string getLastName()
{
return LastName;
}

void setMonthlySalary (int salary)
{
if (salary <0)
{
salary =0;
}
MonthlySalary = salary;
}

int getMonthlySalary()
{
return MonthlySalary;
}

int Income (int wMonths)

{    int income =0;

if (wMonths>10)

{     income=MonthlySalary*10+1.5*MonthlySalary*(wMonths-10);

}
else

{   income = (MonthlySalary*wMonths);

}
if ( income > 10000)
income = 10000 + (income - 10000) * 0.8;

return income;
}
};
``````

and for my main function........

``````#include"Employee.h"
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

int main()
{
int numberEmp=0,i;
cin>> numberEmp;
for (i=0;i<numberEmp;i++)
{
string firstName="";
string lastName="";
int monthSal=0,totalInc=0;
int workmonth=0;
cin>> firstName;
cin>> lastName;
cin>> monthSal;
cin>>workmonth;
Employee emp(firstName,lastName,monthSal);

totalInc=emp.Income(workmonth);
cout<<"Name:"<<firstName<<" "<<lastName<<"; Total Income:"<<totalInc<<endl<<endl<<endl;
}

}
``````

how then do i ammend this program so that i get an output like this......

OUTPUT....

``````Please input first name: Bob

All employees’income:
Name: Susan Baker, income: 22000
Name: Jack Jones, income: 10000
Name: Bob Jones, income: 8000
``````

end quote.

Well, u need to send the employee array to the sort function. Modify the GENERIC code that was previously supplied to u to suit ur needs.
Use employee.lastname for sorting purpose.

Gppd lUCk!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.