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.

Isnin, 26 Mac 2012

POINTER - PART II


1.       What is wrong with the following code:

        Short a [32];

        for( int i = 0; i < 32; i++)

       *a++ = i*i;

2.       Determine the value of each of the indicated variables after the following code executes. Assume that each integer occupies 4 bytes and that m is stored in memory starting at byte 0x3ffd00.

                   int m = 44;

                  int* p = &m;

                  int&  r = m;

                  int n = (*p) ++;

                  int* q = p – 1;

                  r = * (--p) + 1;

                   ++*q;

a.                   m


b.                   n


c.                     &m


d.                    *p


e.                   r


f.                    *q


Jumaat, 23 Mac 2012

POINTER PART I

Dear students;
Answer all the questions, and submit to my email aminahpolimas@yahoo.com, by 25 March 2012. Kindly discuss in group/partner. Good luck!!

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

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

3.       Explain the difference between the following two declarations:

i.           int  n1=n;

ii.                         int& n2=n;

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

i.           int& r = n;

ii.                P = &n;

5.       Explain the difference between the following two uses of theindirection operator  *:

     i.      int* q = p;

     ii.     n  =  *p;

6.        True or false? Explain:
     i.          if (x == y) then (&x == &y)

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

7.        What is wrong with the following code:

                     int& r = 22;

8.        What is wrong with the following code:

                     int* p = &44;

9.        What is wrong with the following code:

                     char c = ‘w’ ;

                     char* p = c;

Isnin, 12 Mac 2012

call by reference VS call by value

Dear students, observe output of the program below.
If you still not understand it, feel free to contact me.

//Program menunjukkan fungsi call by reference vs call by value

#include<iostream.h>
void ref(int&, int&);//call by reference
void val(int,int);// call by value
void main()
{
 int x=5,y=7;
 int q=6, r= 12;

 cout<<"Value of x and y in main function"<<endl;
 cout<<"Value of x="<<x<<endl<<"Value of y="<<y<<endl<<endl;


 ref(x,y);//memanggil fungsi ref
 cout<<"Value of x and y in main function after ref function called"<<endl;

 //Lihat perubahan pada nilai x dan y
 cout<<"Value of x="<<x<<endl<<"Value of y="<<y<<endl<<endl;

 cout<<"Value of q and r in main function"<<endl;

 cout<<"Value of q="<<q<<endl<<"Value of r="<<r<<endl<<endl;

 val(q,r);//memanggil fungsi val

 cout<<"Value of q and r in main function after val function called"<<endl;

 //Lihat adakah nilai pada q dan r berubah?
 cout<<"Value of q="<<q<<endl<<"Value of r="<<r<<endl<<endl;


}
//fungsi yang menggunakan konsep call by reference
void ref(int &a, int &c)
{
 a=8;
 c=0;


 cout<<"value of x and y in the val function"<<endl;
 cout<<"Value of x="<<a<<endl<<"Value of y="<<c<<endl<<endl;
}
//fungsi menunjukkan konsep call by value
void val(int d, int e)
{
 d=d*10;
 e=e*2;

 cout<<"value of q and r in the val function"<<endl;
 cout<<"Value of q="<<d<<endl<<"Value of r="<<e<<endl<<endl;

}


Jumaat, 2 Mac 2012

Function

Dear students;
Kindly answer all the question below with brief explaination/justification.
Submit to my email aminahpolimas@yahoo.com before 12.00 pm 3 March 2012.
Your submission will be considered as your attendance. Thank you.
Good Luck!
P/S: Please submit softcopy to my email. Dont submit hardcopy.

Section A:

1. When a function is called, what happens to program execution? [3 marks]
 When a function is called, program execution jumps to the function. When    the function returns, program execution returns to the caller at the statement immediately following the function call.

2.What is the difference between an argument and a parameter? [3 marks]
 An argument is a value passed to a function. A parameter is a variable that receives the value.

3. If a function requires a parameter, where is it declared? [3 marks]
A parameter is declared after the function’s name, inside the parentheses.

Section B:
1.Show the two forms of the return statement. [3 marks]
  Here are two forms of return:
    return;
    return value;
2.Can a void function return a value? [3 marks]
No, void functions cannot return values.

3.Can a function call be part of an expression? [3 marks]
A call to a non-void function can be used in an expression. When this happens. The function is executed so that its return value can be obtained.

Section C:
1. What are the main differences between local and global variables? [3 marks]
A local variable is known only within the block in which it is declared. It is created upon entry into its block and destroyed when the block is left. A global variable is declared outside all functions. It can be used by all functions and exists during the entire lifetime of the program.

2. Can a local variable be declared anywhere within a block? [3 marks]
Yes, a local variable can be declared anywhere within a block as long as it is declared before it is used.

3. Does a local variable hold its value between calls to the function in which it is declared?                                                                                                                  [3 marks]
No, local variables are destroyed when the function in which they are declared returns.

Section D:
1. What is a function prototype? What is the purpose of a prototype? [3 marks]
A prototype declares a function’s name, return type, and parameters. A prototype tells the compiler how to generate code when the function is called and ensures that it is called correctly.

2. Aside from main(), must all function be prototyped? [3 marks]
Yes, all functions except for main() must be prototyped.
3.  When you use a standard library function, why must you include its header? [3 marks]
In addition to other things, a header includes the prototype for the library function.

4.       What is the difference between function’s declaration and its definition?

Section E:

1.       #include <iostream.h>

2.       #include <string.h>

3.       void main()

4.       {

5.                   char password [10];

6.             char n[20], name[20];

7.            

8.             cout<<"Please insert  your name:";

9.             cin>>name;

10.                 strcpy(n,name);

11.           cout<<n <<endl;

12.                 cout<<"Please enter your password";

13.                 cin>>password;

14.      

15.                       if (strcmp (password, "12345") == 0)

16.                              cout<<"Access Granted !";   

17.                       else

18.                             cout<<"Access Denied !";     

19.     }

1.    Compile the above code.

2. Build a program that can calculate the volume and area of  the box. This program has 2 functions, which are function for assigning values of length, width and depth, and function to calculate and display the volume and area of the box.(Attach your output console and coding).