Sort Name (sort) with C++ Program
This is an example of the sort program to sort the names in C ++:
A. Coding
Program
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
main()
{
int i, j, a, k,
m, jumlah;
char
kata[100][40], sementara[40];
cout<<"\t\t<<<MENGURUTKAN NAMA
13.02.8551>>>\n";
cout<<"=================================================================\n";
cout<<"Berapa jumlah nama yang akan anda urutkan = ";
cin>>jumlah;
for
(i=1;i<=jumlah;i++)
{
cout<<"Input nama ke- "<<i<<" = ";
gets(kata[i]);
if (i>1)
{
for
(j=1;j<=(i-1);j++)
{
a=(strcmp(kata[i], kata[j]));//membandingkan jika kata[i]>kata[j]
maka +, jika sama 0, jika kata[i]<kata[j] maka -
if
(a<=0)
{
strcpy (sementara, kata[i]); //strcpy (var_tuj, var_asal)
for (k=(i-1);k>=j;k--)
{
m=(k+1);
strcpy (kata[m], kata[k]);
}
strcpy (kata[j], sementara);
}
}
}
}
clrscr();
cout<<"\t\t<<<MENGURUTKAN NAMA
13.02.8551>>>\n";
cout<<"=================================================================\n";
cout<<"Hasil nama setelah proses pengurutan menjadi :
\n";
for
(i=1;i<=jumlah;i++)
{
cout<<"Nama ke - "<<i<<"
:"<<kata[i]<<endl;
}
cout<<"==============================SELESAI============================\n";
getch();
}
0 comments:
Posting Komentar