Sabtu, 16 April 2016

E-book How to Solve it by Computer

Kasus 4.1, 4.2, & 4.7 dapat dilihat di : http://hiwilmacleta.blogspot.co.id/2016/04/howtosolveitbycomputer.html
Kasus 4.4 & 4.6 dapat dilihat di :
http://ratihpuspita29.blogspot.co.id/search/label/bab7

4.3
Problem
Find the maximum number in a set of n number
Source Code :

#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void masuk_data(int A[], int n )
{
    int i;
    for(i=0;i<n;i++){
        cout << "Data ke-" << i+1 <<" : " ;
        cin >>A[i];
    }
}
int maksimum2(int a,int b)
{
    if(a>b) return (a);
    else return (b);
}
int maksimum (int data [] ,int n)
{
    int i, temp;
    temp=data[0];
    for(i=0;i<n;i++)
    temp=maksimum2(temp, data[i]);
    return(temp);
}
main()
{
    int n;
    int bilangan[10];
    cout<<"Masukkan Banyak Data : ";
    cin>>n;
    masuk_data(bilangan,n);
    cout << "Nilai terbesar : "<<maksimum (bilangan,n);
    cout<<endl;
    return 0;
}


Kalau di run, hasilnya :


Flowchart :





4.5

Problem

Given a ramdomly ordered array of n elments, partition the element into two subsets such that elements <= x are in one subset and elements >x are in the other subsets.

C++ :
#include <iostream>
using namespace std;

void quicksort(int *arr, int kiri, int kanan){
                int tmp, i=kiri, j=kanan, cetak;
                int pivot=arr[(kiri+kanan)/2];
                                while (i<=j){
                                while (arr[i]<pivot){
                                                i++;

                                }

                                while (arr[j]>pivot){
                                                j--;
                                }
                                if(i<=j){
                                                tmp=arr[j];
                                                arr[i]=arr[j];
                                                arr[j]=tmp;
                                                i++;
                                                j--;
                                }
                }
                if (kiri<j)
                quicksort(arr,kiri,j);
                if(i<kanan)
                quicksort(arr,i,kanan);
}

int main(int argc, char** argv) {
                int data[8]={17,10,13,15,18,12,14,16};
                cout<<"Awal : "<<data[8]<<endl;
                quicksort(data,0,7);       
                cout<<"Terurut : "<<data[8];
                return 0;
}


Kalau di run, hasilnya :



Tidak ada komentar:

Posting Komentar