Cari Blog Ini

Kamis, 21 April 2011

REKURSIF

This is a recursive definition, and we can make a recursive subroutine to implement it.

Previously, there were two pertimbahangan should we put in our calculation, which is a fact about recursive subroutine. First, the binary search algorithm begins by checking the "central element of a list". What happens if the list is empty? If no element in the list, then we are not likely to see elements in the middle. Or in other words, this is called "initial conditions" to check the element in the list, which has a list that is not empty.

What happened was we had to find value in the list empty? The answer is simple: We can say that the value we are looking for is not in the list. Empty list is the basic case for a binary search algorithm. Case basis for a recursive algorithm is a case that will be dealt with directly, not done recursively. Binary search algorithm has another base case, ie if we find the value we find in the middle of a list, then the program is completed. We do not need to do further recursion.

The second consideration is the subroutine parameters. In non-recursive subroutine binary search discussed earlier, the parameter is an array. However, in a recursive approach, we should be able to apply the subroutine recursively only part of the original list. In the original subroutine is non-recursive, we search the entire array, while the recursive subroutine should be looking at some of the array. Subroutine parameters should be able to tell in which part of the array will be searched.

Here is a recursive binary search algorithm to find a value in a section of an array of integers:

static int cariBiner (int [] A, int idksRendah, idksTinggi int, int value) {
/ / Look for the "value" in the array "A" from position "idksRendah" to "idksTinggi"
/ / The assumption is that an array sorted in ascending order
/ / If found return index value in array
/ / Where the value is located. If it does not return -1

if (idksRendah> idksTinggi) {
/ / This means that the list is empty because it should be lower idksRendah
/ / From idksTinggi. "Value" can not possibly exist in the list is empty.
return -1;
}
else {
/ / Check the elements in the list. If the value is equal to the content element
/ / Middle, then the return position.
/ / If not, search recursively at the beginning or end
/ / From the half-list
int idksTengah = (idksRendah + idksTinggi) / 2;
if (value == A [idksTengah])
idksTengah return;
else if (value A [idksTengah]
return cariBiner (A, idksTengah + 1, idksTinggi, value);
}
} / / End cariBiner ()

In the routine above, parameters and idksTinggi idksRendah declare the array to be searched. To search the entire array, we just need to call cariBiner (A, 0, A.length - 1, value). In either case basis - ie there is no element in the index ranges are given and when no value is found in the middle of that range - subroutine to give the answer directly, without recursion. In other cases, the subroutine will call itself recursively to calculate and return the results.

Parallel array

Parallel array is to use multiple arrays with the same index. For example we want to create multiple columns in parallel - an array of x in the first column, array y in the second column, an array of colors in the third column, and so on. Data for the i-th row can be obtained from each of these arrays. There's nothing wrong with this way, but this way as opposed to object-oriented philosophy that collect data related in one object. If we follow the rules like this, we do not have to imagine amaka data connection and the other one because all data will be grouped in one place.

Suppose I write a class like

{DataString class
/ / Data from one of the messages
int x, y; / / Position Message
Color color; / / Color message
}

To save data in several messages, we can use an array of type DataString [], which is then declared as instance variable with the name of the data so

DataString [] data;

The contents of the data value null until we create a new array, for example with

data = new DataString [JUMLAH_PESAN];

Once the array is made, the value of each element of the array is null. We want to store data in objects of type DataString, but no object is created. What we've created is just the container only. Element in it in the form of objects which we never made. For that element in it can we make the for loop like:

for (int i = 0; i data [i] = new DataString ();

Now we can take data every message with the data [i]. X, data [i]. Y, and data [i]. Color.

Last associated with a switch statement. Suppose we have a value from 0 to 11 months, which represents the month within one year from January to December. We want to print it on screen, with the command

switch (month) {
case 0:
bulanString = "January";
break;
case 1:
bulanString = "February";
break;
case 2:
bulanString = "March";
break;
case 3:
bulanString = "April";
break;
.
.
.
case 11:
bulanString = "December";
break;
default:
bulanString = "One month";
}

We can replace the entire switch command by using the array, for example with namaBulan array that is declared as follows:

static String [] namaBulan = {"January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"};

Then we can replace the entire switch on top with

bulanString = namaBulan [month];

Program C + + To Determine Passed / Not Passed

Program C + + To Determine Passed / Not Passed

Ex matter:
1. create a program to determine whether a student is "Passed" or "No Pass" on the basis of Value Theory and Practice Value entered by the user. Terms of students would graduate if the average value of at least 60 and minimum value of Practice 55.
Eg = 100 Value Theory, Practice Value = 50 then the result = Disqualified. For example, Value Theory = 40, Value Practice = 90 then the result is Passed.
Seeing the above question, then we know that there are two inputs namely Value Theory and Practice Value. Students will be based on two things: the average value and value theory. So first we must find the average value of first is by adding up the value of theory and practice and then divided by two. Program C + + more to answer the questions above are as follows:
#include
#include

void main(){ clrscr();
float teori, praktek, rata2;
cout<<"Masukkan nilai Teori : "; cin>>teori;
cout<<"Masukkan nilai Praktek : "; cin>>praktek;
rata2 = (teori + praktek) / 2;
if(rata2 >= 60 && praktek >= 55) {
cout<<"Lulus"; } else {
cout<<"Tidak Lulus"; }
getch();}

Program Explanation:
First we create 3 pieces of variables, namely the theory, practice and rata2, the three of us make with the float type data type can store fractional values. This is to anticipate when will the results of the division produces a float value. Furthermore, the program will ask for input from the user to the theory and practice.
The next step the program will calculate the value rata2 ie the number of theory and practice are divided in two. Then by using the IF statement, the program will perform testing whether rata2 value of at least 60 and practice at least 55. Remember here in the IF part of the test using the operators AND, so the two expressions must be TRUE.

If the result is correct, it will display "Passed" and vice versa if it is wrong it will display the "No Passing".

Showing Value Letter With IF

Showing Value Letter With IF
This time we will try to program C + + to show the value of letters of numbers entered by the user. So for example user input 85 numbers then it will display the letter A. Problem selengkapkanya are as follows:

By using the IF statement, create a program to display the letter from the value entered by the user, with sebegai following provisions:
• If the value is greater than 80, then the value of letter = A
• If the value is greater than 75, then the value of letter = B
• If a value greater than 65, then the value of letter = C
• If the value is greater than 45, then the value of letter = D
• If the value is less than or equal to 45, then the value of letter = E
• If the input value is greater than 100 or less than 0, it will display the message "wrong input".
To make the program C + + displays the value of letters as a matter of the above, we can use the IF statement. The following source code C + + program to display the value of these letters:


# include
# include

void main ()
{
court <<"Program Point Value" <> bil;
if (bil> 100 | | bil <0) court <<"Input wrong"; else if (bil> 80)
court <<"The value of letter = A"; else if (bil> 75)
court <<"The value of letter = B"; else if (bil> 65)
court <<"The value of letter = C"; else if (bil> 45)
court <<"The value of letter = D";
else
court <<"The value of letter = E";
getch ();
}

Explanation of the program displays the value of the letter:
In accordance with the provisions in question, then we must ensure that if the input is above 100 or below 0 will display the message "Input wrong", therefore first tested whether the input above 100 or below 0. Further testing of the new input values ​​to determine the value of the letters according to the provisions on the matter.

Rabu, 13 April 2011

Mencari Bilangan Terbesar atau Terkecil

salah satu soal algoritma sederhana adalah mencari bilangan terbesar atau terkecil, dimana bilangan tersebut diinputkan oleh user. Misalnya mencari bilangan terbesar atau terkecil diantara 3 buah bilangan yang diinputkan oleh user. Kita bisa menjawab pertanyaan ini dan mencari bilangan terbesar atau terkecil dengan menggunakan Operator Kondisi yang tersedia pada bahasa pemrograman C++.

Tetapi umumnya untuk mencari bilangan terbesar atau terkecil seperti ini, programmer akan menggunakan pernyataan IF karena lebih mudah dimengerti serta lebih fleksibel. Untuk lebih jelasnya silahkan simak program C++ dibawah ini untuk mencari bilangan terbesar :
view source
print?
01 #include
02 #include
03
04 void main()
05 {
06 cout<<"Program Mencari Bilangan Terbesar"<>bil1;
10 cout<<"Masukkan bilangan 2 : "; 11 cin>>bil2;
12 cout<<"Masukkan bilangan 3 : "; 13 cin>>bil3;
14 if(bil1 > bil2)
15 terbesar = bil1;
16 else
17 terbesar = bil2;
18 if(bil3 > terbesar)
19 terbesar = bil3;
20 cout<<"Bilangan Terbesar = "< 21 getch();
22 }

Penjelasan program mencari bilangan terbesar :

Pada awalnya program akan meminta user untuk memasukkan ketiga bilangan yaitu bil1, bil2 dan bil3. Kemudian program akan membandingkan antara bil1 dan bil2, jika ternyata bil1 lebih besar, maka dianggap terbesar diisi dengan nilai bil1, jika sebaliknya maka terbesardiisi dengan bil2. Selanjutnya tinggal membandingkan antara bil3 dengan terbesar, jika ternyata bil3 lebih besar lagi dibanding terbesar maka nilai terbesar diganti lagi dengan nilai bil3.

Pada akhirnya terbesar akan terisi dengan bilangan terbesar diantara ketiga bilangan yang diinputkan oleh user. Sebagai latihan, silahkan modifikasi program diatas agar menjadi program mencari bilangan terbesar diantara 4 buah bilangan yang diinputkan oleh user. Selamat mencoba, jika anda belum berhasil dan penasaran dengan caranya, silahkan hubungi saya di wirautama06[at]yahoo.com.

Contoh Pernyataan FOR Sederhana Dalam Program C++

Dalam bahasa pemrograman C++ untuk melakukan perulangan (looping) yang paling umum digunakan adalah pernyataan For. Pernyataan For berguna untuk melakukan perulangan (looping) terhadap satu atau sejumlah pernyataan.

Contoh sederhana adalah untuk menampilkan tulisan C++ sebanyak 10 kali, seperti contoh program dibawah ini :
view source
print?
#include
#include

void main()
{
clrscr();
int i;
for(i=0; i<10; i++)
{
cout<<"C++"< }
getch();
}

Penjelasan Pernyataan For Sederhana Dalam Program C++ diatas :

Mula-mula kita membuat sebuah variabel baru yaitu i dengan tipe integer. Dalam pernyataan for, i diberi nilai awal yaitu 0. Kemudian dilakukan pengujian apakah i<10, karena hasilnya true maka pernyataan di dalam For dijalankan. Kemudian dilakukan increment terhadap i dalam pernyataan i++. Selanjutnya dilakukan pengujian lagi apakah i<10. Jika iya (true) maka pernyataan di dalam For dijalankan, jika tidak maka program dilanjutkan ke bawah For. Begitu seterusnya hingga pengujian i<10 bernilai false.

Program C++ Mencari Nilai Rata-Rata Dengan For

contoh Program C++ Mencari Nilai Rata-Rata Dengan For

seperti yang kita ketahui bahwa dalam bahasa pemrograman C++ pernyataan For digunakan untuk melakukan perulangan (looping) terhadap satu atau sejumlah pernyataan. Sebagai contoh kita bisa mencari nilai rata-rata yang diinputkan oleh user. Kemudian program akan menampilkan jumlah total serta nilai rata-rata dari nilai yang diinputkan oleh user tersebut.

Misalkan kita ingin mencari nilai rata-rata dari 10 nilai yang diinputkan oleh user, maka program C++ nya adalah sebagai berikut :
view source
print?
#include
#include

void main()
{
float n, total, rata2;
total = 0;
for(int i=0; i<10; i++) { cout<<"Masukkan nilai ke "<<(i+1)<<" : "; cin>>n;
total = total + n;
}
rata2 = total / 10;
cout<<"Total = "< cout<<"Rata2 = "< getch();
}

Penjelasan Program C++ Mencari Nilai Rata-Rata Dengan For :

Mula-mula kita deklarasikan variabel yaitu n, total dan rata2 dengan tipe float. Mengapa float? agar nantinya nilai rata-rata yang ditampilkan bisa dalam bentuk pecahan. Variabel total yang akan kita gunakan untuk menyimpan nilai total kita inisialisasi (beri nilai awal) dengan 0. Selanjutnya kita lakukan perulangan (looping) dengan For sebanyak 10 kali yaitu mulai dari 0 hingga 9.

Dalam setiap perulangan kita meminta input dari user yang akan disimpan dalam variabel n. Kemudian nilai total (yang baru) ditambahkan nilai n yang sebelumnya telah diinputkan oleh user. Setelah 10 kali perulangan (looping) kita cari nilai rata-rata dengan melakukan pembagian terhadap variabel total yaitu total dibagi 10 dan hasilnya disimpan dalam variabel rata2.

Langkah terakhir adalah menampilkan nilai total yang tersimpan dalam variabel total dan menampilkan nilai rata-rata yang tersimpan dalam variabel rata2.

Program Dengan Menggunakan Block Kode

Contoh Program sederhana Dengan Menggunakan Block Kode

#include
#include
using namespace std;

int main(){
double hasil,a,b;
cout<<”Masukkan nilai: “; cin>>a;
cout<<”Masukkan pembagi: “; cin>>b;
if(a!=0){
cout< hasil=a/b;
cout< }
cout< system(“PAUSE”);
return EXIT_SUCCESS;
}

Penjelasan :

- Program di atas memerintahkan kita untuk menginputkan 2 angka

1. nilai

2. pembagi

- Jika nilai tidak sama dengan nol, maka program akan berjalan dan terus melanjutkan instruksi berikutnya.

- Dan sebaliknya, Jika nilai sama dengan nol, maka program akan berhenti.

Sebagai Simulasi :

misal nilai yang kita inputkan = 10

dan pembagi yang kita inputkan = 5

maka output program diatas adalah sebagai berikut :

Masukkan nilai: 10

Masukkan pembagi: 5

10 Tidak sama dengan nol, sehingga pembagi OK

10/5 adalah 2

Program Perulangan (loop)

Example Program Simple loop (loop)

#include
#include
using namespace std;

int main(){
int nilai;
for(nilai=1;nilai<=10;nilai++)
cout< cout<
system(“PAUSE”);
return EXIT_SUCCESS;
}

Explanation:

- Looping syntax:

for (initialization; conditions; increase)

statement;

- Initialize loop control variables set to their original values.

- The condition is an expression which is tested each time the loop repeats. as long as the conditions are right (not zero) the loop continues to run.

- Increase in the expression that determines how the loop control variable increases each time the loop repeats.

- The program will print the numbers 1 to 10

- Initial value is initialized with a value of 1.

- Each time you repeat, the value <= 10 will be tested.

- If true, the output value will count and increased by one.

- When the count reaches a value greater than 10, the condition becomes false, and the loop will stop running.

Minggu, 10 April 2011

The program binary to decimal conversion to java

The program binary to decimal conversion to java

The following is a simple program that berungsi for the conversion of binary numbers to decimal numbers with the programming language Java. valid for long ga, ga not have much further ado, straight, this source code:
public class conversion
{
public static void main (String [] args)
{
String bil = "110 111";
int [] arr_bil = new int [1000];
String [] st = new String [1000];
double result = 0, result = 0;
int a = 0;
char t;
for (int i = 0; i
{
t = bil.charAt (i);
st [i] = Character.toString (t);
}
for (int i = bil.length () -1; i> = 0; i-)
{
arr_bil [a] = Integer.parseInt (st [i]);
result = arr_bil [a] * (Math.pow (2, a));
result = result + result;
a + +;
}

{
int resultIn = (int) result;
System.out.println ("Numbers integer:" + bil);
System.out.println ("Numbers Decimal:" + resultIn);
}
}
}

and when the program running, it will display:
Integers: 110 111
Decimal Numbers: 55

Minggu, 03 April 2011

Algorithm program to print a number that is divisible by 3 and 5 between 1 to 100

Algorithm program to print a number that is divisible by 3 and 5 between 1 to 100

1. program will display the statement "a simple program displays a number between 1-100 is divisible by 3 and 5" to the user.
2. after the program returns to call the function process.
3. function check process will be repeated from 100-100 numbers are depleted in the 3 and 5. If the 1-100 number is divisible by 3 and 5 the remainder is o, then the program will display the number and save it as a number. If not the program will ignore these numbers. Repeated until the number 100 in checks. And reverse the total value to the main function as the number of numbers which can be divided into 3 and 5.
4. function reverses the process of value to the function main.
5. program displays the output to the user.

HIS PROGRAM:

#include
#include
class bil{
public:
int proses();
private:
int total;
};
int bil::proses()
{
total=0;
for(int i=1;i<100;i++){ if(i%3==0 && i%5==0) { cout<

simulation program to purchase fuel

simulation program to purchase fuel
task group:
1. Ibnu Abdullah A (10018079)
2. Andri Kurniawan (10018054)
3. Bambang Priyanto (10018039)
4. Beti Wahyu Putro (10018064)

simulation buy fuel with specifications:
Input form:
money / how many liters that will be purchased
Type of fuel to be purchased
Indicators will continue (increasing 0.1liter) for the total price / number of liters of fuel purchased not exceed demand
Output states:
The number of liters purchased (if the input in the form of money)
Total money to be paid (if the input in the form liter)

#include
#include

using namespace std;

int main(int argc, char *argv[])
{

class spbu (){
friend ostream& opreator<<(ostream&,spbu&); friend istream& operator>>(istream&,spbu&);
public :
void banyak();
void pilihan();
void hitung_liter();
void hitung_harga();
private :
int harga;
int Biaya;
int a,b ;
float hasil ;
float premium ;
float solar ;
float pertamax ;
};
void banyak::liter(){
cout<<"Masukkan berapa liter yang dibutuhkan : "; cin>>x;
}
void banyak::pilihan(){
cout<<"pilih 1 untuk premium\n"<<<"pilih 2 untuk solar\n"<<<"pilih 3 untuk pertamax\n"<<<"Masukkan pilihan : "; cin>>y;
}
void banyak::hitung_liter(){
float i = 0.0;
while(i <= x){
i = i + 0.1;
cout<<
}
}
void banyak::hitung_harga(){
float hasil = 0.0;
float premium = 4500;
float solar = 6000;
float pertamax = 8000;
if(y == 1)
hasil = x * premium;
else if(y == 2)
hasil = x * solar;
else if(y == 3)
hasil = x * pertamax;
cout<<"harga : "<<
}
void main(){
spbu :
banyak.banyak();
banyak.harga();
banyak.hitung_liter();
banyak.hitung_harga();


system("PAUSE");
return EXIT_SUCCESS;
}

java program to calculate the factorial

java program to calculate the factorial

Award as the value of factorial is the result of the multiplication of positive integers less than or equal to the value N. Writing is usually a factorial: n!

berikut programnya :

import jeliot.io.*;


public class faktorial
{
public static void main (String[ ] args)
{
long limit = 20; // menghitung faktorial integer daeri 1 – 20
long faktorial = 1; // pendefinisian variabel faktorial

for (int i = 0; i <= limit; i++)
{
faktorial = 1;

for (int faktor = 2; faktor <= i; faktor ++)
faktorial *= faktor;
System.out.println (i + "!" + " adalah " + faktorial);
}
}
}

Jumat, 01 April 2011

Array Dimensi Satu

Array Dimensi Satu
Bentuknya :
Tipe nama_var[ukuran];
Dengan :
Tipe : menyatakan jenis elemen array (int, char, unsigned, dan lain-lain)
Ukuran : menyatakan jumlah maksimal elemen array
Contoh :
Float nilai_ujian[5];

Pada turbo C++ array disimpan dalam memori secara berurutan. Elemen pertama berindeks nol digambarkan sebagai berikut :
Nilai_ujian[0]
Nilai_ujian[1]
Nilai_ujian[2]
Nilai_ujian[3]
Nilai_ujian[4]

Masing-masing berbentuk float dan berjumlah 5 elemen.
Selain itu, deklarasi array juga dapat berupa :
Static int bulan[12]={1,2,3,4,5,6,7,8,9,10,11,12}
Sesuai dengan deklarasi array diatas, maka isi variable array telah ditentukan yaitu :
Bulan[0] bernilai 1
Bulan[1] bernilai 2
Bulan[2] bernilai 3
Bulan[3] bernilai 4
Bulan[4] bernilai 5
Bulan[5] bernilai 6
Bulan[6] bernilai 7
Bulan[7] bernilai 8
Bulan[8] bernilai 9
Bulan[9] bernilai 10
Bulan[10] bernilai 11
Bulan[11] bernilai 12

Untuk memperjelas tentang array dimensi satu, perhatikan maslah berikut ini :

Misalkan Anda diminta membuat algoritma dan program untuk menampilkan bilangan dari 1 sampai bilangan 10, dengan pangkatnya masing-masing. Adapun batas nilai maksimal yang disimpan adalah 100.
Sesuai yang telah Anda pelajari , bahwa bilangan 1 pangkatnya adalah 1. Hasil ini diperoleh dari 1*1, kemudian bilangan 2 pangkatnya adalah 4, hasil ini diperoleh dari 2*2 sampai bilangan 10 yang pangkatnya adalah 100, hasil ini diperoleh dari 10*10.
Algoritma dari permasalahan diatas adalah berikut ini :
1. Tentukan elemen array untuk menampung nilai perkalian
2. Tentukan nilai awal indeks, batas akhir indeks dan kenaikannya (dalam hal ini , nilai awal indeks adalah 0, batas akhir indeks adalah 10, dan kenaikannya adalah 1)
3. Lakukan perulangan sesuai langkah 2
4. Nilai awal indeks ditambah dengan 1
5. Lakukan perkalian masing-masing elemen array sampai batas akhir indeks terpenuhi.
6. Tampilkan perkalian semua elemen array
7. Selesai .
Contoh program array dimensi satu
/*Program :array2.cpp*/
#include
int main()
{
int square[100];
int i; /*loop index*/;
int k; /*the integer*/

/*calculate the squares */
for (i=0; i<10; i++)
{
k= i+1;
square[i]=k*k;
printf(“nPangkat dari %d adalah %d “, k, square[i]);
}
return 0;
}

Bila program dijalankan akan muncul hasil :
Pangkat dari 1 adalah 1
Pangkat dari 2 adalah 4
Pangkat dari 3 adalah 9
Pangkat dari 4 adalah 16
Pangkat dari 5 adalah 25
Pangkat dari 6 adalah 36
Pangkat dari 7 adalah 49
Pangkat dari 8 adalah 64
Pangkat dari 9 adalah 81
Pangkat dari 10 adalah 100

Penjelasan :
Dari program diatas, Anda dapat melihat ada 10 buah elemen yang masing-masing nilainya akan dipangkatkan, mulai dari 1 sampai 10. Dimana dalam memori sudah dipesan tempat sebanyak 100.
Sedangkan apabila array akan dikirim ke sebuah fungsi caranya adalah hanya dengan mencantumkan nama array tanpa diikuti dengan tanda apapun, seperti contoh berikut :
int c[5] = {-45, 0, 6, 72, 1543};


JUMLAH (c, 5)
—-

Dalam contoh diatas, yang memanggil fungsi JUMLAH dengan mengirimkan argument berupa variable array c dan sebuah konstanta 5. Perhatikan bahwa variable array ditulis hanya c tanpa notasi tambahan apapun. Deklarasi variable array yang menjadi parameter dari suatu fungsi dituliskan dengan nama variable array yang diikuti dengan tanda kurung [], tanpa menuliskan banyaknya lokasi memori yang diinginkan.

DEKLARASI ARRAY

Contoh Program array
/*Program :array1.cpp*/
#include
#define SIZE 12
main()
{
int a[SIZE]={1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45};
int indeks, total =0;
for(indeks=0; indeks<=SIZE-1; indeks++)
total += a[indeks];
printf(“nTotal setiap elemen array adalah %d”,total);
return 0;
}

Bila program diatas dijalankan, akan muncul hasil :
Total setiap elemen array adalah 383

ini penjelasanya teman :
Hasil penjumlahan setiap elemen array diperoleh dari jumlah data atau elemen array sebanyak 12 buah yang sudah didefinisikan pada awal program yaitu #define SIZE 12. Kemudian setiap elemen array dari a[0] yang berisi data, a[1] yang berisi data 3 di jumlahkan sampai dengan a[11] yang berisi data 45. Proses penjumlahan dilakukan pada loop dimulai dari 0 sampai data yang terakhir atau elemen terakhir.

DEKLARASI POINTER

D E K L A R A S I P O I N T ER

Seperti halnya variabel yang lain, variabel pointer juga harus dideklarasikan terlebih dahulu sebelum digunakan.
Bentuk Umum : Tipe_data *nama_pointer;
Tipe data pointer mendefinisikan tipe dari obyek yang ditunjuk oleh pointer. Secara teknis, tipe apapun dari pointer dapat menunjukkan lokasi (dimanapun) dalam memori. Bahkan operasi pointer dapat dilaksanakan relatif terhadap tipe dasar apapun yang ditunjuk. Contoh, ketika kita
mendeklarasikan pointer dengan tipe int*, kompiler akan menganggap alamat yang ditunjuk menyimpan nilai integer - walaupun sebenarnya bukan (sebuah pointer int* selalu menganggap bahwa ia menunjuk ke sebuah obyek bertipe integer, tidak peduli isi sebenarnya). Karenanya,
sebelum mendeklarasikan sebuah pointer, pastikan tipenya sesuai dengan tipe obyek yang akan ditunjuk.
Contoh :
int *px;
char *sh;

Contoh Program :

#include “stdio.h”
#include “conio.h”
int main()
{
int x, y; /* x dan y bertipe int */
int *px; /* px pointer yang menunjuk objek */
clrscr();
x = 87;
px = &x; /* px berisi alamat dari x */
y = *px; /* y berisi nilai yang ditunjuk px */
printf(“Alamat x = %p\n”, &x);
printf(“Isi px = %p\n”, px);
printf(“Isi x = %i\n”, x);
printf(“Nilai yang ditunjuk oleh px = %i\n”, *px);
printf(“Nilai y = %i\n”, y);
getch();
}

operasi aritmatika

Operasi Aritmatika

Suatu variabel pointer hanya dapat dilakukan operasi aritmatika dengan nilai integer saja. Operasi yang biasa dilakukan adalah operasi penambahan dan pengurangan. Operasi penambahan dengan suatu nilai menunjukkan lokasi data berikutnya (index selanjutnya) dalam memori. Begitu juga
operasi pengurangan.

Contoh Program :

#include "stdio.h"
#include "conio.h"
int main()
{
int nilai[3], *penunjuk;
clrscr();
nilai[0] = 125;
nilai[1] = 345;
nilai[2] = 750;
penunjuk = &nilai[0];
printf(“Nilai %i ada di alamat memori %p\n”, *penunjuk, penunjuk);
printf(“Nilai %i ada di alamat memori %p\n”, *(penunjuk+1),
penunjuk+1);
printf(“Nilai %i ada di alamat memori %p\n”, *(penunjuk+2),
penunjuk+2);
getch();
}