Jumat, 08 April 2016

tugas alpro diktat bab 1

hasil dari pemgerjaan latihan di (DIKTAT ALGORITMA DAN PEMROGRAMAN Edisi II) seperti sebagai berikut:
untuk mempermudah pengupload maka dikumpulkan di file hosting google drive.
HASIL LINK: 
LINK BAB 1

5.3 perpangkatan

  • Dengan menggunakan fungsi ln dan exp, buatlah fungsi untuk menghasilkan nilai x^y
  • analisis
    Dengan menggunakan sifat logaritma :
    ln(xy) = y*ln(x)
    exp(ln(xy)) = exp(y*ln(x))
    xy = exp(y*ln(x))
  • cpp
    #include <iostream>
    #include <math.h>
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    float pangkat(int x,int y){
        return(exp(y*log(x)));
    }

    int main(int argc, char** argv) {
        float hasil;
        int a,b;
        cout<<"menghitung hasil perpangkatan\n";
        cout<<"Tulis sebuah bilangan :";cin>>a;
        cout<<"Mau dipangkat berapa :";cin>>b;
        cout<<a<<"^"<<b<<"="<<pangkat(a,b);
       
        return 0;
    }

5.4 perkalian 2 bilaangan

  • fungsi perkalian 2 bilangan bulat dengan menggunakan operator penjumlahan.
  • cpp
    #include <iostream>
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    int kal(int k,int b){
        if(b>0)
        return k+kal(k,b-1);
        else return 0;
    }
    int main(int argc, char** argv) {
        int k,b;
        cin>>k;cout<<"dikalikan ";cin>>b;
        cout<<kal(k,b);
        return 0;
    }

5.5 factorial

  • menghitung factorial rekursif
  • cpp
    #include <iostream>
    using namespace std;

    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    int fac(int n){
        if(n>1) return fac(n-1)*n;
        else return 1;
    }
    int main(int argc, char** argv) {
        int a;cout<<"factorial : ";cin>>a;
        cout<<fac(a);
        return 0;
    }

5.6 fibonanci

  • menghitung fibonanci sampai  suku ke n
  • cpp
    #include <iostream>
    #include <conio.h>
    using namespace std;
    class Operator {
        friend ostream& operator<<(ostream&, Operator&);
        friend istream& operator>>(istream&, Operator&);
        public:
            long fibonacci(int);
        private:
            int n;
        };
    int fibonacci (int n){
        if ((n == 1) || (n == 2)) return(1);
        else return(fibonacci(n-1) + fibonacci(n-2));
    }
    ostream& operator<<(ostream& out, Operator& a) {
        for (int i = 1; i <= a.n; i++)
            out << fibonacci(i) << " ";
        return out;
        }
    istream& operator>>(istream& in, Operator& a) {
        cout << "Sampai suku ke : ";
        in >> a.n;
        return in;
    }
    main() {
        Operator run;
        cin >> run;
        cout << run;
        getch();
    }

5.8 mencari gcd

  •  menghitung gcd dari dua bilangan bulat positif
  • cpp
    #include <iostream>
    using namespace std;
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    int gcd(int c,int d){
        if(d==0) return(c);
        if(c<d) return(gcd(d,c));
        return(gcd(c-d,d));
    }
    int main(int argc, char** argv) {
        int a,b;
        cin>>a;cin>>b;
        cout<<gcd(a,b);
        return 0;
    }

ppt 5 penambahan berpangkat rekursif


  •     Hitung nilai dari
    52 + 62 + 72 + 82 + 92 + 102
  •  cpp
    #include <iostream>
    using namespace std;
    int re(int n){
        if(n>=5){
            return re(n-1)+n*n;
        }
        else return 0;
    }

    int main(int argc, char** argv) {
        cout<<re(10);
        return 0;
    }