# Sorting structures

• 05-07-2006
sybariticak47
Sorting structures
Hi! I'm trying to figure out how to sort stuctures according to a float value called v. It almost works, but I don't completely understand it. If someone could help that would be great!

Code:

```#include <iostream> #include <stdlib.h> #include <fstream> using namespace std; typedef struct fract fract; struct fract {     float v;         int num;         int den;        }; int cmp(const void *va, const void *vb)// compare fuction {         return 10*(((fract*)va)->v - ((fract*)vb)->v); } fract val[5000]; int relprime(int a, int b)          {                                    if(a > b)       return 1;   if(a%2 == 0 && b%2 == 0)       return 0;   for(int i = 3; i*i <= a; i += 2)       if(a%i == 0 && b%i == 0)         return 0;   return 1; } int main() {     int N,k=0;     cin >> N;     for(float i=2; i<=N; i++)             for(float j=1; j<i; j++)                     if(relprime(j,i))                     {                                     val[k].num=j;                                     val[k].den=i;                                     val[k].v=j/i;                                     cout << val[k].num << " " << val[k].den << " " << val[k].v << "\n";                                     k++;                     }         cout << "\n";                  qsort(val, k, sizeof(val[0]), cmp);//sorting algorithm     cout << 0 << "/" << 1 << "\n";     for(int i=0; i<k; i++)                       cout << val[i].num << "/" << val[i].den << "\n";     cout << 1 << "/" << 1 << "\n";     int y;     cin >> y;     return 0; }```
Thanks a bunch.
• 05-07-2006
Ideswa
Code:

`if(relprime(j,i))`
you check if relprime is true? it returns an int, not a boolean
• 05-07-2006
7stud
See if this helps:
Code:

```#include <iostream> using namespace std; struct fract {     float v;         int num;         int den;        }; int compareFracts(const fract* f1, const fract* f2) {         return 10 * (f1->v - f2->v); } int main() {         fract f1 = {.5, 1, 2};         fract f2 = {.75, 3, 4};         int result = compareFracts(&f1, &f2);         cout<<result<<endl;         return 0; }```
• 05-07-2006
7stud
Quote:

if(relprime(j,i))

you check if relprime is true? it returns an int, not a boolean
Try this:
Code:

```if(1) cout<<true<<endl;         if(0) {         cout<<true<<endl; } else {         cout<<false<<endl; } if(-1) cout<<true<<endl;```
• 05-07-2006
Ideswa
Of course, I am wrong :o
• 05-07-2006
sybariticak47
Your code compares the values, but it doesn't sort them. How do you sort the stuctures?
• 05-07-2006
laserlight
Quote:

Your code compares the values, but it doesn't sort them. How do you sort the stuctures?
Have you considered using std::sort()?

Code:

```#include <algorithm> struct fract {         float v;         int num;         int den; }; struct cmpFract {         bool operator()(const fract& lhs, const fract& rhs) const {                 return lhs.v < rhs.v;         } }; int main() {         const int fracts_size = 5;         fract fracts[fracts_size];         fracts[0].v = 2.5;         fracts[1].v = 1.2;         fracts[2].v = 3.0;         fracts[3].v = 2.2;         fracts[4].v = 5.0;         std::sort(fracts, fracts + fracts_size, cmpFract()); }```
• 05-08-2006
7stud
Quote:

Your code compares the values, but it doesn't sort them. How do you sort the stuctures?
How would you sort the integers 4, 2, 6?
• 05-08-2006
iMalc
laserlight is right, if you're going to use C++, for heavens sake use std::sort().
• 05-08-2006
indigo0086
Question laserlight, how come the third argument in the sort method is a structure? I thought it was supposed to be a predicate function that returns a bool.
• 05-08-2006
sigfriedmcwild
It's a struct with operator() defined.

Which means it acts as if it was a function (operator() is called which in this case is a predicate comparing 2 structures)
• 05-08-2006
ElastoManiac
I hate it when people put functions in structures. What are classes used for then???
And howcome your tab size is diffrent in some portions of code sybariticak47?
You should first learn to write, then learn to write programs...
• 05-08-2006
laserlight
Quote:

I hate it when people put functions in structures. What are classes used for then?
A struct is a class that has public access for members by default. A class is a struct that has private access for members by default. So if you hate it when people put functions in structs, you hate it when people put functions in classes, i.e. you hate member functions for some inane reason.
• 05-09-2006
ElastoManiac
Structures were originally designed to hold data.
Then classes were invented!
• 05-09-2006
prog-bman
struct is the same as class in C++. Elasto, it is more of a matter of preference to put member functions in there. I personally don't but there is nothing wrong with it.