Unit 2_ Basics of Programming
Unit 2_ Basics of Programming
Introduction to Algorithms
1. Idea of an Algorithm
Understand the Problem: Clearly define the problem and understand the
requirements.
Break Down the Problem: Divide the problem into smaller, more manageable parts.
Develop an Algorithm: Write down the steps needed to solve the problem
logically.
Test the Algorithm: Ensure the algorithm works by applying it to different
scenarios.
Optimize the Algorithm: Improve efficiency by minimizing time and space
complexity.
3. Characterstics of an Algorithm
2. Flowchart
Basic Symbols:
Example: Flowchart for the above algorithm to find the sum of two numbers:
Start → Input a, b → Process: sum = a + b → Output sum → End
3. Pseudocode
Start
Input a, b
sum = a + b
Print sum
End
#include <iostream>
using namespace std;
int main() {
// Code to be executed
return 0;
}
g++ program.cpp
./a.out
There are four steps in the C++ compilation process that converts source code into
machine-readable code:
1. Preprocessing
2. Compilation
3. Assembling
4. Linking
The following steps are taken to execute a program:
1. Firstly, the input file, i.e., hello.cpp, is passed to the preprocessor, and
the preprocessor converts the source code into expanded source code. The
extension of the expanded source code would be hello.i.
2. The expanded source code is passed to the compiler, and the compiler converts
this expanded source code into assembly code. The extension of the assembly
code would be hello.s.
3. This assembly code is then sent to the assembler, which converts the assembly
code into object code.
4. After the creation of an object code, the linker creates the executable file.
The loader will then load the executable file for the execution.
Step 1: Creating a C Source File: We first create a C program using an editor and save
the file as filename.cpp
$ vi filename.cpp
Step 2: Compiling using GCC compiler: We use the following command in the terminal for
compiling our filename.cpp source file
Step 3: Executing the program: After compilation executable is generated and we run
the generated executable using the below command.
$ ./filename
The program will be executed and the output will be shown in the terminal.
Character Set, Tokens, and Data Types
1. Character Set
2. Tokens
3. Variables
Definition: Named storage locations in memory that can hold data.
Syntax:
int a = 5;
4. Constants
const int PI = 3;
5. Data Types:
C++ provides several basic (or fundamental) data types that allow you to define
variables with different storage capacities and capabilities. These types include
integers, floating-point numbers, characters, and boolean values.
Basic Data Types in C++
1. Integer Types:
Size
Data Type Range
(Bytes)
-9,223,372,036,854,775,808 to
long long int 8
9,223,372,036,854,775,807
unsigned short
2 0 to 65,535
int
unsigned long
4 or 8 0 to 4,294,967,295 (or larger)
int
unsigned long
8 0 to 18,446,744,073,709,551,615
long int
2. Floating-Point Types:
±1.7e-308 to
double 8 15
±1.7e+308
long
8 or 16 More than double Depends on system
double
3. Character Type:
Size
Data Type Range
(Bytes)
4. Boolean Type:
5. Void Type:
Does not store any data. Used mainly in functions to specify that they
return nothing.
#include <iostream>
using namespace std;
int main() {
int a = 10; // Integer
float b = 3.14f; // Floating-point
double c = 3.1415926535; // Double precision floating-point
char d = 'A'; // Character
bool e = true; // Boolean
unsigned int f = 300; // Unsigned integer
return 0;
}
Output:
Integer a: 10
Float b: 3.14
Double c: 3.14159
Character d: A
Boolean e: 1
Unsigned Integer f: 300
Type Description
Types of Errors
Programming Errors • Errors are the problems or the faults that occur in the program,
which makes the behaviour of the program abnormal. • Programming errors are also known
as the bugs or faults. • The process of removing these bugs is known as debugging. •
These errors are detected either during the time of compilation or execution.
1. Syntax Error
2. Semantic Error
3. Logical Error
4. Run-Time Error
5. Linker Error
1. Syntax Errors: Syntax errors are also known as the compilation errors as they
occurred at the compilation time.
Example:
3. int main()
{
cout<<"Hello"; // missing close curly bracket }
4. a=10;
cout<<a; // a is undeclared
Commonly occurred syntax errors are:
2. Logical Error: The logical error is an error that leads to an undesired output.
These errors produce the incorrect output, but they are errorfree, known as logical
errors.
- The occurrence of these errors mainly depends upon the logical thinking of
the developer.
- If the programmers sound logically good, then there will be fewer chances
of these errors.
Definition: Errors where the program runs but produces incorrect results due to
flawed logic.
Example:
for(int i=0; i<=10; i++); // logical error, as we put semicolon after loop
{
cout<< i;
}
Definition: Errors that occur during the execution of the program, such as
dividing by zero or accessing invalid memory.
Example:
int a= 10;
int b= a / 0;
cout<< b;
4. Linker Error:* These error occurs when after compilation we link the
different object files with main’s object using Ctrl+F9 key(RUN).
Linker errors are mainly generated when the executable file of the
program is not created. This can be happened either due to the wrong
function prototyping or usage of the wrong header file.
The most common linker error that occurs is that we use Main() instead
of main().
Example:
#include<iostream>
using namespace std;
int Main() // undefined reference to main
{
cout<<"Hello";
return 0;
}
int i;
i=i+2;
2. Type compatibility
int b = "javatpoint";
3. Errors in expressions
int a, b, c;
a + b = c;
int a[10];
a[10] = 34;
Example: