You are on page 1of 36

Introduction to Programing

Introduction:
A programming language is a set of instructions and syntax used to
create software programs. Some of the key features of programming
languages include:
Syntax: The specific rules and structure used to write code in a
programming language.
Data Types: The type of values that can be stored in a program, such as
numbers, strings, and booleans.
Variables: Named memory locations that can store values.
Operators: Symbols used to perform operations on values, such as
addition, subtraction, and comparison.
• Control Structures: Statements used to control the flow of a program,
such as if-else statements, loops, and function calls.
• Libraries and Frameworks: Collections of pre-written code that can be
used to perform common tasks and speed up development.
• Paradigms: The programming style or philosophy used in the
language, such as procedural, object-oriented, or functional.
Compiler and Linker: Roles and Functions
• Compiler:
• Function:
• A compiler is a software tool that translates the high-level source
code written in a programming language (like C, C++, Java) into
machine code (binary code) that can be understood and executed by
a computer's processor.
• Stages: The process typically involves several stages: lexical analysis,
syntax analysis, semantic analysis, optimization, and code generation.
• Output:
• The output of a compiler is usually an executable file or an
intermediate representation (like bytecode in Java) that can be
further processed or executed.
2. Linker:
Function:
• After the compilation process, the linker takes over. Its primary function
is to combine multiple object files and libraries to produce a single
executable file or a library that can be loaded into memory for execution.
• Tasks:
• Symbol Resolution:
• The linker resolves references between different object files, ensuring
that functions and variables defined in one file and used in another are
correctly linked.
• Address Binding: It assigns final memory addresses to functions and
variables in the executable.
• Static and Dynamic Linking:

• Linking can be static (linker combines all necessary code into a single
executable file) or dynamic (linker links the program to external
libraries at runtime)
Introduction to Algorithms
• What Exactly Is an Algorithm?
• An algorithm is a set of steps for solving a known problem.

Steps of Algorithm

• Most algorithms are implemented to run following the four steps below:
• take an input
• access that input and make sure it's correct
• show the result
• terminate (the stage where the algorithm stop running)
Some steps of the algorithm may run repeatedly, but in the end, termination is what ends an
algorithm.
Example
• For a theoretical basis, for instance, an algorithm for dividing two
numbers and showing the remainder could run through the steps
below:
• Step 1: the user enters the first and second numbers – the dividend
and the divisor
• Step 2: the algorithm written to perform the division takes in the
number, then puts a division sign between the dividend and the
divisor. It also checks for a remainder.
• Step 3: the result of the division and remainder is shown to the user
• Step 4: the algorithm terminates
Characteristics of an algorithm:

• Precision – the steps are precisely stated.


• Uniqueness – results of each step are uniquely defined and only
depend on the input and the result of the preceding steps.
• Finiteness – the algorithm stops after a finite number of instructions
are executed.
• Input – the algorithm receives input.
• Output – the algorithm produces output.
• Generality – the algorithm applies to a set of inputs.
Types of algorithms

• There are several types of algorithms, all designed to accomplish


different tasks:
• Greedy algorithm. This algorithm solves optimization problems by
finding the locally optimal solution, hoping it is the optimal solution at
the global level. However, it does not guarantee the most optimal
solution.
• Recursive algorithm. This algorithm calls itself repeatedly until it solves a
problem. Recursive algorithms call themselves with a smaller value every
time a recursive function is invoked.
• Backtracking algorithm. This algorithm finds a solution to a given
problem in incremental approaches and solves it one piece at a time.
• Divide-and-conquer algorithm. This common algorithm is divided into
two parts. One part divides a problem into smaller sub problems. The
second part solves these problems and then combines them to produce
a solution.
• Dynamic programming algorithm. This algorithm solves problems by
dividing them into sub problems. The results are then stored to be
applied to future corresponding problems.
• Brute-force algorithm. This algorithm iterates all possible solutions to a
problem blindly, searching for one or more solutions to a function.
• Randomized algorithm. This algorithm reduces running times and time-
based complexities. It uses random elements as part of its logic.
Advantages of Algorithms:

• 1. It is a step-wise representation of a solution to a given problem, which


makes it easy to understand.
• 2. An algorithm uses a definite procedure.
• 3. It is not dependent on any programming language, so it is easy to
understand for anyone even without programming knowledge.
• 4. Every step in an algorithm has its own logical sequence so it is easy to
debug.
• 5. By using algorithm, the problem is broken down into smaller pieces or
steps hence, it is easier for programmer to convert it into an actual program.
Disadvantages of Algorithms:

• 1. Alogorithms is Time consuming.


• 2. Difficult to show Branching and Looping in Algorithms.
• 3. Big tasks are difficult to put in Algorithms.
Variables and Data Types in C Programming

Variables
• In a programming language, a variable is a memory location where
you store a value. The value that you have stored may change in the
future according to the specifications.
Example
• int x;
• Int X=5;
Data Types
• In C++, data types are declarations for variables. This determines the
type and size of data associated with variables.

For example,
int x = 13;
Here, x is a variable of type int. Meaning, the variable can only store
integers of either 2 or 4 bytes.
Rules for declaring variables and data
types
• There are set of rules to be followed while declaring variables and data
types in C Programming:
• The 1st letter should be alphabet.
• Variables can be combination of alphabets and digits.
• Underscore (_) is the only special character allowed.
• Variables can be written in both Uppercase and Lowercase or combination
of both.
• No Spaces allowed between Characters.
• Variable name should not make use to the C Reserved Keywords.
• Variable name should not start with a number
C++ Fundamental Data Types
The table below shows the fundamental data types, their meaning, and their sizes (in bytes):
Now, let us discuss these fundamental data types in more detail.

Data Type Meaning Size (in Bytes)

int Integer 2 or 4

float Floating-point 4

double Double Floating-point 8

char Character 1
bool Boolean 1
String String 4
1. C++ int
•The int keyword is used to indicate integers.
•Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
•For example
Int salary;

int salary = 85000;


2. C++ float and double
•float and double are used to store floating-point numbers (decimals and exponentials).
•The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the
precision of float
•For example,
float area = 64.74;
double volume = 134.64534;

3. C++ char
•Keyword char is used for characters.
•Its size is 1 byte.
•Characters in C++ are enclosed inside single quotes ' '.
•For example,
char test = 'h';
5. C++ bool
•The bool data type has one of two possible
values: true or false.
•Booleans are used in conditional statements and loops.
•For example,
bool cond = false;
• string: we use this for any data that is text. For example, a name or
address or message. In most programming languages, strings require
quotes. Notice that the text inside of the quotes can include spaces
and other special characters.

• usersName = “The Nishtar“;

You might also like