Ads 468x60px

Minggu, 04 Mei 2014

Pengurutan Sepasang Data (huruf dan angka) berdasarkan Huruf

Berikut program untuk mengurutkan sepasang data yang dimasukkan berdasarkan abjad/huruf dengan menggunakan bahasa C. Caranya cukup guampang sekali, dalam pengurutan data ini saya menggunakan penyimpanan array, apa itu array? array adalah tipe data yang menyimpan jenis data yang sama. Supaya lebih paham silahkan compile kode program dibawah ini:

#include <iostream.h> char nama[5];int nilai[5];void tukar_huruf();main(){              
                for(int i=0;i<5;i++){                                cout<<"Huruf ke-"<<i+1<<" : ";                                cin>>nama[i];                                cout<<"Angka ke-"<<i+1<<" : ";                                cin>>nilai[i];                                cout<<"======================================\n";                }              
                cout<<"Data sebelumnya diurutkan: \n";                for(int a=0;a<5;a++){                                cout<<nama[a]<<"   |   ";                }                cout<<endl;                for(int b=0;b<5;b++){                                cout<<nilai[b]<<"   |   ";                }              
                tukar_huruf();}              
void tukar_huruf(){                int temp,temp2;              
                for (int i=0;i<5;i++){                                for (int ii=4;ii>=i;ii--){                                                if (nama[ii]<nama[ii-1])                                                {                                                                temp=nama[ii];                                                                temp2=nilai[ii];                                                                nama[ii]=nama[ii-1];                                                                nilai[ii]=nilai[ii-1];                                                                nama[ii-1]=temp;                                                                nilai[ii-1]=temp2;                                                }                                }                }              
                cout<<"\n=========================================\n";                cout<<"Urutan Berdasarkan Huruf:\n";                for (int iii=0;iii<5;iii++){                                cout<<nama[iii]<<"   |   ";                }                cout<<endl;                for (int iiii=0;iiii<5;iiii++){                                cout<<nilai[iiii]<<"   |   ";                }}

0 komentar:

Posting Komentar