Thread: how to sort through an array

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    how to sort through an array

    Hi all

    I need to sort through my array, and put all the patients into a queue. The sorting has to be made solely on 'prio', which takes a number from 1-4, where 1 has highest priority.

    Can anyone please give me THE way of doing this, as I have been trying to do so for a couple of days now, with no luck.

    Appreciate it all.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "patientStruct.h"
    
    #define MAX  1000
    
    int main()
    {
    	int kontroltal[9] ={4, 3, 2, 7, 6, 5, 4, 3, 2};//kontroltal til modulo11-algoritme
    	FILE *f = popen("./riget_version3", "r");
       Patient p[MAX];
       int num = 0;
    	int num2;
    	
      if(!f)
      {
         fprintf(stderr, "Failed to open riget_version3\n");
         return 0;
      }
    							 
      while( num < MAX && fscanf(f, "%[^\t]\t%d\t%c\n", p[num].cprnr, &p[num].prio, &p[num].skade) == 3)
      {
    	  /* Process Info */
    		printf("Cprnr: %s\tPrioritet: %d\t Skadesart: %c\n", p[num].cprnr, p[num].prio, p[num].skade);
    		/*Kører for-løkke på cpr-array og regner efter med modulo11-algoriten*/
    			int i;
    			int sum = 0;
    			int num10 = (p[num].cprnr[9]-48);
    			for(i=0;i<9;i++)
    			{
    				int n = (p[num].cprnr[i]-48);
    				num2 = n*kontroltal[i];
    				sum = sum + num2;
    			}	
    			if(num10 %2)
    				strcpy(p[num].sex, "Mand");
    			else
    				strcpy(p[num].sex, "Kvinde");
    			
    			printf("Køn: %s\n", p[num].sex);
    			int modulo11 = sum%11;
    			int kontrolcif = 11-modulo11;
    			if((kontrolcif-num10) == 0)
    				printf("Dit cpr er gyldigt\n");
    			else
    				printf("Dit cpr er ikke gyldigt, du er afvist\n");
    			printf("_________________________________________________________________________\n");
    			num++;
      }
      fclose(f);
      // start en sort funktion her evt. qsort
      return 0;
    }
    Here is my patientstruct.h:
    Code:
    typedef struct
    {
      char cprnr[9];
      int  prio;
      char skade;
      char sex[7];
      int fracture;
    }Patient;

    Appreciate it all.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If I had a struct such as this,
    Code:
    struct sPatient
    {
       char cprnr[9];
       int  prio;
       char skade;
       char sex[7];
       int  fracture;
    };
    Then I'd write my comparison function to use with qsort like this.
    Code:
    int cmp_prio(const void *a, const void *b)
    {
       const struct sPatient *x = a, *y = b;
       return (x->prio > y->prio) - (x->prio < y->prio);
    }
    If there were a structure array such as this into which input was read,
    Code:
    struct sPatient patient[10];
    and the actual number of records obtained were size, then I would call qsort like this.
    Code:
    qsort(patient, size, sizeof patient[0], cmp_prio);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Just a question; are you actually using popen() to access your patient information, or is this a typo?
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    I'm using popen, as riget_version3 is another program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  2. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 07:14 PM
  3. New Sort an Array of Classes
    By ajpeters in forum C++ Programming
    Replies: 11
    Last Post: 09-06-2005, 06:23 PM
  4. how to sort out an array?
    By Chobo_C++ in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2004, 05:33 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM