Code which arranges elements in binary file.

Programming
Tutor: None Selected Time limit: 1 Day

Write piece of code (function) that arranges char elements in binary file in alphabetical order.

May 13th, 2015

Here is source code of the C program to sort the names in analphabetical order. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to read N names, store them in the form of an array
  3.  * and sort them in alphabetical order. Output the given names and
  4.  * the sorted names in two columns side by side.
  5.  */
  6. #include <stdio.h>
  7. #include <string.h>
  8. void main()
  9. {
  10.     char name[10][8], tname[10][8], temp[8];
  11.     int i, j, n;
  12.     printf("Enter the value of n \n");
  13.     scanf("%d", &n);
  14.     printf("Enter %d names n", \n);
  15.     for (i = 0; i < n; i++)
  16.     {
  17.         scanf("%s", name[i]);
  18.         strcpy(tname[i], name[i]);
  19.     }
  20.     for (i = 0; i < n - 1 ; i++)
  21.     {
  22.         for (j = i + 1; j < n; j++)
  23.         {
  24.             if (strcmp(name[i], name[j]) > 0)
  25.             {
  26.                 strcpy(temp, name[i]);
  27.                 strcpy(name[i], name[j]);
  28.                 strcpy(name[j], temp);
  29.             }
  30.         }
  31.     }
  32.     printf("\n----------------------------------------\n");
  33.     printf("Input NamestSorted names\n");
  34.     printf("------------------------------------------\n");
  35.     for (i = 0; i < n; i++)
  36.     {
  37.         printf("%s\t\t%s\n", tname[i], name[i]);
  38.     }
  39.     printf("------------------------------------------\n");
  40. }

$ cc pgm32.c
$ a.out
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
 
----------------------------------------
Input Names    Sorted names
------------------------------------------
heap           class
stack          heap
queue          object
object         program
class          project
program        queue
project        stack


May 13th, 2015

Are you studying on the go? Check out our FREE app and post questions on the fly!
Download on the
App Store
...
May 13th, 2015
...
May 13th, 2015
Dec 9th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer