Trouble with array sorting

This is a discussion on Trouble with array sorting within the C++ Programming forums, part of the General Programming Boards category; I'm working on teaching myself a little more about classes. I have a basic idea and I'd like to complicate ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    Trouble with array sorting

    I'm working on teaching myself a little more about classes. I have a basic idea and I'd like to complicate things a little. I know, I know.. "Why the heck would you do that?" So I can learn basically.

    Anyway, i'm going to post this script i'm working on. What I'm doing is collecting first name, last name and age. They get stored in an array and when the user is ready, they are printed to the screen.

    I'm having the hardest time figuring out how to ..
    [list=a][*]Split this into 2 files (a .cpp file and a .h file)[*]give the user the option to sort the output by name or age[/list=a]

    Here's my code..
    PHP Code:
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <iomanip>

    using namespace std;

    class 
    NameDataSet
    {
        public:
            
    char firstName [128];
            
    char lastName [128];
            
    int age;
    };

    int getData(NameDataSetnds)
        {
            
    cout << "\nEnter first name: ";
            
    cin >> nds.firstName;
            
            if ((
    strcmp(nds.firstName"exit") == 0) || (strcmp(nds.firstName"EXIT") == 0))
                    {
                    return 
    0;
                    }
                    
                    
    cout << "Enter last name: ";
                    
    cin >> nds.lastName;
                    
                    
    cout << "Enter age: ";
                    
    cin >> nds.age;
                    
                    return 
    1;
        }
        
    void displayData(NameDataSetnds)
    {
        
    cout << nds.firstName << " " << nds.lastName << " " << nds.age << "\n";
    }

    int main(int nArgcharpszArgs[])
    {
        
    NameDataSet nds[25];
        
        
        
    cout << "Read first, last and age\n" << "Enter 'exit' for first name to exit\n";
        
    int index 0;
    while (
    getData(nds[index]))
        {
        
    index++;
        }
        
        
    cout << "\nInformation: \n";
        
        for (
    int i 0indexi++)
        {
            
    displayData(nds[i]);
        }
        return 
    0;

    Thanks in advance everyone.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Splitting it up in a cpp / h combo is not that bad. You basically just want the class information to go in the header and the implementation in the cpp. Also you might want to make your Display function a method of the class rather then a standard function that takes the class as a param. Here is a simple class split into an h / cpp combo.

    Point.h
    Code:
    #ifndef POINT_H // inclusion guard
    #define POINT_H
    
    class Point
    {
      private:
    
        int m_x, m_y;
    
      public:
    
        // Default constructor
        Point( ) : m_x(0), m_y(0) { }    
    
        // Overloaded constructor
        Point( int x, int y ) : m_x(x), m_y(y) { }
    
        // Set methods
        void SetX( int x );
        void SetY( int y );
    
        // Get methods
        int GetX( void ) const;
        int GetY( void ) const;
    };
    Point.cpp
    Code:
    #include "Point.h" 
    
    void Point::SetX( int x )
    {
      m_x = x;
    }
    
    void Point::SetY( int y ) 
    {
      m_y = y;
    }
    
    int Point::GetX( void ) const
    {
      return m_x; 
    }
    
    int Point::GetY( void ) const
    {
      return m_y;
    }
    Don't worry about the point details but that should give you an idea how to split it up. Normally you would want the Get and Set methods to e in the class and not the cpp because they are one liners but I just wanted to show you how you would put them in their own cpp.

    As for the different sorting. Just have an enumeration with all the different ways the class can be sorted and store that in the class. Then make a method to set the sort method and retrieve the current method of sorting.

    If you need any more help let me know.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    Wow. Thats the best description I've seen of how to split a file. Thanks!

    Now, i'm pretty new to C++ so the sorting thing kinda left me scratching my head.

    The enumeration. How would I have one and how would I set it with all the different ways to sort?

    I know how to give the user the option to choose which method they want..but I can't figure out how to actually do it. I've been banging my head against this bubble sorting thing but I cannot figure out how to use it in my code. Any ideas?

    Thanks for your help so far!

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    struct Employee
    {
       int idNumber;
       float salary;
       string name;
       //etc.
    };
    
    //declare sorting functions
    void sortbyID(Employee *);
    void sortbySalary(Employee*);
    void sortbyName(Employee*);
    
    //in main, declare variables
    Employee employees[10];
    int choice;
    bool stop = false;
    
    //fill Employee array, somehow;
    
    //allow user to choose task
    while(!stop)
    {
      cout << "enter choice as to how you want to sort" << endl;
      cout << "1. sort by idNumber" << endl;
      cout << "2. sort by salary" << endl;
      cout << "3. sort by name" << endl;
      cout << "4. exit; 
    
      cin >> choice;
    
      switch(choice)
      {
         case 1:
           sortbyID(employees);
           break;
         case 2:
           sortbySalary(employees);
           break;
         case 3:
           sortbyName(employees);
           break;
         case 4:
           stop = true;
           break;
         default:
            cout << "not a valid selection" << endl;
      }
    
       if(choice != 4)
        //display sorted array here
    }
    
    //define sorting functions here
    void sortbyID(Employee * employees)
    {
       //sort algorhithm using employees[index].ID as variable to sort on. 
       //sort entire Employee at a time, not just the ID
    }
    
    //etc.
    templatize at your desire
    Last edited by elad; 09-19-2003 at 12:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Any intelligent way sorting an array?
    By choykawairicky in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 01:50 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM

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