Ordering an Array - Help

Howzit guys,

I'm currently trying to figure out how to output the stored integers in a given array of 10 elements in descending order.

I have an array of 10 people storing how much each person gets paid per hour.

int personPay[9];

I want the index+1 to identify which person gets paid that amount.

ie. cout << "person " << i+1 << " gets paid " << personPay[i] << " per hour."

my question is how would i output to the screen how much each person gets paid in descending order.

ie.

output: person8 gets paid $10 per hour
person2 gets paid $7 per hour
person5 gets paid $6 per hour
closed account (DSLq5Di1)
Change your logic in the loop to iterate from last to first element.. ?
Here's the code...It's for Windows in VS2010
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <Windows.h>
#include <algorithm>
#include <vector>

using namespace std;

#define MAX 100000

void inputArray(int x[], int m);
void printArray(int x[], int m);
void sortAccessing(int x[], int m);
void sortDescendin(int x[], int m);


int main ()
{

	int n ;
	cout<<"Enter the size of the array: ";
	cin>>n;

	int myArray[MAX];


	inputArray(myArray, n);
	printArray(myArray, n);
	sortAccessing(myArray, n);
	sortDescendin(myArray, n);


	
cin.get(); cin.get();
return 0;
}



void inputArray(int x[], int m) {

	for (int i = 0; i < m; i++) {
	
		cout<<"Enter member in the array with index: "<<i<<"  ";
		cin>>x[i];
	
	}

}


void printArray(int x[], int m) {



	cout<<"\n\nMemebers of the array: "<<endl;
	for(int i = 0; i < m; i++) {
	
		cout<<"  "<<x[i];
	}


}



void sortAccessing(int x[], int m) {


		sort(x, x+m);


		cout<<"\n\nMembers of the array in acc. order: "<<endl;
		for(int i = 0; i < m; i++) {
		
			cout<<"  "<<x[i];
		
		}
}


void sortDescendin(int x[], int m){


	cout<<"\n\nMembers of the array in descending order: "<<endl;
	vector<int> myVector(x, x+m);
	vector<int>::iterator it;

	sort(myVector.begin(), myVector.end(), greater<int>());

	for(it = myVector.begin(); it != myVector.end(); it++) {
	
	
		cout<<"  "<<*it;
	
	}

}
Last edited on
Does your solution have any contraints? For example, is using elements of stl allowed?

Do you have to track the person number? If you just sort the array the original indexes are lost, so you can't say person #3 (in original order) is the highest paid. (well, you're example does suggest this...)

Andy

P.S. I assume the array size of 9 is a typo as it's stated nearby that there are 10 people...
closed account (DSLq5Di1)
Is this what you are trying to achieve GlitchM?
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct person
{
	string name;
	unsigned rate;
};

bool rate_descending(const person& a, const person& b) // comparison function
{
	return b.rate < a.rate;
}

int main()
{
	person input[] = {{"Bruce Wayne", 14}, {"Roger Ramjet", 9}, {"Spongebob", 23}};
	// just for ease of initialization in this example

	vector<person> employees(input, input + 3);
	sort(employees.begin(), employees.end(), rate_descending);

	for (auto it = employees.begin(); it != employees.end(); it++)
	{
		cout << it->name << "\t$" << it->rate << endl;
	}

	cin.sync();
	cin.ignore();
	
	return 0;
}
Last edited on
hahahahah I'm not trying to achieve nothing... that was my code for ordering int array in acc and des order :)
closed account (DSLq5Di1)
Ah I see, your last reply was a little vague.. and the code you posted above is not much use if you are not associating the person with their hourly rate.

-edit- @Janlan Err sorry I need more sleep, mistook you for the op.. lol
Last edited on
Hey guys, sorry for late reply. work's hectic at mo :P

@andywestken: put 9 in the int array[9] cos i was counting the 0 as an index as well.

@janlan: I see ur point there, but i need to keep the indices associated with their relevent amount.

sorry all, forgot to put the following constraint in my question: can't use classes\struct or <vector>.

so i'm working mainly with if,if-else, while, for to work around this.
tried working with <algorithm> but the logic is still alluding me :P

but thanx for all your replies really appreciate the help :)

This works but highly inefficient code..... I am also new.....


#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int basic[10];
int arr[10];
int min,k,swap=0;;

for(int i=0;i<10;i++)
arr[i]=i+1;
cout<<"Enter the payments of:"<<endl;

//to get input

for(int i=0;i<10;i++)
{
cout<<"Person "<<i+1<<":";
cin>>setw(6)>>left>>basic[i];
}


for(int i=0;i<9;i++)
{
min=basic[i];
k=i;
for(int j=i+1;j<10;j++)
{
if(min>basic[j])
{
swap=1;
min=basic[j];
k=j;
}
}
if(swap)
{
basic[k]=basic[i];
basic[i]=min;
int sw=arr[i];
arr[i]=arr[k];
arr[k]=sw;
swap=0;
}
}


for(int i=0;i<10;i++)
{
cout<<"\nPerson "<<setw(3)<<left<<arr[i]<<" has salary :"
<<setw(6)<<right<<showpoint
<<setprecision(2)<<basic[i];
}


cin.sync();
cin.get();
cin.sync();

return 0;
}


if you find a better solution then plz let me also know worked 3 hours on it.. lol...
Topic archived. No new replies allowed.