Thread: sorting?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    1

    Question sorting?

    this was given by my teacher during one of our quizzes. sadly, i failed that one and up to now, i can't figure what to do with it. our discussion went as far as single dimensional arrays. i was wondering if you could give me an idea on what to do.

    Output looks like this:

    a] Add entry
    b] View
    c] exit

    * i used switch here.

    when a is selected:

    employee name:
    employee number:
    department:
    salary:

    add another one? [y/n]

    *still no problem here. problem starts when you need to view all that you have entered. what command should i use to be able to do that?

    name employee number department salary



    total salary of employees:

    i really am stumped...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I've thought about whether to submit this post or not, as it may complicate things more than help. But I decided what the heck, it may be helpful, particularly if you are using an array of struct/class. So here goes.

    With arrays there isn't a straightforward way to know when you have reached the end of data in the array. For example, if an array can hold up to ten Employees, but there is only eight Employees in the array at the moment, how do you know that so you can stop printing after eight rather than ten loops? Well, you keep track as you add them/delete them so you know without question how many there are and stop printing after you've reached the appropriate number. Alternatively, you use some other technique that indicates when you have reached the end of actual data. Then you can add up the elements as you print them out, rather than when you add/delete them (this means you can't access the number of elements in the array willy nilly like you could if you keep track as you add/delete them, but it works well under a number of circumstances). To do this you need to assign all of the elements of the array with some value that will never be used by an object of the type to be stored in the array (either explicitly with objects of a primitive type or via the default constructor with user defined struct/classes), and when you find the default value(s) looping through the array you know you have reached the end of the data in the array. In this case, if you are using an array of struct then restrict it so an employee number can never be negative and have the default (null) value of the Employee number be -1 or let Employee name be "" , that is an empty string, as default, which can never be for an actual Employee, either.

    For example.
    Code:
    Here's a C++ struct called Employee
    struct Employee
    {
       Employee();
       string name;
       int number;
    };
    
    Now here's a definition for the default constructor 
    Employee::Employee()
    {
      name = ""; //name is empty
      number = -1;  //number is -1
    }
    
    Now declare an array of 10 Employees.
    Employee employees[10];
    
    The declaration of the array of Employees will cause the compiler to call the default constructor for the Employee struct/class and intialize all elements of the array to the default values of the data members provided by the default constructor.
    
    Then if you want to print out the Employees in the array (either before or after you enter some Employee data) you can do this:
    
    int i =  0;
    while(employee[i]l.name != "" && employee[i].number != -1)
    {
       cout << employee[i].name << ' ' << employe[i].number << endl;
       ++i;
    }
    
    cout << "the number of employees is in the array" << i << endl;
    
    If you use null terminated char arrays instead of the STL string class then you will need to adjust for that syntax as well, but the underlying principle holds.  You shouldn't need to check both data members for the null value either.  Just one should do.
    If you are using a list or a vector or some container other than an array, then there are other ways to detect the end of user data and/or built in methods to determine size without your having to devise a mechanism to do it. But I doubt that you are using lists or vectors, yet.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Oops, forgot to view forum site. I hardly ever check in here as C isn't my favorite. My apologies.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Suggestions

    if you know no. of records that will be entered you can use array of structure.
    Saravanan.T.S.
    Beginner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM