Bubble sorting trouble

I just need to fix one thing but I am unsure what I need to do I know where to issue lies but am unsure how to fix it I know I must change the data type in the Circle class but I am unsure what data type needs to be changed or added.

#pragma once
// This header file contains the Circle class declaration.
#ifndef CIRCLE_H
#define CIRCLE_H
#include <cmath>

class Circle
{
private:
double radius; // Circle radius
int centerX, centerY; // Center coordinates

public:
Circle() // Default constructor
{
radius = 1.0; // accepts no arguments
centerX = centerY = 0;
}

Circle(double r) // Constructor 2
{
radius = r; // accepts 1 argument
centerX = centerY = 0;
}

Circle(double r, int x, int y) // Constructor 3
{
radius = r; // accepts 3 arguments
centerX = x;
centerY = y;
}

void setRadius(double r)
{
radius = r;
}

int getXcoord()
{
return centerX;
}

int getYcoord()
{
return centerY;
}

double findArea()
{
return 3.14 * pow(radius, 2);
}
}; // End Circle class declaration
#endif

// This program uses an array of objects.
// The objects are instances of the Circle class.
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Circle.h" // Circle class declaration file
using namespace std;

const int NUM_CIRCLES = 4;
void bubbleSort(Circle[], int);


int main()
{
Circle circle[NUM_CIRCLES]; // Define an array of Circle objects

// Use a loop to initialize the radius of each object
for (int index = 0; index < NUM_CIRCLES; index++)
{
double r;
cout << "Enter the radius for circle " << (index + 1) << ": ";
cin >> r;
circle[index].setRadius(r);
}

bubbleSort(circle, NUM_CIRCLES);

// Use a loop to get and print out the area of each object
cout << fixed << showpoint << setprecision(2);
cout << "\nHere are the areas of the " << NUM_CIRCLES
<< " circles.\n";
for (int index = 0; index < NUM_CIRCLES; index++)
{
cout << "circle " << (index + 1) << setw(8)
<< circle[index].findArea() << endl;
}
return 0;
}

void bubbleSort(Circle circle[], int size)
{
int temp;
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (circle[j] > circle[j + 1])
{
swap(circle[j], circle[j + 1]);
}
}
}
}
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
1
2
//  In bubbleSort()
if (circle[j] > circle[j + 1])

Your class doesn't know how to compare two circles. i.e. You do not have a > operator.
As one file, consider:

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
#include <iostream>
#include <iomanip>
#include <utility>

class Circle {
private:
	double radius {1.0};
	int centerX {}, centerY {};

public:
	Circle() {}
	Circle(double r) : radius(r) {}
	Circle(double r, int x, int y) : radius(r), centerX(x), centerY(y) {}

	void setRadius(double r) { radius = r; }
	int getXcoord() const { return centerX; }
	int getYcoord() const {return centerY; }
	double findArea() const { return 3.14 * radius * radius; }
};

constexpr unsigned NUM_CIRCLES {4};
void bubbleSort(Circle[], unsigned);

int main() {
	Circle circle[NUM_CIRCLES];

	for (unsigned index {}; index < NUM_CIRCLES; ++index) {
		double r {};

		std::cout << "Enter the radius for circle " << (index + 1) << ": ";
		std::cin >> r;

		circle[index].setRadius(r);
	}

	bubbleSort(circle, NUM_CIRCLES);

	std::cout << std::fixed << std::showpoint << std::setprecision(2);
	std::cout << "\nHere are the areas of the " << NUM_CIRCLES << " circles:\n";

	for (unsigned index {}; index < NUM_CIRCLES; ++index)
		std::cout << "circle " << (index + 1) << std::setw(8) << circle[index].findArea() << '\n';
}

void bubbleSort(Circle circle[], unsigned size) {
	for (unsigned i {}; i < size - 1; ++i)
		for (unsigned j {}; j < size - i - 1; j++)
			if (circle[j].findArea() > circle[j + 1].findArea())
				std::swap(circle[j], circle[j + 1]);
}


This does the comparison using the circle area. As circle area is used several times it would be better to compute this once within the constructors and stored as part of the class.
Topic archived. No new replies allowed.