Thread: Sorting an Array of Structures

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Sorting an Array of Structures

    Hi All,

    I think I have a simple problem here that's befuddling me: an ADT unsorted list that needs to be sorted by item number. I'm having a problem passing that array to a function defined in a class called "List".

    Here's what I have set up so far:

    Code:
    struct DataRecord
    {
    	int        DataNumber;
    	string   DataName;
    };
    
    const int MAX_ITEMS = 50;		
    DataRecord DataItem[MAX_ITEMS];		
    int count;			
    typedef int ItemType;
    
    // defining class
    
    class List
    {
    
    public:
    	
    	List();
    	void SortData();
    
    private:
    
    	int length;
    	ItemType DataItem[MAX_ITEMS];
    	
    };
    
    // *********** MAIN ******************
    
    int main()
    {
    	ReadData( DataItem, count, MAX_ITEMS );
    	//this works.  I can get the data into the array.
    
    	void List::SortData ();          //this doesn't work
    
           return 0;
    }
    
    // ********** List::SortData Function **************
    
     void List::SortData()
     {
            ItemType temp;
            int passCount = 0;
            int minIndex;
            int searchIndex;
    
        for (passCount = 0; passCount < count - 1; passCount++)
        {
          minIndex = passCount;
                   
          for (searchIndex = passCount + 1; 
                searchIndex < count; searchIndex++) 
           {
    
             // evaluate DataNumber
    
             if (DataItem[searchIndex].DataNumber < DataItem[minIndex].DataNumber)
    
             minIndex = searchIndex;
    
             // swap DataItem[minIndex] with DataItem[passCount]
    
             temp = DataItem[minIndex];
             DataItem[searchIndex] = DataItem[passCount];
             DataItem[passCount] = temp;			
    
            } // ends INNER for loop
    		
         } // ends OUTER for loop
    
      return;
     }

    For some reason the Sort function does not recognize the structure variables, specifically, DataNumber.

    Any help or direction would be appreciated,
    bob2509

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    You don't seem to be constructin a List object anywhere, which can be used in main like so:
    Code:
    List myList;
    myList.SortData( );
    The ReadData line works because it its reading the data into the global variable you have declared, they are not being read into a List object whihc I think is what you want to do.
    Last edited by endo; 05-11-2002 at 10:34 AM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    I tried that and this is what I got:

    C:\c++\Project2_beta2\Project2_beta2\Project2_beta 2.cpp(703) : error C2228: left of '.DataNumber' must have class/struct/union type

    It's still not recognizing the structure values.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have two variables called DataItem

    One of them is global (the one you read data into) with
    > ReadData( DataItem, count, MAX_ITEMS );

    The other one is a private member within your List class

    Guess which one your list member function will try and access
    beep - correct, the one in the class.

    They're also of different types - DataRecord and ItemType, which doesn't help matters

    A quick fix is to say which DataItem you want
    Eg.
    Code:
    if ( :: DataItem[searchIndex].DataNumber < :: DataItem[minIndex].DataNumber)
    The :: is the scope operator, which should reference the global var (and not the class member).

    You might want to rethink what is going on here...

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    I put the class in a header file and still get the same error.

    Here's the header file:

    Code:
    const int MAX_ITEMS = 100;		// Variable Decleration
    typedef int ItemType;
    
    // defining class
    
    class List
    {
    
    public:
    	
    	List();
    	void SortInfo();
    	void Print();
    
    private:
    
    	int length;
    	ItemType ItemDB[MAX_ITEMS];
    	
    };
    And here's the function I get the error in:

    Code:
     void List::SortInfo()
     {
    	 ItemType temp;
    	 int passCount = 0;
    	 int minIndex;
    	 int searchIndex;
    
        for (passCount = 0; passCount < count - 1; passCount++)
    	{
    		minIndex = passCount;
    
    		for (searchIndex = passCount + 1; searchIndex < count; searchIndex++) 
    		{
    			if (ItemDB[searchIndex].ItemNumber < ItemDB[minIndex].ItemNumber)
    				minIndex = searchIndex;
    
    			// swap ItemDB[minIndex].ItemNumber with ItemDB[passCount].ItemNumber
    
    			temp = ItemDB[minIndex];
    			ItemDB[searchIndex] = ItemDB[passCount];
    			ItemDB[passCount] = temp;			
    
    		} // ends INNER for loop
    		
    	} // ends OUTER for loop
    
    	return;
     }
    Here's the error being generated:

    C:\c++\Project2_beta2\Project2_beta2\Project2_beta 2.cpp(684) : error C2228: left of '.ItemNumber' must have class/struct/union type

    Please Help.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > typedef int ItemType
    ...
    > ItemType ItemDB[MAX_ITEMS];

    Which is just a long way of saying

    int ItemDB[MAX_ITEMS];

    It has no .members, because it isn't a structure

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Salems right. Nowhere in your class declaration is there a DataRecord variable, and yet, you try to access one in SortData!
    Instead, you have an "ItemType" variable in the class which, I assume, you are reading into with ReadData - which by the way- is not a member function in your class. I have a suggestion. Start over. What is it you are trying to do? What I mean is, what problem are you trying to solve with this class? Classes should not have muddled interfaces, and should not rely on global variables for sure!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Sorry if I'm a bit confusing here. I'm trying to take an unsorted list and sort it by itemNumber. The ReadInfo function takes in the data as unsorted. The SortInfo function then is suppose to sort it. I am not allowed to use pointers or qsort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting an array of structures
    By kisiellll in forum C Programming
    Replies: 15
    Last Post: 04-06-2009, 05:25 AM
  2. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  3. trying to initialize an array of structures
    By dreamgoat in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 05:33 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM