Wednesday, November 28, 2018

Structure and Union and Memory Allocation


Structure Defination:
            a data type to store group of data with various of data type
Syntax:
struct name_structure {
    dataType1 name_field1;
    dataType2 name_field2;
   
};

Union Defination:
Union is used for memory join. By using union, a memory location can be assigned for two or more variable with different data types
Syntax:
union  name_union{
     typedata1  name_var1 ;
     typedata2   name_var2;
     ……
} name_var_union;

Union Containing:
 name_union   name_var_union; 


Memory Allocation Defination:
acquiring some memory space (RAM) managed by the OS to be used by a program.

Memory de-allocation Defination
            releasing memory space (RAM) back to the OS.


 Albert TG/2201736385/LB04
Binusian 2022





Function and Recursion


 Function and Recursion

There is 2 Diffrent Function:
-Library  Function
            Such as strcpy() in string.h, sqrt() in math.h, printf() in stdio.h

-User Defined Function

In order to create your own function you need to use this construction of function

           return-value-type  function-name( parameter-list )
           {
   statements;
           }

return-value-type:  data type of the value returned
      If not filled, then default data type will be used (default integer)
      If return-value-type is void then the function will not return value

Parameter-list: list of value sent from the function initiator (user)


Recursion Definition:
a function call inside a certain function calling itself

Recursive Function has two components:
     Base case:
            return value(constant) without calling next recursive call.
     Reduction step:
            sequence of input value converging to the base case.


Example:

Fibonacci Number
sequence: 0, 1, 1, 2, 3, 5, 8, 13 ...
Relation between the number define recursively as follows:
Fib(N) = N                                          if N = 0 or 1
Fib(N) = Fib(N-2) + Fib(N-1)            if N >= 2


int Fib(int n) {
   int f;
   if(n==0) f = 0;
      else if(n==1) f = 1;
         else f = Fib(n-2) + Fib(n-1);
   return f;
}
 
 
 Hope you understand with this thing if you have any question gladly ask at the comment section below

Albert TG/2201736385
LB04
Binusian 2022