Week 2 Lecture Programming Fundamentals
Week 2 Lecture Programming Fundamentals
Week 2 Lecture Programming Fundamentals
Introduction to Programming
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.
Conditional Execution
Looping (Repetition)
Instructions are executed multiple times using loops (e.g., for, while).
o Data Types
o Examples in C++:
cpp
Copy code
int age = 25; // Integer
float salary = 55000.50; // Floating point number
char grade = 'A'; // Character
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.
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.
#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.
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. 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).
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
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
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 3: Check if n = 0
IF n == 0 THEN
Output 1
ELSE
result = 1
FOR i = 1 TO n DO
result = result * i
END FOR
Output result
END