Dasar c

287 views
201 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
287
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dasar c

  1. 1. Dasar C++ | FileHeader&SintaksFileHeader--->File yang berfungsi sebagai pengenal perintah i/o yang digunakan dalam penulisan perintahprogram.Jenis FileHeaderconio (ConsoleInputOutput) sintaks ---> clrscr(); & getch();stdio (Standar InputOutputObject) sintaksInput ---> scanf & getssintaksOutput ---> puts, putchar, printfiostream (InputOutputStream) sintaksInput ---> cinsintaksOutput ---> coutiomanip (InputOutput Manipulator) biasanya digunakan dalam array. sintaks --->setw(width/lebar), setiosflags (rataanparagraph/kalimat), setprecision (desimal)==================================Catatan :Perintah input dan output di C++Perintah input ---> scanf(); | gets(); | cin>>;perintah output ---> printf(); | cout(); | putchar();==================================Penentu Format untuk scanf | printfTipe Data | Penentu Format~~~~~~~~~~~~~~~~~~~*Integer | %d*Float / Decimal | %fBerpangkat | %e*Character | %c*String | %sLong Integer | %ld~~~~~~~~~~~~~~~~~~~(*) Tipe data yang sering digunakanPerintah SintaksInput | scanf | gets | cinBentuk UmumScanf ("Penentu Format",&nama_variabel);gets(nama_variabel);cin>>nama_variabel;Perintah SintaksOutput | printf | puts | putchar | coutBentuk Umumprintf("statemen");puts("statement");putchar("statement");cout<<"statement";Perintah SintaksManipulation | setioflags | setw | setprecisionBentuk Umumcout<<setiflags(ios::left/right)<<setw(panjang_spasi)<<nama_variabelcout<<setprecision(ukuran_desimal<<nama_variabel=======================================Catatan :
  2. 2. n untuk pindah baris barut untuk mengetengahkan teks (seperti fungsi tabulasi)scanf | cin : tidak bisa menerima spasigets : bisa menerima spasi<<endl untuk pindah barisContoh Sintaks C++SEARCHING1.Masukan beberapa angka (min 5), tampilkan yang mengandung angka 9.#include "iostream.h"#include "conio.h"void main(){clrscr();int i[10];int x,y,c=1;int b[10];cout<<"Banyak inputan : ";cin>>y;for(x=0;x<=y;x++){gotoxy(1,x+4);cout<<"Masukan angka : ";cin>>i[x];if(i[x] % 10 == 9 ){gotoxy(39,1+c++); cout<<"o "<<i[x];< p=""></i[x];<>}elseif(i[x] / 10 == 9 ){gotoxy(39,1+c++); cout<<"o "<<i[x];< p=""></i[x];<>}elseif(i[x] /100 == 9 ){gotoxy(39,1+c++); cout<<"o "<<i[x];< p=""></i[x];<>}}
  3. 3. getch();}2.Buat kalimat, cari huruf C (ada atau tidak).#include "iostream.h"#include "conio.h"#include "string.h"void main(){clrscr();int x,y,jd,c=0;char a[1][100];cout<<"Masukan Kalimat : ";cin>>a[1];jd=strlen(a[1]);for(y=jd;y>=0;y--){if(a[1][y]==c){c++;}else{c=c+0;}}if(c==0){cout<<"Tidak terdapat huruf c";}else{cout<<"Terdapat huruf c";}getch();}3.Masukan beberapa angka cari nilai terkecil dan terbesar, lalu kalikan.#include "iostream.h"
  4. 4. #include "conio.h"void main(){clrscr();int k,i,x;int a[7]={8,5,7,6,3,4,2};intflag=1;int n=9;for(x=0;x<=n-2;x++ &&flag==1){for(i=0;i<=n-2-1;i++){flag=1;if(a[i]>a[i+1]){k=a[i+1];a[i+1]=a[i];a[i]=k;}}}//Menampilkancout<<"Hasil kali nilai terbesar dan terkecil adalah"<<endl;< p=""></endl;<>cout<<a[0]<<" x="" "<<a[6]<<"="<<a[0]*a[6];</p><p class=" msonormal"="" style="margin-left: 36pt;">getch();</a[0]<<">}4.Tampilkan angka 1 s/d 500, lalu tampilkan lagi yang mengandung angka 5.#include "iostream.h"#include "conio.h"void main(){clrscr();//int i[10];int x,y,c=1;
  5. 5. //int b[10];//cout<<"Banyak inputan : ";cin>>y;for(x=1;x<=500;x++){cout<<x<<" ";<="" p=""></x<<">gotoxy(1,x+4);cout<<"Masukan angka : ";cin>>i[x];if(x % 10 == 5 ){gotoxy(55,20+c++);cout<<"o "<<x<<endl;< p=""></x<<endl;<>}elseif(x / 10 == 5 ){gotoxy(55,20+c++);cout<<"o "<<x<<endl;< p=""></x<<endl;<>}elseif(x /100 == 5 ){gotoxy(55,20+c++);cout<<"o "<<x<<endl;< p=""></x<<endl;<>}}getch();}5.Buat kalimat, cari hurup D lalu tampilkan jumlahnya.#include "iostream.h"#include "conio.h"#include "string.h"void main(){clrscr();int x,y,jd,c=0;char a[1][100];cout<<"Masukan Kalimat : ";cin>>a[1];jd=strlen(a[1]);for(y=jd+1;y>=0;y--){if(a[1][y]==d){c++;
  6. 6. }else{c=c+0;}}if(c==0){cout<<"Tidak terdapat huruf d";}else{cout<<"Terdapat huruf d sebanyak "<<c;< p=""></c;<>}getch();}6.Looping hingga jumlah n, lalu cari yang mengandung n.#include "iostream.h"#include "conio.h"void main(){clrscr();int x,a,y=0,b;cout<<"Anda ingin menampilkan,"<<endl;< p=""></endl;<>cout<<"Dari : ";cin>>a;cout<<"Sampai : ";cin>>b;for(x=a;x<=b;x++){cout<<x<<" ";</x<<"y++;}cout<<endl<<endl<<"ditemukan "<<b<<endl;<="" p=""></endl<<endl<<"ditemukan>cout<<"dengan "<<y<<"x inputan.";<="" p=""></y<<"x>getch();}
  7. 7. 7.Buat satu kalimat, tentukan banyak huruf yang ditulis dan jangan lupa banyak spasinya. Minimaldalam satu kalimat ada 10 kata.include "iostream.h"#include "conio.h"#include "string.h"void main(){clrscr();intjd,n,x,y;intsp;chardt[2][100]={"Aku adalah kapitendoledole","merantau kemana mana ocehehe"};sp=0;for(x=1;x<=2;x++){jd=strlen(dt[x-1]);for(y=jd;y>=0;y--){if(dt[x][y]== ){sp=sp+1;}}cout<<endl<<"jumlah hurup="" data="" ke="" "<<x<<"="" adalah="" "<<jd-sp<<endl;<=""p=""></endl<<"jumlah>cout<<"dan Jumlah spasi adalah "<<sp;< p=""></sp;<>}getch();}SORTING1.Bilangan Fibonanciinclude "iostream.h"#include "conio.h"
  8. 8. void main();{clrscr;int a[5];a[0]=2;for(x=0;x<=4;x++){a[x+1]=a[x]*+2;}for(x=0;x<=4;x++){cout<<a[x]<<" ";<="" p=""></a[x]<<">}getch();}2.Urutkan nama#include "iostream.h"#include "stdio.h"#include "conio.h"#include "string.h"void main(){clrscr();int x,y,z,n,s,c=0;char* a[5];cout<<"Insert total : ";cin>>n;for(x=0;x<n;x++){< p=""></n;x++){<>cout<<"Insertname : ";cin>>a[x];if(c <strlen(a[x])){c=strlen(a[x]);} }for(y=0;y<n-1;y++){< p=""></n-1;y++){<>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>
  9. 9. s=0;for(z=0;z<c;z++){< p=""></c;z++){<>if( a[x][z] != a[x+1][z] ){if( a[x][z] > a[x+1][z] ){s++;}else{s--;}break;}}if(s>0){a[n]=a[x];a[x]=a[x+1];a[x+1]=a[n];}}}cout<<endl<<endl<<"sesudah diurutkan="" :="" ";<="" p=""></endl<<endl<<"sesudah>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>cout<<a[x]<<" ";}<="" p=""></a[x]<<">getch();}3.Buat deret aritmatika dengan angka 1,3,9,27,…#include "iostream.h"#include "conio.h"void main();{clrscr;int a[5];a[0]=1;for(x=0;x<=4;x++){a[x+1]=a[x]*3;}for(x=0;x<=4;x++){
  10. 10. cout<<a[x]<<" ";<="" p=""></a[x]<<">}getch();}4.Buat deret aritmatika dengan pola 2,4,6,8,….#include "iostream.h"#include "conio.h"void main();{clrscr;int a[5];a[0]=2;for(x=0;x<=4;x++){a[x+1]=a[x]*+2;}for(x=0;x<=4;x++){cout<<a[x]<<" ";<="" p=""></a[x]<<">}getch();}5.Mengurutkan nama kota#include "iostream.h"#include "stdio.h"#include "conio.h"#include "string.h"void main(){clrscr();int x,y,z,n,s,c=0;char* a[5];cout<<"total inputan : ";cin>>n;
  11. 11. for(x=0;x<n;x++){< p=""></n;x++){<>cout<<"input nama kota : ";cin>>a[x];if(c <strlen(a[x])){c=strlen(a[x]);} }for(y=0;y<n-1;y++){< p=""></n-1;y++){<>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>s=0;for(z=0;z<c;z++){< p=""></c;z++){<>if( a[x][z] != a[x+1][z] ){if( a[x][z] > a[x+1][z] ){s++;}else{s--;}break;}}if(s>0){a[n]=a[x];a[x]=a[x+1];a[x+1]=a[n];}}}cout<<endl<<endl<<"sesudah diurutkan="" :="" ";<="" p=""></endl<<endl<<"sesudah>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>cout<<a[x]<<" ";}<="" p=""></a[x]<<">getch();}6.Urutkan angka dari kecil-besar#include "iostream.h"#include "conio.h"void main(){
  12. 12. clrscr();int k,i,x;int a[7]={8,5,7,6,3,4,2};intflag=1;int n=9;for(x=0;x<=n-2;x++ &&flag==1){flag=0;for(i=0;i<=n-2-1;i++){flag=1;if(a[i]>a[i+1]){k=a[i+1];a[i+1]=a[i];a[i]=k;}}}for(x=0;x<=6;x++){cout<<a[x]<<endl;< p=""></a[x]<<endl;<>}getch();}7.Urutkan angka dari awal-akhir#include "iostream.h"#include "conio.h"void main(){clrscr();int k,i,x;int a[7]={8,5,7,6,3,4,2};for(x=6;x<=0;x--){cout<<a[x]<<endl;< p=""></a[x]<<endl;<>
  13. 13. }getch();}8.Urutkan kata dari mulai jumlah terkecil#include "iostream.h"#include "stdio.h"#include "conio.h"#include "string.h"void main(){clrscr();int x,y,z,n,s,c=0;char* a[5];cout<<"Insert total : ";cin>>n;for(x=0;x<n;x++){< p=""></n;x++){<>cout<<"Insertname : ";cin>>a[x];}for(y=0;y<n-1;y++){< p=""></n-1;y++){<>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>if(strlen(a[x] <strlen(a[x+1] ){a[n]=a[x];a[x]=a[x+1];a[x+1]=a[n];}}}cout<<endl<<endl<<"sesudah diurutkan="" :="" ";<="" p=""></endl<<endl<<"sesudah>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>cout<<a[x]<<" ";<="" p=""></a[x]<<">}getch();
  14. 14. }9.Kebalikan dari no.8#include "iostream.h"#include "stdio.h"#include "conio.h"#include "string.h"void main(){clrscr();int x,y,z,n,s,c=0;char* a[5];cout<<"Insert total : ";cin>>n;for(x=0;x<n;x++){< p=""></n;x++){<>cout<<"Insertname : ";cin>>a[x];}for(y=0;y<n-1;y++){< p=""></n-1;y++){<>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>if(strlen(a[x] <strlen(a[x+1] ){a[n]=a[x];a[x]=a[x+1];a[x+1]=a[n];}}}cout<<endl<<endl<<"sesudah diurutkan="" :="" ";<=""p=""></endl<<endl<<"sesudah>for(x=0;x<n-1;x++){< p=""></n-1;x++){<>cout<<a[x]<<" ";<="" p=""></a[x]<<">}getch();}
  15. 15. 10.Sorting dengan buble#include "iostream.h"#include "conio.h"void main(){clrscr();int k,i,x;int a[7]={8,5,7,6,3,4,2};intflag=1;int n=9;for(x=0;x<=n-2;x++ &&flag==1){flag=0;for(i=0;i<=n-2-1;i++){flag=1;if(a[i]>a[i+1]){k=a[i+1];a[i+1]=a[i];a[i]=k;}}}for(x=0;x<=6;x++){cout<<a[x]<<endl;< p=""></a[x]<<endl;<>}getch();}11.Sorting dengan Squential??12.Insert acak dengan sorting bilangan genap#include "iostream.h"#include "conio.h"
  16. 16. void main(){clrscr();int a[5];int b[5];int x,i,flag=1;int n=7;for(x=0;x<=4;x++){cout<<"Masukan angka : ";cin>>a[x];if(a[x] % 2 == 0){b[x]=a[x];}for(x=0;x<=n-2;x++ &&flag==1){flag=0;for(i=0;i<=n-2-1;i++){flag=1;if(b[i]>b[i+1]){k=b[i+1];b[i+1]=b[i];b[i]=k;}}}for(x=0;x<=6;x++){cout<<b[x]<<endl;< p=""></b[x]<<endl;<>}getch();}13.Insert acak dengan sorting bilangan ganjil#include "iostream.h"#include "conio.h"
  17. 17. void main(){clrscr();int a[5];int b[5];int x,i,flag=1;int n=7;for(x=0;x<=4;x++){cout<<"Masukan angka : ";cin>>a[x];if(a[x] % 2 != 0){b[x]=a[x];}}for(x=0;x<=n-2;x++ &&flag==1){flag=0;for(i=0;i<=n-2-1;i++){flag=1;if(b[i]>b[i+1]){k=b[i+1];b[i+1]=b[i];b[i]=k;}}}for(x=0;x<=6;x++){cout<<b[x]<<endl;< p=""></b[x]<<endl;<>}getch();}14.Insert bilangan sebanyak 10 tampilkan bilangan terbesar#include "iostream.h"
  18. 18. #include "conio.h"void main(){clrscr();int k,i,x;int a[10];intflag=1;int n=9;for(x=0;x<=9;x++){cout<<"Masukan angka : ";cin>>a[x];}for(x=0;x<=n-2;x++ &&flag==1){flag=0;for(i=0;i<=n-2-1;i++){flag=1;if(a[i]>a[i+1]){k=a[i+1];a[i+1]=a[i];a[i]=k;}}}for(x=0;x<=6;x++){cout<<"Nilai terbesar adalah "<<a[9];< p=""></a[9];<>}getch();}15.Insert bilangan sebanyak 10 tampilkan bilangan terkecil#include "iostream.h"#include "conio.h"void main(){clrscr();
  19. 19. int k,i,x;int a[10];intflag=1;int n=9;for(x=0;x<=9;x++){cout<<"Masukan angka : ";cin>>a[x];}for(x=0;x<=n-2;x++ &&flag==1){flag=0;for(i=0;i<=n-2-1;i++){flag=1;if(a[i]>a[i+1]){k=a[i+1];a[i+1]=a[i];a[i]=k;}}}for(x=0;x<=6;x++){cout<<"Nilai terkecil adalah "<<a[0];< p=""></a[0];<>}getch();}POINTER1.Pembagian uangvoid main(){int b,*a,d,c,x,y,g,h,i,j,o;cout<<"Nominal : ";cin>>*a;b=*a/675;c=b*675;
  20. 20. d=*a-c;g=h=i=j=0;x=1;while(x<d){< p=""></d){<>if(d>=500){d=d-500;g=1;}elseif(d>=100 && d<500){d=d-100;h=1;}elseif(d>=50 && d<100){d=d-50;i=1;}else{d=d-25;j=1;}x++;}cout<<"Uang Rp. 25,00 = "<<b+j<<endl;< p=""></b+j<<endl;<>cout<<"Uang Rp. 50,00 = "<<b+i<<endl;< p=""></b+i<<endl;<>cout<<"Uang Rp. 100,00 = "<<b+h<<endl;< p=""></b+h<<endl;<>cout<<"Uang Rp. 500,00 = "<<b+g<<endl;< p=""></b+g<<endl;<>}2.Bilangan ganjil, bilangan genapvoid main(){Int*a;
  21. 21. cout<<”masukan bilangan : “ ;cin>>*a;if(*a %2==0){cout<<”bilangan genap”;}else{cout<<”bilangan ganjil”;}getch();}3.Mengganti huruf pertamavoid main(){int x,y;char*a;cout<<”masukan kata : “;cin>>*a;y=strlen(*a);for(x=y;x>=0;x--){If(x=0){a++=”A”;}}cout<<*a;}4.Menghitung panjang stringvoid main(){int x,y;char*a;cout<<”masukan kata : “;cin>>*a;y=strlen(*a);cout<<”Panjang string : “<<y;< p=""></y;<>
  22. 22. }5.Menjumlahkan Elemen Arrayvoid main(){int*a;int n,x;for(x=0;x<=4;x++){fout<<”masukan nilai : “;cin>>*a;n=n+*a;}cout<<”Total : “<<n;< p=""></n;<>}6.Faktorial7.Konversi decimal ke biner8.Konversi decimal ke hexa9.tofunctionvoid tampil(int*f);void main(){int*p;a[5]={1,2,3,4,5};a=&a[0];tampil(p);}void tampil(int*f){int x;for(x=0;x<=4;x++){cout<<*f++;}}
  23. 23. 10.Luas bangun ruangvoid main(){cout<<”luas alas : “;cin>>*la;cout<<”tinggi : “cin>>*t;*l=*la * *t;cout<<”Luas : “<<*l;}11.Konversi suhu12.Pencarian kamus array13.Menghitung nilai ujian mahasiswavoid main(){int x;int*p,b=0,n;float rata;cout<<"Berapa nilai yang diinputkan : ";cin>>n;for(x=0;x<=n-1;x++){cout<<"Masukan nilai "<<x+1<<" ";cin="">>*p;</x+1<<">b=b+*p;}rata=b/n;cout<<"Jumlah nilai "<<b<<endl;< p=""></b<<endl;<>cout<<"Rata-rata "<<rata;< p=""></rata;<>}14.Typevoidvoid main(){void*p;intfor(x=0;x<=5;x++){
  24. 24. cout<<"Masukan nilai : ";cin>>*p;}for(x=0;x<=5;x++){cout<<"Nilai : "<<*p++;cout<<"Alamat : "<<&p++;}}15.Arrayvoid tampil(int*f);void main(){int*p;a[5]={1,2,3,4,5};a=&a[0];tampil(p);}void tampil(int*f){int x;for(x=0;x<=4;x++){cout<<*f++;}}16.Persamaan kuadrat17.Penjualan barang18.Bentuk segitiga19.Matrixs#include#includevoid main(){
  25. 25. int*a,*b,*c,x,y;for(x=0;x<=1;x++){for (y=0;y<=1;y++) {cout<<"masukan Matrik a :";cin>>*a;} }for(x=0;x<=1;x++){for (y=0;y<=1;y++) {cout<<"masukan Matrik b :";cin>>*b;} }for(x=0;x<=1;x++){for (y=0;y<=1;y++){*c=*a+*b;cout<<*c<<" ";}}}20.toStructurvoid main(){Int*i;int x,y,c=1;int b[10];cout<<"Banyak inputan : ";cin>>y;for(x=0;x<=y;x++){gotoxy(1,x+4);cout<<"Masukan angka : ";cin>>*i;if(*i++ % 10 == 9 ){gotoxy(39,1+c++); cout<<"o "<<i[x];< p=""></i[x];<>}elseif(*i++ / 10 == 9 ){gotoxy(39,1+c++); cout<<"o "<<i[x];< p=""></i[x];<>
  26. 26. }elseif(*i** /100 == 9 ){gotoxy(39,1+c++); cout<<"o "<<i[x];< p=""></i[x];<>}}}

×