Week 2 Lecture Programming Fundamentals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Programming Fundamentals: Week 2 Detailed Lecture Notes

Introduction to Programming

1.1 What is Programming?

 Programming is the process of writing instructions that a computer can execute to


perform specific tasks. It involves converting logical problem-solving steps into code.
 Why Program?
o To solve problems, automate tasks, create software, develop applications,
and control hardware.
o Emphasize that programming is a crucial skill in many fields, including
computer science, engineering, business, and more.

1.2 Components of Programming:

1. Syntax and Semantics:


o Syntax

Rules that define how code should be written. Syntax errors (e.g.,
missing semicolons) prevent the program from running.

o Semantics
The meaning behind the code. For example, using addition when
subtraction was intended is a semantic error.

2. Logic and Control Flow:


o Control flow structures determine the order in which instructions are executed in a
program. Common structures include:
 Sequential Execution:

Instructions are executed one after another.

 Conditional Execution

Instructions are executed based on conditions (e.g., if statements).

 Looping (Repetition)

Instructions are executed multiple times using loops (e.g., for, while).

3. Variables and Data Types:


o Variables
Variables are used to store data, which can be modified during
program execution.

o Data Types

Data Types specify the type of data stored in a variable (e.g.,


integers, floating-point numbers, characters).

o Examples in C++:

cpp
Copy code
int age = 25; // Integer
float salary = 55000.50; // Floating point number
char grade = 'A'; // Character

1.3 Importance of a Programming Language:


 What is a Programming Language?
o A programming language is a formal language comprising a set of instructions
that produce various kinds of output.
o C++ is widely used for its efficiency, control over system resources, and support
for object-oriented programming.
 Types of Programming Languages:
o Low-Level Languages: Close to machine language, with limited abstraction (e.g.,
Assembly language).
o High-Level Languages: Abstracted from machine language, user-friendly syntax
(e.g., C++, Python, Java).

1.4 Key Programming Paradigms:


1. Procedural Programming: Focuses on a sequence of steps or procedures to solve
problems (e.g., C, Python).
2. Object-Oriented Programming (OOP): Organizes code into objects with attributes
and behaviors (e.g., C++, Java).
3. Functional Programming: Emphasizes the use of functions and avoids changing
states (e.g., Haskell, Lisp).

2. Role of Compilers and Linkers (30 minutes)

2.1 What is a Compiler?

 A compiler is a tool that translates code written in a high-level programming


language into machine code (binary code) that a computer can understand.
 In C++, the compiler is essential for converting written code into an executable
format.

2.2Compilation Process:

1. Source Code
Source code is a set of instructions that a programmer writes to create a computer
program. It's written in a language that humans can understand, like Python or Java.
The computer uses these instructions to perform tasks. Think of it like a blueprint for
building a software. The original C++ code written by the programmer.

2. Compiler
The compiler translates the source code into machine code or an
intermediate object file. A compiler is a tool that translates the code you write in a
programming language into machine code that a computer can understand and
execute. It helps turn human-readable instructions into a language that the
computer's hardware can follow to perform tasks.

3. Object Code
The machine code generated by the compiler, not yet linked into a complete
program. Object code is the machine-readable instructions created from your source
code. It's what the computer can directly understand and execute to perform tasks.
In short, it's the computer's version of your program.

4. Linking
The linker combines multiple object files into a single executable file. Linking
is the process of connecting different pieces of code together to create a single
executable program. It ensures that all the parts of your program can find and use
each other correctly. Think of it as putting together the pieces of a puzzle to form a
complete picture that the computer can run.

5. Execution
The operating system loads the executable into memory, and the CPU
executes it. Execution is the process of running a program on a computer. When a program
is executed, the computer follows the instructions written in the code to perform tasks, like
displaying text or calculating numbers. It's like giving the computer a set of commands to
carry out.
2.3 Phases of Compilation in C++:
1. Preprocessing
The preprocessor handles directives like #include and #define, expanding
macros and including libraries. Preprocessing is the step where the preprocessor
modifies the code before it's compiled. It processes directives like #include and
#define. This step helps set up the code for the actual compilation.

2. Compilation
The compiler translates the preprocessed code into assembly language.
Compilation is the process where the compiler translates the preprocessed source
code into machine code or an intermediate code (like object code). This step
converts human-readable code into a form that the computer can understand and
execute.

3. Assembly
Assembly refers to a low-level programming language that is closely related to
machine code. The assembler converts the assembly code into machine code
(binary).

4. Linking
The linker combines the binary code with any required libraries, producing a
complete executable. Linking is the process of combining different pieces of code
(like object files or libraries) into a single executable program. It connects functions
and variables from various files and resolves references between them, ensuring
that all parts of the program work together.

2.4 Errors During Compilation:

1. Syntax Errors

Occur when the syntax rules of the language are violated (e.g., missing
semicolons). Syntax errors are mistakes in the code that break the rules of the
programming language. They happen when you miss something like a semicolon,
parentheses, or use a wrong word. The program won't work until these mistakes are
fixed.

2. Linking Errors
Happen when the linker cannot find necessary files or libraries to create the
executable. Linking errors occur when the compiler is unable to connect different parts of
the program, such as functions or variables, during the linking process. These errors happen
if the code refers to something that doesn't exist or can't be found, like a missing function
definition or an incorrect library.

3. Runtime Errors
Runtime errors are mistakes that occur while the program is running. These errors
happen during execution, such as trying to divide by zero, accessing invalid memory, or
running out of memory. The program crashes or behaves unexpectedly when a runtime
error occurs. Occur during execution, not during compilation, such as dividing by zero.

4. Logical Error
Logical errors happen when the code runs, but doesn't do what you expect. The
program works, but gives wrong results because of mistakes in the logic, like adding
when you should subtract.

2.5 Example of a Simple C++ Program and Its Compilation:


1. Source Code:

#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!" << endl;
return 0;
}

2. Compilation Steps:
o Preprocessing: Includes <iostream>.
o Compilation: Converts to machine code.
o Linking: Links necessary libraries for cout.

2.6 What is a Linker?


 The linker is a program that combines different object files into a single executable
program.
 It links the code with necessary libraries and handles external dependencies.

3. Introduction to Algorithms
3.1 What is an Algorithm?
 An algorithm is a well-defined sequence of steps designed to perform a task or solve
a problem.
 Algorithms are essential to programming because they form the foundation of logic
and problem-solving.

3.2 Characteristics of a Good Algorithm:

1. Finite: An algorithm should have a clear end point.


2. Definite: Each step should be precise and unambiguous.
3. Effective: The algorithm should achieve the desired result efficiently.
4. Input/Output: Clearly defined inputs and expected outputs.

3.3 Types of Algorithms:


1. Simple Computational Algorithms
Simple Computational Algorithms are step-by-step procedures or
instructions used to solve a specific problem or perform a task. They are designed to
be simple and efficient, focusing on basic operations like addition, subtraction,
multiplication, or searching. Perform arithmetic or basic calculations (e.g., finding
the sum of numbers).

2. Sorting and Searching Algorithms


Sorting and Searching Algorithms are fundamental techniques in computer
science used to organize and find data efficiently. Arrange or locate data efficiently
(e.g., bubble sort, binary search).

3. Recursive Algorithms
Recursive Algorithms are algorithms that solve a problem by solving smaller
instances of the same problem. They repeatedly call themselves with simpler or
smaller inputs until they reach a base case, which stops the recursion. Call
themselves within the solution (e.g., calculating factorials).

3.4 Writing an Algorithm:


1. Define the Problem: Clearly state what you’re trying to solve.
2. Determine Inputs and Outputs: Identify what data the algorithm will need and what
it should return.
3. Step-by-Step Solution: Outline the steps required to solve the problem.
4. Review and Test: Check the logic for correctness.
3.5 Example Algorithm for Problem-Solving:
 Problem Statement: Write an algorithm to check if a number is positive, negative, or
zero.

Algorithm Steps:

1. Start
2. Input a number n
3. If n > 0, print "Positive"
4. Else if n < 0, print "Negative"
5. Else, print "Zero"
6. End

3.6 Pseudocode for Simple Algorithms:


Pseudocode is a simple, informal way to describe an algorithm or program's logic
without worrying about the syntax of a specific programming language. It uses plain
language and basic programming constructs to represent the steps needed to solve a
problem.

 Example: Write pseudocode to find the maximum of three numbers.

Pseudocode:

Start
Input a, b, c
If (a > b) and (a > c)
Print "a is the largest"
Else if (b > a) and (b > c)
Print "b is the largest"
Else
Print "c is the largest"
End

3.7 Real-World Applications of Algorithms:


1. E-commerce Search: Algorithms to find relevant products in an online store.
2. Social Media: Algorithms for recommendations, friend suggestions, and content
ranking.
3. Data Analysis: Algorithms for data sorting, filtering, and statistical analysis.

Homework:
1. Read Chapter 2 of Starting Out with Programming Logic and Design by Tony Gaddis
for a deeper understanding of algorithms.
2. Write an algorithm to calculate the factorial of a number n. Define the steps clearly
and check for boundary conditions like n = 0.

START

Step 2: Input the number n

Step 3: Check if n = 0

IF n == 0 THEN

Output 1

ELSE

Step 4: For n > 0

result = 1

FOR i = 1 TO n DO

result = result * i

END FOR

Step 5: Output the result

Output result

END

You might also like