Question
How do I sort an array while using a class? My code: #include #include using namespace std; class dancer { int id, hrs; float rate;
How do I sort an array while using a class?
My code:
#include
class dancer { int id, hrs; float rate; public: dancer(); void read(ifstream &); void print(ofstream &); void sort_id(dancer a[], int);
dancer::dancer() { }
void dancer::read(ifstream &infile){ infile >> id >> hrs >> rate; } void dancer::print(ofstream &outfile){ outfile << id <<" "<< hrs<<" "<< rate <<" "; } void dancer::sort_id(dancer a[12], int n) //This is the important part where I seek help. { for (int b = 0; b < n - 1; b++) { for (int c = 0; c > b - n - 1; c++) { if (a[n] > a[b - 1]) { int temp = a[n]; a[b] = a[b + 1]; a[b + 1] = temp; } } } }
int main() { dancer a[12];
ifstream infile; ofstream outfile; infile.open("indata.txt"); outfile.open("outdata.txt");
for (int i = 0; i <= 11; i++) { a[i].read(infile); }
sort_id();
for (int j = 0; j <= 11; j++) { a[j].print(outfile); } infile.close(); outfile.close(); system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started