Telephone numbers of important customers
are recorded as follows:
Full name Telephone number
Joseph Louis Lagrange 869245
Jean Robert Argand
900823
Carl Freidrich Gauss
806788
----- ---
----- ---
It is desired to prepare a revised
alphabetical list with surname (last name) first, followed by a comma and the
initials of the first and middle names. For example,
Argand,J.R
We create a table of strings, each
row representing the details of one person, such as first_name, middle_name,
last_name, and telephone_number. The columns are interchanged as required and
the list is sorted on the last_name. Fig.8.12 shows a program to achieve this.
PROCESSING OF CUSTOMER LIST
Program
#define
CUSTOMERS 10
main( )
{
char
first_name[20][10], second_name[20][10],
surname[20][10],
name[20][20],
telephone[20][10], dummy[20];
int
i,j;
printf("Input names and telephone
numbers \n");
printf("?");
for(i=0; i < CUSTOMERS ; i++)
{
scanf("%s %s %s %s",
first_name[i],
second_name[i], surname[i],
telephone[i]);
/* converting full name to surname
with initials */
strcpy(name[i], surname[i] );
strcat(name[i], ",");
dummy[0] = first_name[i][0];
dummy[1] = '\0';
strcat(name[i], dummy);
strcat(name[i], ".");
dummy[0] = second_name[i][0];
dummy[1] = '\0';
strcat(name[i], dummy);
}
/* Alphabetical ordering of surnames
*/
for(i=1; i <= CUSTOMERS-1;
i++)
for(j=1; j <= CUSTOMERS-i;
j++)
if(strcmp (name[j-1], name[j])
> 0)
{
/* Swaping names */
strcpy(dummy,
name[j-1]);
strcpy(name[j-1],
name[j]);
strcpy(name[j],
dummy);
/* Swaping telephone numbers
*/
strcpy(dummy,
telephone[j-1]);
strcpy(telephone[j-1],telephone[j]);
strcpy(telephone[j],
dummy);
}
/* printing alphabetical list */
printf("\nCUSTOMERS LIST IN
ALPHABETICAL ORDER \n\n");
for(i=0; i < CUSTOMERS ; i++)
printf(" %-20s\t %-10s\n", name[i],
telephone[i]);
}
Output
Input names and telephone numbers
?Gottfried Wilhelm Leibniz 711518
Joseph Louis Lagrange 869245
Jean Robert Argand 900823
Carl Freidrich Gauss 806788
Simon Denis Poisson 853240
Friedrich Wilhelm Bessel 719731
Charles Francois Sturm 222031
George Gabriel Stokes 545454
Mohandas Karamchand Gandhi 362718
Josian Willard Gibbs 123145
CUSTOMERS LIST IN ALPHABETICAL ORDER
Argand,J.R 900823
Bessel,F.W 719731
Gandhi,M.K 362718
Gauss,C.F 806788
Gibbs,J.W 123145
Lagrange,J.L 869245
Leibniz,G.W 711518
Poisson,S.D 853240
Stokes,G.G 545454
Sturm,C.F 222031
No comments:
Post a Comment