PROGRAM
write a program that can sort a list of names and telephone number alphabetically.#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=10;
struct Person { char fname[15];
char lname[25];
long tel;
} td[size],t;
void swap(Person&s1,Person&s2)
{ strcpy(t.fname,s1.fname);
strcpy(t.lname,s1.lname);
t.tel=s1.tel;
strcpy(s1.fname,s2.fname);
strcpy(s1.lname,s2.lname);
s1.tel=s2.tel;
strcpy(s2.fname,t.fname);
strcpy(s2.lname,t.lname);
s2.tel+t.tel;
}
int main()
{ clrscr();
int i,j;
for(i=0;i<size;++i)
{ cout<<"Person"<<i+1<<endl;
cin>>td[i].fname>>td[i].lname>>td[i].tel;
}
for(i=0;i<size;++i)
{ for(j=0;j<size-1-i;++j)
{
if(strcmp(td[j].lname,td[j+1].lname)>0)
swap(td[j],td[j+1]);
else if(strcmp(td[j].lname,td[j+1].lname)==0)
if(strcmp(td[j].fname,td[j+1].fname)>0)
swap(td[j],td[j+1]);
}
}
for(i=0;i<size;++i)
{ cout<<"Person"<<i+1<<endl;
cout<<td[i].lname<<" "<<td[i].fname<<","<<td[i].tel<<endl;
}
getch();
return 0;
}
========================================================================
OUTPUT
Person1
akshay kumar,1111111111
Person2
amir khan,2222222222
Person3
bhavesh poptani,3333333333
Person4
best boss,4444444444
Person5
king khan,5555555555
Person6
master blaster,6666666666
Person7
noman land,7777777777
Person8
salman khan,9999999999
Person9
sameer raj,88888888
Person10
yash raj,0000000000
========================================================================
No comments:
Post a Comment