473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sorting Names in a Structure. How can I sort the names in this structure using bubble

I cannot manage to get the code for sorting the names in alphabetical order. Please help...
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. typedef struct{
  5.     char criminalName[10];
  6.     char criminalCase[20];
  7.     int age;
  8.     char dateCaptured[10];
  9.     int criminalID;
  10.     }criminal;//structure declaration
  11.  
  12. void add();
  13. void view();
  14. void option(int n);
  15. void exit();
  16. int menu();
  17. void sort();
  18. /*function calls*/
  19.  
  20. main(){
  21.     int num;
  22.  
  23.     do{
  24.         num = menu();
  25.         option(num);
  26.     }while(num!=3);
  27.     //loop in choosing an action to be performed by the user
  28. }
  29.  
  30. int menu() {
  31.     int choice;
  32.  
  33.     printf("\nGood day officer! What task would you like to do?\n[1] Add Record\n[2] View record\n[3] Exit\nchoice: ");
  34.     scanf("%d", &choice);//Menu for choosing an action to be performed by the user
  35.  
  36.     return choice;
  37. }
  38.  
  39. void option(int n){
  40.     switch(n){
  41.         case 1:        add();//adding records of the criminal
  42.                 break;
  43.  
  44.         case 2:     view();//viewing the records of the criminal
  45.                 break;
  46.  
  47.         case 3:        printf("Goodbye! Thank you for using this program.\n");//Goodbye Message
  48.                 break;
  49.  
  50.         default:     printf("Error! Choice Invalid!\n");//if the option entered is greater than 3
  51.     }
  52. }
  53.  
  54.  
  55. int i, j;
  56. char temp[7];
  57. criminal crime[10];//variable declarations
  58.  
  59. void add(){
  60.  
  61.     for(i=0; i<10; i++){
  62.  
  63.         printf("Enter criminal's name: ");
  64.         scanf("%s", crime[i].criminalName);
  65.  
  66.         printf("Enter criminal case: ");
  67.         scanf("%s", crime[i].criminalCase);
  68.  
  69.         printf("Enter criminal age: ");
  70.         scanf("%d", &(crime[i].age));
  71.  
  72.         printf("Enter the date the criminal was captured: ");
  73.         scanf("%s", crime[i].dateCaptured);
  74.  
  75.         printf("Enter criminal ID: ");
  76.         scanf("%d", &(crime[i].criminalID));
  77.  
  78.         printf("\n");
  79.     }
  80. }//data to be filled up by the user
  81.  
  82. void view(){
  83.  
  84.         for(i=0; i<10; i++){
  85.         printf("Name: %s \nCase: %s \nAge: %d \nDate Captured: %s \nID: %d \n", crime[i].criminalName, crime[i].criminalCase, crime[i].age, crime[i].dateCaptured, crime[i].criminalID);
  86.         /*printing the data entered by the user*/
  87.  
  88.         printf("\n");
  89.  
  90.     }
  91.  
  92. }//viewing the records of criminals
  93. void sort(){
  94.          for ( i = 0 ; i <= 3 ; i++ )
  95.     {
  96.         for ( j = 0 ; j <= 3 - i ; j++ )
  97.         {
  98.             if ( strcmp ( crime[j], crime[j + 1] ) > 0 )
  99.             {
  100.                 strcpy ( temp, crime[j] ) ;
  101.                 strcpy ( crime[j], crime[j + 1] ) ;
  102.                 strcpy ( crime[j + 1], temp ) ;
  103.             }
  104.         }
  105.     }
  106. }          /* end of sort() */        
  107.  
  108.  
  109. //end of program
Sep 26 '12 #1
3 2435
weaknessforcats
9,208 Expert Mod 8TB
The code has many errors that indicate you are coding before writing down your logic. The code is a long way from compiling.

In the sort function, you need to swap two crime variables. You cannot do this using strcpy. You will have to write a function to copy one crime variable to another since tou have to copy each member of the struct. Use this function in the sort to swap crime variables.

Likewise, strcmp compares two strings and not two crime variables. Again write a function that compares two crime variables using the addresses of the variables plus another argument that says what is being sorted. The criminalName? The criminalCase? Try have this function return the same values as strcmp since programmers are used to those values.

I suggest you write these two functions first and get them working before you write the sort function.
Sep 26 '12 #2
donbock
2,426 Expert 2GB
Actually, you don't need to write a copy function because Standard C allows you to use assignment to copy one structure variable to another. That is:
Expand|Select|Wrap|Line Numbers
  1. criminal v1;
  2. criminal v2;
  3. v1 = v2;
Notice that this simple form might not be appropriate if the structure contains pointers to other variables. In that case you would have to decide on a pointer-by-pointer basis whether it makes more sense to copy only the pointer value or to allocate a new instance of the thing being pointed at and copy it too. Anything more that simple assignment would indeed call for a copy function. Simple assignment is usually sufficient for sort-swapping even when the structure contains pointers. Fortunately, you don't have to worry about this issue because your structure does not contain pointers.
Sep 26 '12 #3
donbock
2,426 Expert 2GB
A frequent idiom for sort-comparison functions is:
Expand|Select|Wrap|Line Numbers
  1. int compare_elem(const void *elem1, const void *elem2);
The return value is
  • negative if *elem1 < *elem2
  • zero if *elem1 == *elem2
  • positive if *elem1 > *elem2
You don't have to follow this idiom if you don't want to. The first change I would be tempted to make is to use "const criminal *" instead of "const void *".
Sep 26 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Håvard Olerud Eriksen | last post by:
I've got an array of arrays that I need to sort on preferably one of the indices of the nested array, keeping the order of the outside array. The structure is like this: array( => array(key =>...
1
by: Chris R. | last post by:
Hi everyone. I am trying to finish my homework, but I seem not to figure out how to fix this structure that seems to make wrong output. What this program should do is use structure to save the...
4
by: Excluded_Middle | last post by:
how to convert these two notation in pointer form: 1 - list = value; (explaination) - list is an array of structure of type M. - count is integer index - value is a structure of type M. 2 -...
4
by: silverburgh.meryl | last post by:
I am currently access this newsgroup thru Google group web interface. Is it possible to access this newsgroup using Thunderbird? Thank you.
31
by: Lane Straatman | last post by:
void s_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { size_t bytes; unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap; array = base;...
9
by: nano2 | last post by:
Hi , Have the following case passing in structure pointers and returning structure pointers can anyone spot whats wrong with this structure name is structCar void callfn( ){
2
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hello, Suppose I have defined a structure.... Structure RenameDocs Public Facility As String Public Document As String Public Subtitle As String Public Page As String End Structure
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
0
by: neehakale | last post by:
Can anybody pls tel me...how it is possible to sort by names using say bubble sort algorithm.........
2
by: Deepa koruturu | last post by:
Hi How to Retrieve Table Names And Column Names in perticuler schema using oracle
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.