18

Given a structure array (in C) I am attempting to print out the results in groups of gender and in sub order by numerical order. For example:

struct employee{
char gender[13]
char name[13];
int id;
};

Say I define the structure array like so:

struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};

How could I go about printing the results like

1234 Matt
1235 Josh


2345 Jessica
Mike Trusov
  • 1,958
  • 1
  • 15
  • 23
bardockyo
  • 1,415
  • 6
  • 21
  • 32

3 Answers3

27

You'll need to implement a sorting function that compares the structs as you require

int compare(const void *s1, const void *s2)
{
  struct employee *e1 = (struct employee *)s1;
  struct employee *e2 = (struct employee *)s2;
  int gendercompare = strcmp(e1->gender, e2->gender);
  if (gendercompare == 0)  /* same gender so sort by id */
    return e1->id - e2->id;
  else
    return -gendercompare;  /* the minus puts "male" first as in the question */
}

And then use qsort from the standard library.

qsort(data, count, sizeof(struct employee), compare);

Inside the compare function you may want to check for id being equal, then you can sort by name (also using strcmp()) however you like.

Edit: Just compiled and fixed this up. Here's a little test program

    #include <stdio.h>
    #include <stdlib.h>

    struct employee{
      char gender[13];
      char name[13];
      int id;
    };

    int compare(const void *s1, const void *s2)
    {
      struct employee *e1 = (struct employee *)s1;
      struct employee *e2 = (struct employee *)s2;
      int gendercompare = strcmp(e1->gender, e2->gender);
      if (gendercompare == 0)  /* same gender so sort by id */
        return e1->id - e2->id;
      else
        return -gendercompare;
    }

    main()
    {
      int i;
      struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};

      for (i = 0; i < 3; ++i)
        printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);

      qsort(info, 3, sizeof(struct employee), compare);

      for (i = 0; i < 3; ++i)
        printf("%d\t%s\t%s\n", info[i].id, info[i].gender, info[i].name);
    }

With output:

$ ./a.exe
1234    male    Matt
2345    female  Jessica
1235    male    Josh
1234    male    Matt
1235    male    Josh
2345    female  Jessica
Nimantha
  • 6,405
  • 6
  • 28
  • 69
kallikak
  • 844
  • 6
  • 13
  • 1
    I think we wants "sub order by numerical order" – dreamcrash Nov 14 '12 at 04:16
  • Here is a sort of off question but say i define a function. How would I pass the conents of employee info to that function? – bardockyo Nov 14 '12 at 04:42
  • If someone has problem understanding this, can refer to https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/qsort.htm – Suraj Jain Oct 24 '17 at 17:01
2

Use your favourite sorting algorithm on the struct array. When comparing two elements of the array to decide which is "greater", compare their genders; if the genders are the same, compare their numbers. (You may want to define a separate function to do this comparison, to make things clearer.) Afterwards, print the sorted array in order using the desired formatting. Keep track of when the gender switches from male to female so you can put in an extra three newlines, as in your example.

Edit: to borrow shamelessly from kallikak, you can just pass your comparison function to qsort, but have it return 1 if one struct is "greater", -1 if it is "less" and (if necessary) 0 if it is the same (using the procedure I outlined above). Take a look at How to write a compare function for qsort from stdlib? for help on writing a custom compare function.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
  • Thank you both for the response. I was wondering how to also check for gender while using a sorting algorithm. For example say I am using quicksort if right is < left and left is male then switch? – bardockyo Nov 14 '12 at 04:05
  • See the compare method in my example. You can check any of the fields in your struct while making your decision on ordering. – kallikak Nov 14 '12 at 04:12
0

Think it's easier to understand, as I am weak in pointer

#include<bits/stdc++.h>

using namespace std;


struct employee{
  char gender[13];
  char name[13];
  int id;
};

bool compare(employee s1,employee s2)
{
  return s1.id<s2.id;
}

main()
{
  int i;
  struct employee info[]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}};
  sort(info,info+3,compare);
  for (i = 0; i < 3; i++)
  printf("%d\t%s\t%s\n",info[i].id,info[i].gender,info[i].name);
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Progga Ilma
  • 578
  • 6
  • 8
  • 1
    Isn't this supposed to be a c program. Seems like you have given a cpp version of the code. – Jo_L Apr 19 '22 at 10:54