Sabtu, 01 November 2014

Program Sorting dengan C++

Berikut ini adalah contoh sebuah program untuk mengurutkan nama/kata atau program sorting dengan menggunakan bahasa C. Teman-teman boleh menggunakan borland C++ atau program aplikasi yang lain. Berikut kode programnya, selamat mencoba!

Program :


#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

main (){
int i,j, b, k, m, jumlah;
char jwb;
char nama[100][100], sementara[100];
cout<<endl;
cout<<"\t\t\tProgram Pengurutan NAMA "<<endl;
cout<<"\t\t\tPuji Lestari (13.02.8576)"<<endl;
cout<<"\t\t======================================\n";
input:
printf ("\nInput jumlah nama = "); scanf ("%i", &jumlah);
cout<<"___________________________________________"<<endl;
for (i=1;i<=jumlah;i++)
            {

            printf ("Input nama ke %i : ", i); scanf ("%s", &nama[i]);
            if (i>1)
                        {
                        for (j=1;j<=(i-1);j++)
                                    {
                                    b=(strcmp(nama[i], nama[j]));
                                    if (b<=0)
                                                {
                                                strcpy (sementara, nama[i]);
                                                for (k=(i-1);k>=j;k--)
                                                            {
                                                            m=(k+1);
                                                            strcpy (nama[m], nama[k]);
                                                            }
                                                            strcpy (nama[j], sementara);
                                                }
                                    }
                        }
            }

cout<<endl;
cout<<"________________________________\n";
printf("| Hasil Urutan : \n");
cout<<"________________________________\n";
for (i=1;i<=jumlah;i++)
            {
  gotoxy(1,12+i+jumlah);
    cout<<"|";
    gotoxy(7,12+i+jumlah);
    cout<<"|";
    gotoxy(32,12+i+jumlah);
    cout<<"|";

    gotoxy(4,12+i+jumlah);
    cout<<i;
    gotoxy(9,12+i+jumlah);

            printf ("%s\n",nama[i]);
            }
gotoxy(1,12+i+jumlah);
cout<<endl<<"________________________________";
cout<<endl;
cout<<"Apakah akan menginputkan lagi? Y/N ";
cin>>jwb;
if(jwb=='Y' || jwb=='y')
goto input;
else
cout<<"Terimakasih";

getch(); }



Screenshotnya :


2 komentar:

  1. itu pake sorting tipe apa? bubble ka? selection ka?

    BalasHapus
  2. itu pake sorting tipe apa? bubble ka? selection ka?

    BalasHapus

 
Copyright © . Explore Ilmu - Posts · Comments
Theme Template by BTDesigner · Powered by Blogger