Ahad, 8 April 2012

Answer Pointer Part I


1.   How do you access the memory address of a variable?

   

    Answer:

Apply the address operator & to the variable.

e.g: &x.
2. How do you access the contents of the memory location whose address is stored in a pointer variable?

Answer:

Apply the deference operator * to the variable *p

3. Explain the difference between the following two declarations:

i.               int n1=n;

Answer:

The declaration int n1=n; defines n1 to be a clone of n; it is a separate object that has the same value as n.

ii.             int& n2=n;

Answer:

The declaration int& n2=n; defines n2 to be a synonym of n; it is the same object as n, with the same address.

4. Explain the difference between the following two uses of the reference operator &:

i.               int& r = n;

Answer:

Declares r to be a reference for the int variable n.

ii.             p = &n;

Answer:

Assigns the address of n to the pointer p.

5. Explain the difference between the following two uses of the indirection operator *:
i.            int* q = p;

Answer:

Declares q to be a pointer (memory adress) pointing to the same int to which n points.

ii.            n = *p;

Answer:

Assigns to n the int to which p points.
6. True or false? Explain:
i.     
if (x == y) then (&x == &y)

  Answer:

True: because &x and &y are synonyms for x and y, respectively;         so      if (x = = y) then they all have the same value.  

iii.             if (x == y) then (x* == *y)

Answer:

False: different objects can have the same value, but different objects have different addresses.

7. What is wrong with the following code:
                 int& r = 22;

Answer:

You cannot have a reference to a constant; it’s address is not accessible.

8. What is wrong with the following code:
                 int* p = &44;

Answer:

The reference operator & cannot be applied to a constant.

9. What is wrong with the following code:
    char c = ‘w’ ;
  char* p = c;

Answer:

The variable p has type char, while the expression &c has type pointer to char. To initialize p to &c, p would have to be declared as type char*.



                 


Ahad, 1 April 2012

Aktiviti Perbincangan Topik Fungsi FP201


Arahan :  Sila pilih ahli kumpulan [seorang pelajar DIP 2B + seorang pelajar DIP2C]. Jika terdapat pelajar DIP2B yang tiada kumpulan, sila sertai mana-mana kumpulan. Maksimum TIGA orang [2 orang pelajar DIP2B + seorang pelajar DIP 2C]. Pelajar DIP2B secara automatik dilantik menjadi mentor bagi pelajar DIP2C

Soalan 1: Bina fungsi berdasarkan fungsi prototaip dibawah.

a.       Program 1

Function Prototype
Arahan dalam Function Definition
void Display();
Paparkan rentetan “Hari ini saya belajar topic fungsi dengan mentor saya yang bernama -------”
double Addition(double, double, double);
Menjalankan operasi tiga nilai perpuluhan



b.      Program 2

Function Prototype
Arahan dalam Function Definition
int MaxNum(int, int, int, int);
Memulangkan nilai yang terbesar antara empat nilai argument. Nilai dimasukkan oleh user melalui fungsi main().



c.       Program 3

Function Prototype
Arahan dalam Function Definition
Char Grade(int);
Tentukan gred berdasarkan nilai markah yang dimasukkan oleh user melalui fungsi main(). Fungsi ini akan memulangkan nilai gred kepada main().



Soalan 2: Bincangkan output bagi sebuah fungsi yang menggunakan parameter ‘call by reference’. Anda boleh pilih mana-mana program dari buku rujukan.



Jawapan ini perlu dihantar selepas perbincangan 2 April 2012. Tulis nama semua ahli yang terlibat dalam kertas jawapan. Markah akan diberikan (dikira dalam pemberian markah amali 5). Pelajar disarankan supaya menggunakan laptop.