Sorting

This is a discussion on Sorting within the C++ Programming forums, part of the General Programming Boards category; Need help sorting Employee's pay Code: #include <iostream.h> int main() { // declare variables char nam[30]; float sale; float total; ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    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;
    }
    Last edited by sonict; 01-01-2003 at 12:57 PM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    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++.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    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;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Thanks Aberge for putting structure. Now the sorting
    Last edited by sonict; 01-01-2003 at 01:26 PM.

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    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;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ...

    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.

  9. #9
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Bah, should've tested it
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 05:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21