Sorting structures

This is a discussion on Sorting structures within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm trying to figure out how to sort stuctures according to a float value called v. It almost works, ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    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.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    if(relprime(j,i))
    you check if relprime is true? it returns an int, not a boolean
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Of course, I am wrong
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    Your code compares the values, but it doesn't sort them. How do you sort the stuctures?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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());
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    laserlight is right, if you're going to use C++, for heavens sake use std::sort().

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    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.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    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)

  12. #12
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    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...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Structures were originally designed to hold data.
    Then classes were invented!
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  15. #15
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    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.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  3. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 02:02 PM
  4. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21