Algorithm to cout an array in certain order?

Basically, I need to compare one cell to all of the other cells in the same array. I have most of the code done, I just need 2 algorithms. I've been using pen and paper to design one, but I just can't figure it out.

Basically, I need to cout all cars under $25,000. I got that done easily.

Then I need to compare all the cells, to find a tie in price. If two cars are both for example, $16,000 then I need to cout the first one first. So I need to compare the price first. If they are the same, I need to cout the higher year (newer car) year.

I have been testing out some algorithms, but none of them seem to work. I really don't know what to do. Can someone please help me? Can I at least see some examples of algorithms for sorting arrays.

Here is my unfinished code so far:

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
  #include <iostream>
#include <string>
#include <sstream>
#include "car.h"
using namespace std;

int main()
{
	int a;
	string b; string c;
	float d; float e;
	int file(ifstream& cartxt);
	const int number=2;
	car cars [number];
	int i;
	for(i=0; i<number; i++)
	{
		cout << "Enter the YEAR of the car: ";
		cin >> a;
		cars[i].setYear(a);
		cout << "Enter the MAKE of the car: ";
		cin >> b;
		cars[i].setMake(b);
		cout << "Enter the MODEL of the car: ";
		cin >> c;
		cars[i].setModel(c);
		cout << "Enter the number of MILES on the car: ";
		cin >> d; 
		cars[i].setMiles(d);
		cout << "Enter the PRICE of the car: ";
		cin >> e;
		cars[i].setPrice(e);
		cout << " " << endl;
	}	 
 
	cout << "Cars listed: " << endl;
	for(int i=0; i<number; i++)
	{
		if(cars[i].getPrice()<=25000)
		{
		cout << "Year: " << cars[i].getYear() << endl;
		cout << "Make: " << cars[i].getMake() << endl;
		cout << "Model: " << cars[i].getModel() << endl;
		cout << "Miles: " << cars[i].getMiles() << endl;
		cout << "Price: $" << cars[i].getPrice() << endl;
		cout << " " << endl;
		cout << "This is a " << cars[i].getYear() << " " << cars[i].getMake() << " " << cars[i].getModel() << " with " << cars[i].getMiles() << " miles on it, for $" << cars[i].getPrice() << "." << endl;
		cout << " " << endl;
		}
	}

	system("pause");
};


This code was a continuation from my last assignment, so the code isn't fully correct according to the assignment yet. For now I just need the algorithms, then I can change the code to get the input from a text file.
Use std::sort by providing a predicate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>

struct compare
{
	bool operator () (const car &lhs, const car &rhs) const
	{
		return lhs.price < rhs.price ||
			 lhs.miles < rhs.miles ||
			 lhs.year > rhs.year;
	}
};

int main()
{
	...
	std::sort(cars, cars + number, compare());
	return 0;
}
Last edited on
If the array is already sorted by model year (according to your description), then use std::stable_sort(). Then you can use std::upper_bound() to find the latest model year for a given price.

Hope this helps.
Topic archived. No new replies allowed.