?
Solved

sorting array in a structure

Posted on 2003-11-08
3
Medium Priority
?
248 Views
Last Modified: 2010-04-02
hi,
i am trying sort an array that is in a structure and be able to sort along the other records associated with the array.

here is my structure

struct Employee
{
char Name[20];
char SSN[11];
float Hours;
float Rate;
}

Employee Record; // initialize the structure
...
sort(Record) // call the sorting function
...
Employee sort(Record SO) //i am using selection sort
{
int startscan, minindex, minivalue;
for(startscan = 0; startscan < (size-1); startscan++)
{
minidex = startscan;
minivalue = ??? how do I assign to the array/structure???

i want to sort by the names and when they are sorted to be able to change all other values, so that when i display the sorted arrays, i see the info in sorted order with their respective variables i.e. hours, rate, ssn...

please help
Isa
0
Comment
Question by:mafrenz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9707665
You will need a container (such as an array or linked list) to hold all the structures.
The container provides some sort of relationship between the structures that can
be used to impose order (offset into array, position in list, etc).  Once you have a
container in which items can be ordered, your sort routine simply needs to permute
the order (swap elements in array, etc).

0
 
LVL 4

Accepted Solution

by:
dhyanesh earned 375 total points
ID: 9707677
hi

First you should declare Record as an array of Employee records i.e.

Employee Record[50]                 //i.e. if you have 50 employees

void sort(Employee *SO)         //Here you will be passing pointer to entire array of records which are of type Employee

Since you want to sort by name

strcmp() would be best function to compare two strings.

Also minivalue would be a char array and not int.

char minivalue[20]

then to store in minivalue

strcpy(minivalue,SO[startscan].Name);     //copies name at position startscan to minivalue

Also you have to be careful of

char Name[20]
char SSN[11]

The user can easily enter more characters and it will result in unexpected errors.

Dhyanesh
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9707688
hi

In my code i stated a max of 50 employees. However if this is to be dynamic then link list would be best option

Dhyanesh
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question