Monday, December 10, 2018

Structure


Structure is a data type that could store various data type.

The difference between structure and array is array can only store the same data type as the array data type while structure could store various data type.

Syntax

struct struct_name
{
    data_type1 var_name1;
    data_type2 var_name2;
    ...
};

Two ways to declare the struct variable :
1.

struct struct_name
{
    data_type1 var_name1;
    data_type2 var_name2;
    ...
} struct_variable;

2.

struct struct_name
{
    data_type1 var_name1;
    data_type2 var_name2;
    ...
};
struct struct_name struct_variable;



Function and Recursion

Important things from the Function and Recursion material :

Function and Recursion are related to modular programming.
Modules, also called sub-program, is a block of statement that is used to do a certain task.

There are two types of function in C:
1. Library function
    - provided by the compiler. Example : strlen(), strcpy()
2. User defined function
    - does not provided by the compiler. It is made by the programmer during the coding.

Function

return_type function_name(parameter)
{
     statement;
}

Recursive is a function that returns or call the function itself.
Recursive function must have a base case and a reduction step so infinite loop is not made.

Example of recursive function :
==Count power of  a number==

int power (int base, int exponent)
{
     if(exponent==0 || exponent ==1) return b;
     return a*power(base, exponent-1);
}


Thursday, October 11, 2018

Program Control Repetition


So here is the important things from Algorithm and Programming class.

REPETITION

Repetition is one or more instruction that is/are repeated for a certain amount of time.
There are 4 types of repetition operation. There are :
1. for
2. while
3. do-while
4. goto (This operation is not used much during programming)

The syntax for For is :

for(exp1; exp2; exp3)
{
     statement;
}
exp1 : initialization
exp2 : condition
exp3 : increment (++) or decrement (--)

The syntax for While is :
while ( exp )
{
     statement; 
}

The syntax for Do-While is :
do
{
     statement;
}
while ( exp )

The difference between while and do-while is :
while operation checks the condition first then run the statement while
the do-while operation run the statement first then check the condition later.