Collation
Here I write a sorting algorithm, flowchart and source code in C Language.
Title: Arrange N names in Ascending Alphabetical Order.
1. Algorithm
Step1: start.
Step2: Declare variables, 2 arrays of character type (tmp[10],name[20][10]) and 2 integer type variables (I,n).
Step3: Read n and read names from the user.
Step4: print the list as it was entered.
Step5: for loop starting from I = 1 to I <> name[i+1] then
Swap name[i] and name[i+1]
End-if
End-for
Step6: print names sorted in ascending order.
Step7: stop.
2. Flowchart.

3.Source Code in C Language.
/*
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
#Author:Abdulla Mustafa. #
#Date-Written:26/3/2003. #
#Date-Compile:26/3/2003. #
#Date-Updated:05/2/2005. #
#Security:public domain. #
#Source-Computer:PC. #
#Object-Computer:PC. #
#Source-Compiler:Turbo C. #
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char tmp[10],name[20][10];
int i,n;
clrscr();
printf("Enter Number of names:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name %d\n",i+1);
scanf("%s",&name[i]);
}
printf("\nName list before Sorting");
for(i=0;i<n;i++)
{
printf("\n%s",name[i]);
}
for(i=0;i<n-1;i++)
{
if((strcmp(name[i],name[i+1])>1))
{
strcpy(tmp,name[i]);
strcpy(name[i],name[i+1]);
strcpy(name[i+1],tmp);
}
}
printf("\nAscending Order\n...........");
for(i=0;i<n;i++)
printf("\n%s",name[i]);
getch();
}
4.input output
Enter Number of Names:3
enter name 1
mohamed
enter name 2
aisha
enter name 3
ali
Name list before sorting
mohamed
aisha
ali
Name list in asceding order
--------------------------
aisha
ali
mohamed

0 Comments:
Post a Comment
<< Home