Code which arranges elements in binary file.

User Generated

jbtylf

Programming

Description

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

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

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



Anonymous
Really great stuff, couldn't ask for more.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags