No they are not sorted at the first let say we let the user to enter them how hi want and after that we have to sort it with selection metod by salary :?
ok I make some sorce code but I cant input the name of employee only salary can someone help me with this to enter the name and the salary in same place on vector :?
#include <iostream>
#include <vector>
#include <cstdlib>
usingnamespace std;
void swap(int& x, int& y)
{ int temp = x;
x = y;
y = temp;
}
int min_position(vector<int>& a, int from, int to)
{ int min_pos = from;
int i;
for (i = from + 1; i <= to; i++)
if (a[i] < a[min_pos]) min_pos = i;
return min_pos;
}
void selection_sort(vector<int>& a)
{ int next; /* the next position to be set to the minimum */
for (next = 0; next < a.size() - 1; next++)
{ /* find the position of the minimum */
int min_pos = min_position(a, next, a.size() - 1);
if (min_pos != next)
swap(a[min_pos], a[next]);
}
}
void print(vector<int> a)
{ for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << "\n";
}
int rand_int(int a, int b)
{ return a + rand() % (b - a + 1); }
int main()
{ int empl;
cout << "enter the number of employees:\n";
cin >> empl;
vector<int> v(empl);
for (int i = 0; i < v.size(); i++)
{
cout << " enter the employee and the salary ( like this Harry,20000):\n";
cin >> v[i];
}
print(v);
selection_sort(v);
print(v);
return 0;
}
ok I tried to insert this struct at my code but I can't insert it, it gives me many errors :? and I'm not a familiar with this struct I read many for this but something going wrong :?
#include <iostream>
#include <vector>
#include <cstdlib>
#include <vector>
#include <string>
usingnamespace std;
struct employee
{
string name;
int salary;
};
void swap(int& x, int& y)
{ int temp = x;
x = y;
y = temp;
}
int min_position(vector<int>& a, int from, int to)
{ int min_pos = from;
int i;
for (i = from + 1; i <= to; i++)
if (a[i] < a[min_pos]) min_pos = i;
return min_pos;
}
void selection_sort(vector<int>& a)
{ int next; /* the next position to be set to the minimum */
for (next = 0; next < a.size() - 1; next++)
{ /* find the position of the minimum */
int min_pos = min_position(a, next, a.size() - 1);
if (min_pos != next)
swap(a[min_pos], a[next]);
}
}
void print(vector<employee> a)
{ for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << "\n";
}
int main()
{ int empl;
cout << "enter the number of employees:\n";
cin >> empl;
vector<employee> v(empl);
employee employees;
for (int i = 0; i < v.size(); i++)
{
cout << " enter the employee and the salary ( like this Harry,20000):\n";
cin >> employees.name;
cin >> employees.salary;
}
print(v);
selection_sort(v);
print(v);
return 0;
}
struct employee
{
string name;
int salary;
};
employee e; // create an amployee
e.name = "Bill"; //set the name
e.salary = 12000; // set the salary
std::vector<employee> v; // create a list of employees
v.push_back(e); // add our employee e to the end of the list
cout << v[0].name << endl; // print out the name of the first employee in the list
cout << v[0].salary << endl; // print out the salary of the first employee in the list
#include <iostream>
#include <vector>
#include <cstdlib>
#include <vector>
#include <string>
usingnamespace std;
struct employee
{
string name;
int salary;
};
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
// Need to change this to a vector<employee>
int min_position(vector<int>& a, int from, int to)
{
int min_pos = from;
int i;
for (i = from + 1; i <= to; i++)
if (a[i] < a[min_pos])
min_pos = i;
return min_pos;
}
//void selection_sort(vector<int>& a) // This is no longer int but employee
void selection_sort(vector<employee>& a)
{
int next; /* the next position to be set to the minimum */
for (next = 0; next < a.size() - 1; next++)
{ /* find the position of the minimum */
int min_pos = min_position(a, next, a.size() - 1);
if (min_pos != next)
swap(a[min_pos], a[next]);
}
}
//void print(vector<employee> a)
void print(vector<employee>& a) // Pass a reference &
{
for (int i = 0; i < a.size(); i++)
// cout << a[i] << " "; Need to output just the variables from employee
cout << a[i].name << ", " << a[i].salary;
cout << "\n";
}
int main()
{
int empl;
cout << "enter the number of employees:\n";
cin >> empl;
vector<employee> v(empl);
for (int i = 0; i < v.size(); i++)
{
cout
<< " enter the employee and the salary ( like this Harry,20000):\n";
employee e; // create an employee
cin >> e.name; // get name from user
cin >> e.salary; // get salary from user
// You forgot to put your employee into the vector
v.push_back(e); // put employee into vector
}
print(v);
selection_sort(v);
print(v);
return 0;
}
#include <iostream>
#include <vector>
#include <cstdlib>
#include <vector>
#include <string>
usingnamespace std;
struct employee
{
string name;
int salary;
};
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
int min_position(vector<employee>& a, int from, int to)
{
int min_pos = from;
int i;
for (i = from + 1; i <= to; i++)
if (a[i].salary < a[min_pos].salary)
min_pos = i;
return min_pos;
}
void selection_sort(vector<employee>& a)
{
int next; /* the next position to be set to the minimum */
for (next = 0; next < a.size() - 1; next++)
{ /* find the position of the minimum */
int min_pos = min_position(a, next, a.size() - 1);
if (min_pos != next)
swap(a[min_post].salary, a[next].salary);
}
}
void print(vector<employee>& a)
{
for (int i = 0; i < a.size(); i++)
cout << a[i].name << ", " << a[i].salary;
cout << "\n";
}
int main()
{
int empl;
cout << "enter the number of employees:\n";
cin >> empl;
vector<employee> v(empl);
for (int i = 0; i < v.size(); i++)
{
cout << " enter the employee and the salary ( like this Harry,20000):\n";
employee e; // create an employee
cin >> e.name; // get name from user
cin >> e.salary; // get salary from user
v.push_back(e); // put employee into vector
}
print(v);
selection_sort(v);
print(v);
return 0;
}