Monday, December 17, 2018

C++ Algorithm & Programming Semester 1

In this blog, I am explaining about C++ programming language from the beginning. The topics in this blog are:
- Introduction
- Input & Output
- Operator, Operand, Arithmetic
- Selection
- Repetition
- Pointer
- Array
- Function and Recursion
- File Processing
- Sorting
- Searching

Introduction



Algorithm is a procedure for solving a problem in terms of the actions to be executed, and the order in which these actions are to be executed. Algorithm derived from the word algoris and ritmis. It introduced by Al-Khowarizmi.

I. Pseudo-code
Pseudo-code is an artificial and informal language that helps you develop algorithms. It's similar to everyday English. Pseudo-code exists so programmers who knows different programming language can understand the logic of an algorithm.

II. Data Type
In C++, there is some data types:
- character (char)
- integer (int, unsigned int, short int, unsigned short int, long int, unsigned long int, etc)
- float (float, double, long double)

III. Type Cast
A type of data can be changed by using type cast. For example:
int x;
float y = (float)x * 2;

Input & Output


I. Output
Output is the activity when the system show data on the display screen / monitor. Some of standard library function (stdio.h) in C are:
- printf();
- putchar();
- putch();
- puts();
- etc.

II. Input
Input operation is an activity of getting the data into the memory using standard input output devices (keyboard, disk, etc). Some of standard library function (stdio.h) that is related to input operations are:
- scanf();
- getchar();
- getch();
- getche();
- gets();
- etc.

Operator, Operand, Arithmetic


I. Assignment Operators
Used in assigning value to an operand.
Syntax:
Operand1 = Operand2;
Left hand side operand (Operand1) should have (L-Value) such as variable.
Right hand side operand (Operand2) can be constant, another variable, expression or function.
Example:
x = 2; //constant
x = y; //other variable
x = 2 * y; //expression
x = sin(y); //function

II. Arithmetic Operators
- Addition (+)
- Subtraction (-)
- Multiply (*)
- Division (/)
- Modulo (%)
- Increment (++)
- Decrement (--)
- Scope / Priority (())

III. Relational Operators
- Equality (==)
- Not equal (!=)
- Less than (<)
- Greater than (>)
- Less or equal than (<=)
- Greater or equal than (>=)
- Conditional assignment (?:)

IV. Logical Operator
- And (&&)
- Or (||)
- Not (!)

V. Bitwise Operators


VI. Pointer Operators
Pointer operators consist of:
- address of (&)
- value of (*)

Selection


I. If
if (boolean expression) statement;
or
if (boolean expression) {
      statement1;
      statement2;
      ......
}

II. if-else
if (boolean expression) {
      statement1;
      statement2;
      ......
}
else{
      statement3;
      statement4;
      ......
}

III. switch-case
switch (expression) {
      case constant1 : statements1; break;
      .
      .
      case constant2 : statements2; break;
      default : statements;
}


Repetition

I. For loop
for(initialization; conditional; increment/decrement) statement;
OR
for(initialization; conditional; increment/decrement){
      statement1;
      statement2;
      ............
}
example:





II. While loop
while(conditional) statement;
OR
while(conditional){
      statement1;
      statement2;
}
example:








III. Do-While loop
do{
      statement1;
      statement2;
}while(conditional);
example:








Pointer

Pointer is a variable that store the address of another variable.
Syntax:
      <type> *ptr_name;
I. Pointer