Sunday, December 16, 2018

File Processing


• Stream definition :
          A sequence of character. All input and  output data is a stream. C sees file as a stream.
• File Definition :
There are 2 types of  File
      • Text File
          - Saved in a text format or ASCII File

Storage size depends on its data: 10000 needs 5 byte
Can be open using standard text editor application
- Or c:>TYPE file_name

• Binary File 
storing numerical data in affixed format in line with micro-processor format definition
(example: format sign-magnitude 2’s complement).

File is a collection of record
Record is a collection of field
Field is a block of byte
Byte is collection of bit



• How to open a File ?

Opening a File using fopen():
  FILE *fopen (const char *filename, const char *mode );
fopen() defined at <stdio.h>
fopen() return a pointer to the start of a buffer area. Null will be returned if file unable to open.
Types of mode you can use

     Mode        Description

r”       Opening a file to be read.
w”      Creating a file to be written.
a”       Opening a File for data append.
r+”     Opening a File for read/write.
w+”    Creating file for read/write.
a+”     Opening a File for read/append
“rb”      Opening a File (binary) to be read.
“wb”     Creating a file (binary) for write operation.


Don't forget to close your opened file by using
int fclose (FILE *stream);

Structures, Union, and Memory Allocation

Structure Definition :

• Structure is a data type to store group of data with various of data type
• Structure component called member/field/element.
• Heterogeneous (various element data type)
• Structure in other programming language also called record
• Nested Structure is a structure with one of its element is another structure.
Structure Declaration :

Syntax
struct name_structure {
    dataType1 name_field1;
    dataType2 name_field2;
    …
};

Variable can be defined at declaration time
struct name_structure {
  dataType1 name_field1;
  dataType2 name_field2;
} name_variable_structure ;

Structure variable declaration
struct name_structure  name_variable_structure; 

Union Definition :
• Union is used for memory join. By using union, a memory location can be assigned for two or more variable with different data types
• Memory capacity used by union is the largest capacity used by any element of the union

Union Declaration :
Union Data Declaration
union name_union{
         typedata1   name_var1;
         typedata2   name_var2;
         ....
}   name_var_union;

Union Variable Declaration
union   name_union       name_var_union;

Memory Allocation Definition :
• Memory allocation:
  acquiring some memory space (RAM) managed by the OS to be used by a program.
• Memory de-allocation:
  releasing memory space (RAM) back to the OS.

Sunday, December 2, 2018

Recursive, Function, and Cloud


Function Definition:
Function Structure
 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)



Recursive Definition:

Recursive is a function call inside a certain function calling itself
Recursive Function is suitable for recursive problem
Example :
 Factorial (n) or n! defined as follows :
 n! = 1, for n = 0;
 n! = n * (n-1)!, for n > 0
 4! = 4 * 3!
 3! = 3 * 2!
 2! = 2 * 1!
 1! =  1* 0!
 0! =  1
 Trace back : 4! = 1*2*3*4 = 24

Cloud Definition:
Cloud is an online-based storage used by user to store their data. There are 2 types of data
storage consist of offline-based storage and online-based storage. The examples of Cloud
storage company is iCloud, Microsoft OneDrive, Google Drive, and many more. In Cloud 
storage, there are Software as a Service (SaaS), Platform as a Service (PaaS), and
Infrastructure as a Service (IaaS).

Wednesday, October 17, 2018

Pointers and Arrays

Pointer

Pointer is a variable that store the address of another variable
Syntax :
  <type> *ptr_name;
Two operators mostly used in pointer : * (content of) and & (address of)

Pointer to Pointer

Pointer to pointer is a variable that saves another address of a pointer
Syntax:
  <type> **ptr_ptr ;

Array
Data saved in a certain structure to be accessed as a group or individually. Some variables
saved using the same name distinguish by their index.
Syntax:
  type array_value [value_dim];

Array Initialization
Array can be initialized explicitly without dimensional value declaration


Accessing Arrays
Two analogous ways of accessing an element i=2;
  *(A+2) or A[2]
A  is equivalent with &A[0] or a constant pointer to the first element of particular array
To show A[2] on the monitor screen:
  printf(“%d”,A[2]) or
  printf(“%d\n”,*(A+2));


Pointer Constant & Pointer Variable
Pointer variable is a pointer that can be assigned with new value at run-time.
Pointer constant is a pointer that can not be assigned with new value at run-time
Array is Pointer Constant to its first element of the array. Array can be filled with pointer variable.

One Dimensional Array
Syntax 1D Array:
  type name_array[row];

Two Dimensional Array
Syntax 2D Array:
type name_array[row][col];

Three Dimensional Array
Syntax 3D Array :
type name_array[row][col][depth];

Array of Pointer
An array filled with pointer/s
Syntax :
type *array_name [value_dim];

Array of Character
Array filled with character/s
Syntax:
char array_name[value_dim];

String
String is an array of character that ended with null character ( ‘\0’ or in ASCII = 0)

String Manipulation
Types of function to manipulate string in string.h library
– strlen()
Return a value of string length; excluded null char
– strcpy(statement1, statement2)
Copy statement2 to statement1
– strncpy(statement1, statement2,n)
Copy first n characters of statement2 to statement1
– strcat(statement1, statement2)
Adding string statement2 to the end of string statement1
– strncat(statement1, statement2,n)
Adding n characters of string statement2 to the end of string statement1
– strcmp(statement1, statement2)
Comparing the value of string statement1 and statement2, if similar returning 0
– etc.

Thursday, October 11, 2018

Repetition


Repetition
·One or more instruction repeated for certain amount 
of time

·Repetition/looping operation:
– For
– While
– Do-while

             1. For

Syntax:
for(exp1; exp2; exp3) statement;
or:
for(exp1; exp2; exp3){
  statement1;
  statement2;
  …….
 }
exp1 :  initialization
exp2 :  conditional
exp3 :  increment or decrement
exp1, exp2 and exp3 are optional

EXAMPLE :
Program to print out numbers from 1 to 10

     #include<stdio.h>
int main()
{
    int x;
    for( x = 1 ;  x <= 10 ;  x++ ) printf( "%d\n", x );
    return(0);
}


      2. While
Syntax :

while (exp) statements;
or:
while(exp){
  statement1;
  statement2;
   …..
}

EXAMPLE :
int counter = 1;
while ( counter <= 10 ) {
     printf( "%d\n", counter );
     ++counter;
}


3. Do-While
Syntax :

do{
    < statements >;
} while(exp);

Keep executing while exp is true
exp evaluation done after executing the statement(s)

EXAMPLE :
int counter=0;

do {
     printf( "%d  ", counter );
  ++counter;
} while (counter <= 10);