Fundamentals of Programming and Algorithm Design Study Guide
What is Computer Programming?
Computer programming is the process of giving computers instructions on what to do next.
These instructions are written in code by programmers to solve problems or perform tasks.
Programming languages enable humans to interact with machines and make them perform necessary operations, bridging the communication gap.
A Brief History of Computer Programming
(Ada Lovelace & Charles Babbage): The first computer program was created for the mechanical Analytical Engine. Lovelace realized that numbers could represent more than just raw quantities and wrote an algorithm to compute Bernoulli numbers.
(Assembly Language): This language was developed to simplify machine code instructions into readable symbols.
(FORTRAN): Created by John Backus specifically for scientific, mathematical, and statistical applications.
(C): Created by Dennis Ritchie as a high-level programming language that is closer to human expression.
(C++): Created by Bjarne Stroustrup as an object-oriented extension of the C language.
(Python): Developed by Guido van Rossum with a primary focus on code readability and simplicity.
(Java): Developed by Sun Microsystems, originally targeted at handheld devices.
(C#): Developed by Microsoft as an object-oriented language combining features of C++ and Visual Basic.
(Swift): Created by Apple to modernize development for iOS and macOS systems.
Types of Languages and Classification
Low-Level Languages:
Machine Language: Consists purely of binary ( and ). It is executed directly by hardware without the need for translation. Example: Machine Code.
Middle-Level Languages:
Assembly Language: Uses short text symbols called mnemonics (e.g., ). It must be converted to binary using an Assembler.
High-Level Languages:
High-Level: Features human-readable syntax using English keywords and strict rules known as Syntax. It is converted using a Compiler or Interpreter. Examples include C, C++, Java, Python, and FORTRAN.
Translators and Development Tools
Assembler:
Definition: Converts Assembly language instructions into raw machine code ( and ).
How it Works: Converts human-readable mnemonics like , , and directly into binary instructions for the CPU.
Key Feature: Translates code on a basis, meaning one line of assembly equals one line of machine code.
Compiler:
Definition: Translates the entire high-level program into machine code at once before running it.
How it Works: Reads all source code, checks for syntax errors, optimizes it, and outputs an executable file (e.g., ).
Key Feature: Programs execute faster, but all syntax errors must be fixed before the program can run. Examples include compilers for C, C++, and Rust.
Interpreter:
Definition: Translates and executes high-level source code line-by-line at runtime.
How it Works: Reads one line of code, translates it to machine code, runs it, and then moves to the next line.
Key Feature: Execution is slower, but debugging is easier because the program stops exactly at the line containing the error. Examples include interpreters for Python, JavaScript, and Ruby.
IDE (Integrated Development Environment):
Definition: A single software application that combines all development tools into one window.
Components Included:
Code Editor with syntax highlighting and auto-completion.
Compiler/Interpreter buttons to run code instantly.
Debugger to step through code line-by-line.
Examples: Visual Studio Code, CLion, Eclipse, and PyCharm.
Structured Programming and the C Language
Structured programming enforces clear logical control structures to make source code readable, modular, and maintainable.
Fundamentals of C Program Structure:
#include <stdio.h>: Header file inclusion. Header files () are libraries containing standard pre-defined functions. They are loaded using#include <filename.h>directives. For example,<stdio.h>provides standard input/output operations likeprintfandscanf.int main(void): The exact execution entry point for the program. It is equivalent to the "trigger event" in block-based programming (such as "when green flag clicked").Curly Braces (
{ }): Define code blocks and the scope of execution.Statements & Semicolons (
;): Every command line must terminate with a semicolon.printf("hello, world\n");: A statement that prints text to the screen.return 0;: A statement indicating the function has completed successfully.
Key Programming Components:
Functions: Modular, reusable blocks of code that perform specific operations.
Arguments: Values passed into functions as inputs inside parentheses (e.g.,
"hello, world\n"inprintf()).Variables & Data Types: Memory locations used to store changeable values. Example of integer declaration:
int age;.Assignment: The process of setting a value, such as
counter = counter + 1;.
Logic Formulation & Data Representation
Logic formulation is the systematic, step-by-step approach to analyzing and designing solutions to problems before writing executable code.
Representing Numbers:
Decimal System: Human-standard counting using digits ().
Binary System: Computer-standard counting using digits ( and ).
Bit: A single binary digit ( or ).
Hardware Implementation: Computers use electricity. Bits are represented physically using tiny switches called transistors, which are turned for and for .
Byte: A sequence of bits. Example: represents the number .
Decimal to Binary Examples:
Representing Text:
Text is represented by mapping numbers to characters.
ASCII (American Standard Code for Information Interchange): Standard numerical mapping for basic characters, uppercase/lowercase letters, and punctuation.
'A'is ( in binary).'B'is ( in binary).Example message
"HI!": ASCII Decimal: , , . Binary: , , .
Unicode: An expanded standard using more bits than ASCII to accommodate international symbols, accented characters, and emojis. Example: "Face with medical mask" emoji ( f637 ) is represented by bytes: , , , .
Representing Media:
Images: Displays consist of tiny square dots called pixels. Colors are represented using the RGB (Red, Green, Blue) standard, requiring bytes (one per channel with values from ).
Black:
White:
Red:
Green:
Blue:
Video: A sequence of many image frames changing rapidly every second to create the perception of motion.
Sound: Audio formats like MIDI represent music digitally by mapping numbers to musical notes, duration, and volume.
Algorithms and Efficiency
An algorithm is a step-by-step set of instructions for solving a problem.
Phone Book Search Example:
Algorithm 1 (Linear Search): Flip page at a time from start to end. Correctness: Correct. Efficiency: Up to steps.
Algorithm 2: Flip pages at a time. Correctness: Incorrect (might accidentally skip the target page). Efficiency: Up to steps.
Algorithm 3 (Binary Search): Open to the middle, check if the name is in the left or right half, discard the unused half, and repeat. Correctness: Correct. Efficiency: steps.
Pseudocode Standards and Rules
Pseudocode is a plain English representation of an algorithm specifying logical flow and operations.
Building Blocks of Pseudocode:
Functions (Actions/Verbs): Commands like
PICK UP,OPEN TO,LOOK AT,CALL.Conditionals (Branches): Paths based on conditions, such as
IF,ELSE IF,ELSE.Boolean Expressions (Questions): Statements evaluating strictly to
TRUE/FALSEorYES/NO(e.g., "person is on page").Loops (Cycles): Constructs that repeat specific execution sequences (e.g.,
GO TO line 3).
Core Formatting Rules:
Capitalized Control Keywords: Keep control structures uppercase (
IF,THEN,ELSE,WHILE,FOR,FUNCTION,RETURN,START,END).Capitalized I/O: Write input/output operations in uppercase (
READ,PRINT,INPUT,OUTPUT).Indentation: Use indentation to define blocks inside
IF,WHILE, orFORstructures.One Action Per Line: Avoid multi-step statements on a single line.
Pseudocode Operators:
Assignment:
=(e.g.,total = 0).Comparison:
==,!=,<,>,<=,>=.
Pseudocode Examples
Example 1: Product of Two Numbers
STARTREAD num1, num2answer = num1 * num2PRINT answerEND
Example 2: Check Specific Input Values
STARTREAD userInputIF userInput == 5 THENPRINT "Your number is 5"ELSE IF userInput == 6 THENPRINT "Your number is 6"ELSEPRINT "Your number is not 5 or 6"ENDIFEND
Example 3: Range Check & Color Mapping
STARTPRINT "Please enter a number"READ colornumIF colornum > 0 AND colornum <= 10 THENPRINT "Blue"ELSE IF colornum > 10 AND colornum <= 20 THENPRINT "Red"ELSE IF colornum > 20 AND colornum <= 30 THENPRINT "Green"ELSEPRINT "Not a correct color option"ENDIFEND
Example 4: Print Multiples of 5 (Loop)
STARTx = 1WHILE x <= 20PRINT x * 5x = x + 1ENDWHILEEND
Flowchart Standards
Flowcharts are graphical diagrams representing execution flow through standardized geometric symbols.
Flowchart Symbols:
Terminal (Oval): Marks the
STARTandENDpoints.Input / Output (Parallelogram): Represents taking data in or writing data out.
Process (Rectangle): Indicates internal calculations, actions, or variable assignments.
Decision (Diamond): Evaluates a conditional expression returning
TRUE/FALSEorYES/NO.Flow Line (Arrow): Defines the execution progression path between symbols.
Connector (Circle): Connects separate flow path segments on a single page.
Practice Exercises
Question 1: Check Odd or Even Number
Task: Write pseudocode and describe a flowchart that prompts for an integer, checks if it is even or odd, and displays the result.
Question 2: Find the Greatest of Two Numbers
Task: Write pseudocode and describe a flowchart that takes two numbers and prints the larger value. If equal, display a message stating equality.
Question 3: Calculate Student Pass/Fail Status
Task: Write pseudocode prompting for marks (). If marks , print "Pass"; otherwise, print "Fail". Include validation for the range .
Question 4: Sum of Numbers from 1 to N
Task: Write pseudocode and describe a flowchart to calculate and print the sum of all positive integers from up to a user-entered number .