Hai Guys... Selamat datang di Blog Praktikum.
Disini saya menyajikan beberapa contoh contoh program c++ yang bisa buat referensi kalian. Oh iyaa sebelum kalian mencoba, akan saya beritau satu hal. Semua Source Code dibawah ini belum 100% benar, jadi mungkin ada yang salah. Dan apabila kalian menemukan kesalahan,yang awal cek "void main() {" ganti dengan "int main() {" atau "main() {" saja.
langsung saja nih, ngga usah banyak omong hehehe. cekidoott..... ;)
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int square[100]; int i;
int k;
for (i= 0; i < 10; i++)
{
k = i + 1;
square[i] = k * k;
printf ("\n Pangkat dari %d adalah %d", k, square[i]);
}
getch();
}
include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
void printArray (int [] [3]);
int main()
{
int matrik1[2][3]={{1,2,3},{4,5,6}}, matrik2[2][3]={1,2,3,4,5}, matrik3[2][3]={{1,2},{4}};
printArray (matrik1);
printArray (matrik2);
printArray (matrik3);
getch();
}
void printArray (int a[][3])
{
int i, j;
for (i =0; i<=1; i++)
{
for (j = 0; j<=2; j++)
printf("%d ", a[i][j]);;
printf ("\n");
}
}
Output :
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int v=7, *p;
p= &v;
cout<<"Nilai v = " <<v;
cout<<endl;
cout<<endl;
cout<<"Nilai * p = " <<*p;
cout<<endl;
cout<<endl;
cout<<"Alamatnya = " <<p;
getch();
}
Output :
4. Mendeklarasikan Struktur, Memasukkan dan Menampilkan Data Struktu
Source Code :
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define MAX 120
struct dtnilai
{
char nim[12];
char nama[35];
double nilai[MAX];
};
int main()
{
struct dtnilai data;
int i, jml;
char strnilai[5], strjum[5];
printf("NIM : ");
gets(data.nim);
cout<<endl;
printf("Nama : ");
gets(data.nama);
cout<<endl;
printf("Jumlah Test : ");
gets(strjum);
jml = atoi(strjum);
cout<<endl;
for(i=0;i<jml;i++)
{
printf("Nilai Test %d : ", i+1);
gets(strnilai);
data.nilai[i]=atof(strnilai);
}
cout<<endl;
printf("==================================================\n");
cout<<endl;
printf("DATA MAHASISWA YANG TELAH DIINPUTKAN : \n");
cout<<endl;
printf("NIM : %s\n",data.nim);
cout<<endl;
printf("Nama : %s\n",data.nama);
cout<<endl;
for(i=0;i<jml;i++)
{
printf("Nilai Test %d : %1f\n", i+1, data.nilai[i]);
}
getch();
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
using namespace std;
void p (void);
int *a, *b;
void main()
{
p();
}
void p (void)
{
a=(int *)malloc(sizeof(int));
b=(int *)malloc(sizeof(int));
*a=19;
*b=5;
a=b;
*b=8;
printf("Alamat a = %x\t Isi a= %d\n", a, *a);
printf("Alamat b = %x\t Isi b= %d\n", b, *b);
getch();
}
Output :
1. Contoh Program Sisip Senarai (Linked List)
Source Code :
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
typedef struct nod
{
int data;
struct nod *next;
}NOD, *NODPTR;
void CiptaSenarai(NODPTR *s)
{
*s=NULL;
}
NODPTR NodBaru(int m)
{
NODPTR n;
n= (NODPTR) malloc (sizeof(NOD));
if (n !=NULL)
{
n->data=m;
n->next=NULL;
}
return n;
}
void SisipSenarai(NODPTR *s, NODPTR t, NODPTR p)
{
if(p==NULL)
{
t->next=*s;
*s=t;
}
else
{
t->next=p->next;
p->next=t;
}
}
void CetakSenarai(NODPTR s)
{
NODPTR ps;
for (ps=s; ps!=NULL; ps=ps->next)
printf("%d --> ",ps->data);
printf("NULL\n");
}
int main()
{
NODPTR pel;
NODPTR n;
CiptaSenarai(&pel);
n=NodBaru(55);
SisipSenarai(&pel,n,NULL);
n=NodBaru(75);
CetakSenarai(pel);
getch();
}
Output :
2. Mendeklarasikan, memasukkan data, dan menampilkan data pada Single Linked List
Source Code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
using namespace std;
struct dtnilai *tampung;
struct dtnilai *ujung;
struct dtnilai *awal;
int j;
char strnilai[5], jawab[6];
struct dtnilai
{
char nim[15];
char nama[20];
double nilai;
struct dtnilai *next;
};
int main()
{
printf(" DATA MAHASISWA : \n");
printf("=============================================\n");
printf("\n");
while(1)
{
if(j==0)
{
awal=(struct dtnilai*) malloc(sizeof(struct dtnilai));
printf("NIM : ");
gets(awal->nim);
printf("\n");
printf("Nama : ");
gets(awal->nama);
printf("\n");
printf("Nilai Test : ");
gets(strnilai);
printf("\n");
awal->nilai = atof(strnilai);
tampung=awal;
tampung->next = NULL;
}
else
{
ujung=(struct dtnilai*) malloc(sizeof(struct dtnilai));
tampung->next=ujung;
printf("NIM : ");
gets(ujung->nim);
printf("\n");
printf("Nama : ");
gets(ujung->nama);
printf("\n");
printf("Nilai Test : ");
gets(strnilai);
printf("\n");
ujung->nilai=atof(strnilai);
ujung->next=NULL;
tampung=ujung;
}
printf("Masukkan Data Lagi? (y/t) : ");
gets(jawab);
printf("\n");
if((strcmp(jawab, "Y")==0||strcmp(jawab, "y")==0))
{
j++;
continue;
}
else if((strcmp(jawab, "Y")==0||strcmp(jawab, "t")==0))
break;
}
printf("Data Mahasiswa yang telah diinputkan :\n");
printf("=============================================\n");
printf("NIM\t\tNama\t\tNilai\n");
printf("\n");
ujung=awal;
while(ujung!=NULL)
{
printf("%s\t%s\t%6.21f\n", ujung->nim,ujung->nama,ujung->nilai);
ujung=ujung->next;
}
getch();
}
Output :







Tidak ada komentar:
Posting Komentar