Array sorthig prog


  • program to enter five names and sort them alpha beticaly

#include<iostream.h>
#include<string.h>
void main()
{
char n[5][20],k[20];
int i,j,m;
cout<<"enter names of five students"<<endl;
  for(i=0;i<=4;i++)
{
cin>>n[i];
}
 for(i=0;<=3;i++)
{
for(j=i+1;j<=4;j++)
{
m=strcmp(n[i],n[j]);
if(m>0)
{
strcpy(k,n[i]);
strcpy(n[i],n[j]);
strcpy(n[j],k);
}
}
}
cout<<"The names in sorted order are"<<endl;
for(i=0;i<=4;i++)
{
cout<<n[i]<<endl;
}
}

note:-it's just a sudo code if its not executing on your device make some changes in the further code this is only for understanding logic.
thankyou for visiting us stay tuned for learning and more intresting u can ask questions in comment section check other blogs for more info. comment mail for compitative cading thankyou.👍😎🙏

Post a Comment

0 Comments