one task in c++ and I have problem :?

Hi, I need help for one task if someone can help me I will be very grateful

modify the selection sort algorithm to sort a vector of employees by salary
Last edited on
What is your vector of employes sorted by at present?
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 :?

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
 #include <iostream>
 #include <vector>
 #include <cstdlib>
 using namespace 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;
}

this is the code
thanks very much
You have a vector of int. What is this supposed to represent? Is it a vector of salaries?

Are you familar with struct?
Line 47, 48 suggests you want to input a string "Harry,2000" but you are trying to put it into an integer.

That's a miss match.

I think you should consider creating a type as kbw suggests to hold the different pieces of information for each person.

Something like:

1
2
3
4
5
6
7
8
#include <vector>
#include <string>

struct employee
{
    std::string name;
    int salary;
};


Then you would naturally keep a vector of employees like this:

1
2
3

std::vector<employee> v(empl);


You could then either input the name and salary separately or read in the whole line and separate them after.
Last edited on
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 :?
You need to post the code. We're not psychic ;o)
Ups sorry it's my mistake
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
 #include <iostream>
 #include <vector>
 #include <cstdlib>
 #include <vector>
 #include <string>


 using namespace 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;
}

and the error that show is
57: error: no match for ‘operator<<’ in ‘std::cout << v.std::vector<_Tp, _Alloc>::operator[] [with _Tp = employee, _Alloc = std::allocator<employee>](((unsigned int)i))’
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]


and i've work under ubuntu
Okay, I have corredted some of the errors and left notes to make changes by others.

Because you are using a struct you need to access it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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



Here is the code:

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <vector>
#include <string>

using namespace 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;
}
Last edited on
whit your changes and with this which you write to make this show me error :
cp.cpp: In function ‘int min_position(std::vector<employee, std::allocator<employee> >&, int, int)’:
cp.cpp:28: error: no match for ‘operator<’ in ‘((std::vector<employee, std::allocator<employee> >*)a)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = employee, _Alloc = std::allocator<employee>](((unsigned int)i))->employee::salary < ((std::vector<employee, std::allocator<employee> >*)a)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = employee, _Alloc = std::allocator<employee>](((unsigned int)min_pos))’


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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <vector>
#include <string>

using namespace 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;
}
Last edited on
I'm not sure you posted the correct error for the code.

However I did see one mistake in there on line 41:

 
swap(a[min_post].salary, a[next].salary);


small spelling error, it should be:


 
swap(a[min_pos].salary, a[next].salary);

Topic archived. No new replies allowed.