Solved

sorting a structured array

Posted on 1999-01-13
10
205 Views
Last Modified: 2010-04-15
hope somebody could really help me quick.  here's the problem:
note: i'm using UNIX OpenPlatform V with C language....
below is a sample structure -
typedef struct
{
    int emplno;
    char emplname[25+1];
    char date[10+1];
    long reghrs;
    long vachrs;
    long sickhrs;
} EMPLOYEE;

main()
{
    EMPLOYEE emp;
note: the number of employees is unknown.
/******************************************/
now the problem, how would i sort this database by date, by emplno and then by reghrs?  this report should be able to run at anytime and number of records in the database is unknown.  do i always need to calculate how many records the database has?

please, if anybody has ideas... i really need help as fast as i can.  i'm running out of people to ask.  thanks a bunch.
zandy1.
0
Comment
Question by:zandy1
[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
  • 6
  • 3
10 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1255834
What  do you mean the number is unknow. If it`s unknow by compile-time nor probelm

int my_comp (void *p1, void *p2)
{
  EMPLOYEE *pe1=p1;
  EMPLOYEE *pe2=p2;
  int comp;

  if ((comp=strncmp (pe1->date,pe2->date,11)) != 0)
     return (comp);
  if (pe1->emplno != pe2->emplno)
     return (pe1->emplno - pe2->emplno)
  if (pe1->reghrs >pe1->reghrs)
     return (1);
 if (pe1->reghrs <pe1->reghrs)
     return (1);
return (0);
}


EMPLOYEE *pemp;

anz=100; /* could be any number */
pemp = (EMPLOYEE *)calloc(anz;sizeof(EMPLOYEE));

get the data (by reading in from a file or somethink else */
qsort (pemp,anz,sizeof(EMPLOYEE),my_comp);

0
 

Author Comment

by:zandy1
ID: 1255835
thank you for the reply but i still don't get it.  from the hint you replied to me, i did the following function changing the sort a bit. THE STRUCTURE IS STILL THE SAME! i would like it to sort now by emplno then be date and nothing works. it keeps printing the last record after it sorts the file. if anybody could give me an answer and put me in a correct way, i'd really appreciate it. PLEASE help anybody.... below is the function:
/*----------------------*/
PRWK prwk;

int rdprwk(char *whsin, PRWK *prwkrec)
{
   PRWK *tmpprwk;
   int cnt = 100;

   fp = fopen(filename,"r+");  /*open file*/
   if(fread(&prwk,sizeof(PRWK),1,fp)
   do { printf(".....etc");           /*read database unsorted*/
        fread(&prwk,sizeof(PRWK,1,fp);
      }while(!feof(fp));

   tmpprwk=(PRWK *)calloc(cnt,sizeof(PRWK));  /*per example*/
   qsort(tmpprwk,cnt,sizeof(PRWK),my_comp);  /*per example*/
   for(ctr=0,ctr<cnt;ctr++)
   {printf("....etc"); }    /*problem: only prints last record*/
   
   /*------MY_COMP FUNCTION----*/
int my_comp(const void *p1,const void *p2)
{
    PRWK *pe1=p1;
    PRWK *pe2=p2;
    int comp;

    if(pe1->emplno != pe2->emplno)
       return(pe1->emplno - pe2->emplno);
    if((strcmp(pe1->date,pe2->date,11)!=0))
       return(comp);
}
/*---------------------END OF FUNCTIONS---*/
0
 
LVL 10

Expert Comment

by:rbr
ID: 1255836
Ok I see your problem. Try this
/*----------------------*/
 
int rdprwk(char *whsin, PRWK *prwkrec)
{
PRWK *tmpprwk;
int cnt = 0,i=0;

fp = fopen(filename,"r+");  /*open file*/
if(fread(&prwk,sizeof(PRWK),1,fp)
do { printf(".....etc");           /*read database unsorted for getting the number*/
               fread(&prwk,sizeof(PRWK,1,fp);
      cnt++;
}while(!feof(fp));
fclose (fp);

tmpprwk=(PRWK *)calloc(cnt,sizeof(PRWK));  /*per example*/
fp = fopen(filename,"r+");  /*open file*/
if(fread(&prwk,sizeof(PRWK),1,fp)
do { printf(".....etc");           /*read database unsorted*/
               fread(&(tmpprwk[i]),sizeof(PRWK,1,fp);
i++;
}while(!feof(fp));
fclose (fp);
qsort(tmpprwk,cnt,sizeof(PRWK),my_comp);  /*per example*/
for(ctr=0,ctr<cnt;ctr++)
          {printf("....etc"); }    /*problem: only prints last record*/
           
          /*------MY_COMP FUNCTION----*/
       int my_comp(const void *p1,const void *p2)
       {
           PRWK *pe1=p1;
           PRWK *pe2=p2;
           int comp;

           if(pe1->emplno != pe2->emplno)
              return(pe1->emplno - pe2->emplno);
           if((strcmp(pe1->date,pe2->date,11)!=0))
              return(comp);
       }
       /*---------------------END OF FUNCTIONS---*/
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:zandy1
ID: 1255837
hi rbr,
again - thank you very much for the quick response and the sample you gave me - unfortunately, it didn't work.  i followed your sample to the letter and i still get the same output.  only the last record read is being printed 4 times (value of cnt).  would you or any of you friends have anymore ideas?  thanks for the time - please don't give up on me.
zandy1
0
 
LVL 84

Expert Comment

by:ozo
ID: 1255838
Where are you printing the record?
Also, where are you setting comp?
0
 
LVL 10

Accepted Solution

by:
rbr earned 130 total points
ID: 1255839
How do you print ?
Try
for(ctr=0,ctr<cnt;ctr++)       printf("%d,%s,%s,%ld,%ld,%ld\n",tmpprwk[ctr].emplno,tmpprwk[ctr].emplname,tmpprwk[ctr].date,tmpprwk[ctr].reghrs,tmpprwk[ctr].vachrs,tmpprwk[ctr].sickhrs);
       
0
 
LVL 10

Expert Comment

by:rbr
ID: 1255840
Change pls
 int my_comp(const void *p1,const void *p2)
              {
                  PRWK *pe1=p1;
                  PRWK *pe2=p2;

                  if(pe1->emplno != pe2->emplno)
                     return(pe1->emplno - pe2->emplno);
                  return(strcmp(pe1->date,pe2->date,11));
                     return(comp);
              }
         
0
 
LVL 10

Expert Comment

by:rbr
ID: 1255841
Sorry mistake with copy and paste use
int my_comp(const void *p1,const void *p2)
                     {
                         PRWK *pe1=p1;
                         PRWK *pe2=p2;

                         if(pe1->emplno != pe2->emplno)
                            return(pe1->emplno - pe2->emplno);
                         return(strcmp(pe1->date,pe2->date,11));
                           
                     }
           
0
 
LVL 10

Expert Comment

by:rbr
ID: 1255842
And use strncmp instead of strcmp if you pass a length. if your strings are \0 terminated you can use strcmp without 11.
0
 

Author Comment

by:zandy1
ID: 1255843
thanks rbr!!
thank you very much for your quick response.
zandy1
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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

635 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