# Sorting

• 01-01-2003
sonict
Sorting
Need help sorting Employee's pay
Code:

```#include <iostream.h> int main() {         // declare variables         char nam[30];         float sale;         float total;         int weekpay = 200;         int i;         int hold;         // for loop and input names & determine wages         cout << "Input 10 employees. " << endl;         for (i=1; i<=10; i++)         {                 cout << "Enter name of worker : ";                 cin  >> nam;                 cout << "Enter number of sales : ";                 cin  >> sale;                 total = ((sale * 0.09) + weekpay);                 cout << "This employees pay is \$ " << total << endl;         }                 cin >> hold;                        return 0; }```
• 01-01-2003
sonict
Quote:

Originally posted by Salem
create an employee structure containing name and pay.
create an array of that structure
use a loop to read data into that array

When you can do this, then we can talk about sorting

I am a beginner and still haven't learned about structures. I don't know what a structure is and don't know how to do it in c++.
• 01-01-2003
Prelude
>I am a beginner and still haven't learned about structures.
Then you'll have a hard time sorting the pay and having the name go with it.

-Prelude
• 01-01-2003
abrege
wheee

Code:

```#include <iostream> using namespace std; #define weekpay 200 struct employee {         char nam[30];         float sale;         float total; }; int main() {         employee array[10];         cout << "Input data for 10 employees. " << endl << endl;         for (int i = 0; i < 10; i ++)         {                 cout << "Enter name of worker : ";                 cin  >> array[i].nam;                 cout << "Enter number of sales : ";                 cin  >> array[i].sale;                 array[i].total = ((array[i].sale * 0.09) + weekpay);                 cout << "This employees pay is \$ " << array[i].total;                 cout << endl << endl;         }                 cin.get();         return 0; }```
• 01-01-2003
sonict
Thanks Aberge for putting structure. Now the sorting
• 01-01-2003
abrege
You should really figure this out yourself, but...

Code:

```#include <iostream> using namespace std; #define weekpay 200 struct employee {         char nam[30];         float sale;         float total; }; int main() {         employee array[10];         int t;         cout << "Input data for 10 employees. " << endl << endl;         for (int i = 0; i < 10; i ++)         {                 cout << "Enter name of worker : ";                 cin  >> array[i].nam;                 cout << "Enter number of sales : ";                 cin  >> array[i].sale;                 array[i].total = ((array[i].sale * 0.09) + weekpay);                 cout << "This employees pay is \$ " << array[i].total;                 cout << endl << endl;         }         for(int a = 1; a > 10; a ++)         {                 for(int b = 9; b >= a; b --)                 {                         if(array[b - 1].total > array[b].total)                         {                                 t = array[b - 1].total;                                 array[b - 1].total = array[b].total;                                 array[b].total = t;                         }                 }         }         cout << "Sorted pays:\n\n";         for(i = 0; i < 10; i ++)         {                 cout << array[i].total << endl;         }                 cin.get();         return 0; }```
• 01-01-2003
Hammer
>>You should really figure this out yourself
Yes, the OP should have a go on their own. If they don't know about structures, there's little point in providing sorting code. Walk before run, remember? ;)

Besides, your sort code doesn't compile and is wrong anyway.
• 01-01-2003
rmullen3
...
as said, you should figure it out for yourself...

Code:

```int minVal = 0; int minPos; // n == number of employees... // Swap(int& a, int& b) function to swap the variables... for (int u = 0; u < n-1; ++u) {         minPos = -1;         for (int v = u; v < n; ++v) {                 if (array[v].total < minVal || minPos == -1) {                         minVal = array[v].total;                         minPos = v;                 }         }         Swap(array[u].total,array[minPos].total); }```
This will sort the array.
• 01-01-2003
abrege
Bah, should've tested it