4.1
Algoritma
fungsi {menginputkan kata,memproses,membalik kata}
Deklarasi
balik[10] : char;
i : integer
Deskripsi
for(i=10; i>=0; i--){
cout<<balik[i]; }
return 0;
C++
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
char balik[10]="123456789";
cout<<"Kata Awal : ";
for(int i=1;i<10;i++){
cout<<balik[i]<<" ";
}
cout<<endl;
cout<<"Dibalik : ";
for(int i=10;i>=0;i--){
cout<<balik[i];
}
return 0;
}
using namespace std;
int main(int argc, char** argv) {
char balik[10]="123456789";
cout<<"Kata Awal : ";
for(int i=1;i<10;i++){
cout<<balik[i]<<" ";
}
cout<<endl;
cout<<"Dibalik : ";
for(int i=10;i>=0;i--){
cout<<balik[i];
}
return 0;
}
=================================================================================
4.2
Algotirma
Deklarasi
a[100] : char
i : interger
Deskripsi
for(i=0; i<=100; i++){
a[i]=i;
return 0;
C++
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int a[100];
for(int i=0; i<=100; i++){
a[i]=i;
cout<<"a["<<a[i]<<"] := a["<<a[i]+1<<"]"<<endl;
}
return 0;
}
=================================================================================
4.3
Algotirma
Deklarasi
a,b,c,d,bersih,pokok,pajak,beban, pendapatan : integer
laba_usaha, laba_kotor, laba_sebelum_pajak, laba_bersih : integer
Deskirpsi
pendapatan=a+b;
beban=c+d;
laba_kotor=bersih-pokok;
laba_usaha=laba_kotor-c;
laba_sebelum_pajak=laba_usaha+(b-d);
laba_bersih=laba_sebelum_pajak-(pajak*15/100);
Cetak hasil;
return 0;
C++
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
int a,b,c,d,bersih,pokok,pajak;
int pendapatan,beban;
int laba_usaha,laba_kotor,laba_sebelum_pajak,laba_bersih;
cout<<"|====================================================|\n";
cout<<"| |\n";
cout<<"| Program Untuk Mencari Laba Usaha |\n";
cout<<"| |\n";
cout<<"|====================================================|\n";
cout<<"| Mencari Pendapatan |\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Masukkan pendapatan usaha : ";cin>>a;
cout<<"| Masukkan pendapatan di luar usaha : ";cin>>b;
pendapatan=a+b;
cout<<"| Jumlah pendapatan usaha : "<<pendapatan<<"\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Mencari Beban |\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Masukkan beban usaha \t\t: ";cin>>c;
cout<<"| Masukkan beban di luar usaha : ";cin>>d;
beban=c+d;
cout<<"| Jumlah beban \t\t\t: "<<beban<<"\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Mencari Laba |\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Masukkan penjualan bersih : ";cin>>bersih;
cout<<"| Masukkan harga pokok \t: ";cin>>pokok;
cout<<"| Masukkan pajak \t\t: ";cin>>pajak;
cout<<"|----------------------------------------------------|\n";
cout<<"| Hasil Laba |\n";
cout<<"|----------------------------------------------------|\n";
laba_kotor=bersih-pokok;
cout<<"| Laba Kotor \t\t: "<<laba_kotor<<"\n";
laba_usaha=laba_kotor-c;
cout<<"| Laba Usaha \t\t: "<<laba_usaha<<"\n";
laba_sebelum_pajak=laba_usaha+(b-d);
cout<<"| Laba Sebelum Usaha : "<<laba_sebelum_pajak<<"\n";
laba_bersih=laba_sebelum_pajak-(pajak*15/100);
cout<<"| Laba Bersih \t\t: "<<laba_bersih<<"\n";
cout<<"|====================================================|\n";
system("pause");
return 0;
}
=================================================================================
4.4
Algotirma
Deklarasi
i, n : integer
a[20] : char
Deskripsi
for (int i=0;i<n;i++){
if(a[i]==a[i+1])
cout<<a[i];
else if(a[i-1]==a[i])
cout<<"";
else if(a[i]==a[i+2])
cout<<a[i];
else if(a[i-2]==a[i])
cout<<"";
else if(a[i]==a[i+3])
cout<<a[i];
else if(a[i-3]==a[i])
cout<<"";
else if(a[i]==a[i+4])
cout<<a[i];
else if(a[i-4]==a[i])
cout<<"";
else if(a[i]==a[i+5])
cout<<a[i];
else if(a[i-5]==a[i])
cout<<"";
else
cout<<a[i];
}
4.2
Algotirma
Deklarasi
a[100] : char
i : interger
Deskripsi
for(i=0; i<=100; i++){
a[i]=i;
return 0;
C++
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int a[100];
for(int i=0; i<=100; i++){
a[i]=i;
cout<<"a["<<a[i]<<"] := a["<<a[i]+1<<"]"<<endl;
}
return 0;
}
=================================================================================
4.3
Algotirma
Deklarasi
a,b,c,d,bersih,pokok,pajak,beban, pendapatan : integer
laba_usaha, laba_kotor, laba_sebelum_pajak, laba_bersih : integer
Deskirpsi
pendapatan=a+b;
beban=c+d;
laba_kotor=bersih-pokok;
laba_usaha=laba_kotor-c;
laba_sebelum_pajak=laba_usaha+(b-d);
laba_bersih=laba_sebelum_pajak-(pajak*15/100);
Cetak hasil;
return 0;
C++
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
int a,b,c,d,bersih,pokok,pajak;
int pendapatan,beban;
int laba_usaha,laba_kotor,laba_sebelum_pajak,laba_bersih;
cout<<"|====================================================|\n";
cout<<"| |\n";
cout<<"| Program Untuk Mencari Laba Usaha |\n";
cout<<"| |\n";
cout<<"|====================================================|\n";
cout<<"| Mencari Pendapatan |\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Masukkan pendapatan usaha : ";cin>>a;
cout<<"| Masukkan pendapatan di luar usaha : ";cin>>b;
pendapatan=a+b;
cout<<"| Jumlah pendapatan usaha : "<<pendapatan<<"\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Mencari Beban |\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Masukkan beban usaha \t\t: ";cin>>c;
cout<<"| Masukkan beban di luar usaha : ";cin>>d;
beban=c+d;
cout<<"| Jumlah beban \t\t\t: "<<beban<<"\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Mencari Laba |\n";
cout<<"|----------------------------------------------------|\n";
cout<<"| Masukkan penjualan bersih : ";cin>>bersih;
cout<<"| Masukkan harga pokok \t: ";cin>>pokok;
cout<<"| Masukkan pajak \t\t: ";cin>>pajak;
cout<<"|----------------------------------------------------|\n";
cout<<"| Hasil Laba |\n";
cout<<"|----------------------------------------------------|\n";
laba_kotor=bersih-pokok;
cout<<"| Laba Kotor \t\t: "<<laba_kotor<<"\n";
laba_usaha=laba_kotor-c;
cout<<"| Laba Usaha \t\t: "<<laba_usaha<<"\n";
laba_sebelum_pajak=laba_usaha+(b-d);
cout<<"| Laba Sebelum Usaha : "<<laba_sebelum_pajak<<"\n";
laba_bersih=laba_sebelum_pajak-(pajak*15/100);
cout<<"| Laba Bersih \t\t: "<<laba_bersih<<"\n";
cout<<"|====================================================|\n";
system("pause");
return 0;
}
=================================================================================
4.4
Algotirma
Deklarasi
i, n : integer
a[20] : char
Deskripsi
for (int i=0;i<n;i++){
if(a[i]==a[i+1])
cout<<a[i];
else if(a[i-1]==a[i])
cout<<"";
else if(a[i]==a[i+2])
cout<<a[i];
else if(a[i-2]==a[i])
cout<<"";
else if(a[i]==a[i+3])
cout<<a[i];
else if(a[i-3]==a[i])
cout<<"";
else if(a[i]==a[i+4])
cout<<a[i];
else if(a[i-4]==a[i])
cout<<"";
else if(a[i]==a[i+5])
cout<<a[i];
else if(a[i-5]==a[i])
cout<<"";
else
cout<<a[i];
}
C++
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int a[20],n;
cout<<"Batas : ";
cin>>n;
for (int i=0;i<n;i++){
cout<<"Data : ";
cin>>a[i];
}
for (int i=0;i<n;i++){
if(a[i]==a[i+1])
cout<<a[i];
else if(a[i-1]==a[i])
cout<<"";
else if(a[i]==a[i+2])
cout<<a[i];
else if(a[i-2]==a[i])
cout<<"";
else if(a[i]==a[i+3])
cout<<a[i];
else if(a[i-3]==a[i])
cout<<"";
else if(a[i]==a[i+4])
cout<<a[i];
else if(a[i-4]==a[i])
cout<<"";
else if(a[i]==a[i+5])
cout<<a[i];
else if(a[i-5]==a[i])
cout<<"";
else
cout<<a[i];
}
/* for (int i=0;i<n;i++){
cout<<a[i]<<" ";
}*/
return 0;
}
=================================================================================
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int a[20],n;
cout<<"Batas : ";
cin>>n;
for (int i=0;i<n;i++){
cout<<"Data : ";
cin>>a[i];
}
for (int i=0;i<n;i++){
if(a[i]==a[i+1])
cout<<a[i];
else if(a[i-1]==a[i])
cout<<"";
else if(a[i]==a[i+2])
cout<<a[i];
else if(a[i-2]==a[i])
cout<<"";
else if(a[i]==a[i+3])
cout<<a[i];
else if(a[i-3]==a[i])
cout<<"";
else if(a[i]==a[i+4])
cout<<a[i];
else if(a[i-4]==a[i])
cout<<"";
else if(a[i]==a[i+5])
cout<<a[i];
else if(a[i-5]==a[i])
cout<<"";
else
cout<<a[i];
}
/* for (int i=0;i<n;i++){
cout<<a[i]<<" ";
}*/
return 0;
}
=================================================================================
4.5
Algoritma
Deklarasi
tmp, i=kiri, j=kanan, cetak : integer
pivot=arr[(kiri+kanan)/2] : integer
Deskripsiwhile(i<=j){
while (arr[i]<pivot){
i++;
}
while (arr[j]>pivot){
j--;
}
if(i<=j){
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
i++;
j--;
}
}
#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[i];
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 data[8]={17,10,13,15,18,12,14,16};
cout<<"awal : "<<data[8];
cout<<endl;
quicksort(data,0,7);
cout<<"terurut : "<<data[8];
return 0;
}
=================================================================================
4.6
Algoritma :
1.n < - bilangan
2.untuk i=0
Masukkan data [i];
ulang selama i<n;
untuk a= 0
untuk b = n-1
jika data [b]< data [b-1] maka tukar data
.....
Akhir jika
b--1;
akhir untuk
tampilan data [i];
a++;
akhir untuk
3. keluar
Program :
#include <stdio.h>
#include<iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
main() {
int x;
int y;
cout<<"\nMasukkan bilangan ke- 1 :";
cin>>x;
cout<<"Masukkan bilangan ke-2 :";
cin>>y;
if(x>y){
cout<<"lebih besar dari pada\n\n ";cin>>x,y;
}else if(x=y){
cout<<"sama besar dengan\n\n";cin>>x,y;
}else{
cout<<"lebih kecil dari pada\n\n";cin>>x,y;
}
return 0;
}
1.n < - bilangan
2.untuk i=0
Masukkan data [i];
ulang selama i<n;
untuk a= 0
untuk b = n-1
jika data [b]< data [b-1] maka tukar data
.....
Akhir jika
b--1;
akhir untuk
tampilan data [i];
a++;
akhir untuk
3. keluar
Program :
#include <stdio.h>
#include<iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
main() {
int x;
int y;
cout<<"\nMasukkan bilangan ke- 1 :";
cin>>x;
cout<<"Masukkan bilangan ke-2 :";
cin>>y;
if(x>y){
cout<<"lebih besar dari pada\n\n ";cin>>x,y;
}else if(x=y){
cout<<"sama besar dengan\n\n";cin>>x,y;
}else{
cout<<"lebih kecil dari pada\n\n";cin>>x,y;
}
return 0;
}
0 komentar:
Posting Komentar