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).