CC103 Computer Programming 1 - Introduction to Programming and Program Logic
CC 103 Computer Programming 1 – Unit 1 Notes
Data Processing and Programming Basics
- Programming is the process of designing a structured plan and translating it into a sequence of actions that must be followed in logical order, using appropriate tools and components, to assemble everything into a complete and functional solution that solves a problem.
- Computer Programming is the process planning and writing a sequence of instructions in logical order for the computer to execute, using the appropriate tools and components to convert data into meaningful information, building a complete and functional solution.
- Data Processing Cycle (Information Processing Cycle): a set of steps the computer follows to receive data, process the data according to instructions from a program, display the resulting information to the user, and store the results. The cycle is often labeled as the Information Processing Cycle.
- Steps explicitly include: input, process, output, and storage (as part of the cycle).
The Program Development Process
- Understanding the Problem: capture a clear idea of what you want to do. Define inputs, how input is processed, and the required output.
- Emphasizes identifying input, process, and output (the basics of program development).
- Develop the Solution: prepare a detailed plan to implement the solution and develop the program logic.
- Tools to help: (1) Algorithm / pseudocode, (2) Flowcharts, (3) Structure charts.
- Implementation: given the detailed design, write the source code.
- Testing: find and correct errors (debugging). A bug is any error in a program. Testing involves tracing (simulating) how information is processed.
- Documentation: after finishing and testing, provide documentation for the program, including operating system and hardware requirements.
- Maintenance: maintain or update the program to keep it running smoothly and up-to-date with field developments.
Testing: Black Box vs White Box
- Black Box Testing: test the program without knowing its internal structure or how it works. Test plans are developed from requirements statements and then used to test the system as a whole.
- White Box Testing: test the program by assuming knowledge about the internal structure and that every instruction and possible situation has been tested.
Documentation and Maintenance
- Documentation: accompanies the finished program and includes information about requirements, operating system, and hardware requirements needed for the program to run.
- Maintenance: ongoing updates to keep the program functioning as environments and requirements evolve.
Programming Languages: Definitions
- A programming language is a vocabulary and set of grammatical rules for instructing a computer to perform tasks. Each language has unique keywords and a syntax for organizing program instructions.
- Syntax: set of grammatical rules for writing statements in a programming language.
- Semantics: meaning associated with statements or code.
Compilers, Interpreters, and Program Representations
- Compiler: translates a program written in a high-level language into machine-readable form (binary code) before execution.
- Interpreter: translates high-level instructions into machine-understandable instructions at runtime.
- Source Program: the original program written in a high-level language.
- Object Program: the machine language version of a program.
- Regardless of language, the program must be converted into machine language for the computer to understand; a compiler converts source code to a machine-language module (object file).
Procedural vs Object-Oriented Programming (OOP)
- Procedural Programming: a sequence of instructions that tell the computer what to do, organized into procedures/routines/subroutines. Also called imperative programming or top-down programming.
- Object-Oriented Programming (OOP): computations are carried out using objects as the basic unit. An object encapsulates data (attributes) and behavior (methods). Example: a Person object with attributes (name, height, weight) and possible actions (walk, run) defined as methods.
- Programming Paradigm: 1) Object, 2) Method, 3) Class.
- Class: blueprint for creating objects; defines attributes (data) and methods (functions).
- Object: an instance of a class with its own attribute values.
- Method: a function defined within a class that operates on the object's data.
Unit 1 – Unit Headers (Context)
- INTRODUCTION TO PROGRAMMING; UNIT 1; CC 103 Computer Programming 1
- UNIT 1 and UNIT 1 content include: Basic Programming Concepts, Program Logic Formulation, Program Control Structures.
Lesson 2 – Program Logic Formulation
- Definition: It is the process of coming up with the appropriate methodology in developing a specific program logic that will perform a prescribed computing task or solve a problem using the computer. Logic is the method of correct linear, step-by-step thinking to solve a problem.
- Program Logic Formulation: framework for deriving the logic used to implement a program.
- Algorithm / Pseudocode:
- Algorithm: a set of well-defined instructions to solve a particular problem; it takes inputs and produces outputs.
- Pseudocode: a plain-English, simplified version of code that describes the steps to implement before actual programming.
- Tools in Logic Formulation: Algorithm/Pseudocode, Flowcharts, Structure Charts.
- Typical algorithmic steps for solving problems:
- 1. Initialize data.
- 2. Read/Input the data.
- 3. Process (perform computations).
- 4. Output (display results).
- Initialization: preliminary actions required by a program.
- Input: collecting the data used by the program.
- Process: transforming data from one form to another.
- Output: presenting the processed input.
Example Problems (Algorithm Design)
- Problem 1: Sum of two numbers
- Data: two numbers; Goal: compute sum and output it.
- Algorithm sketch:
- 1) get(input/read) two numbers.
- 2) compute sum
- 3) output sum
- Example variables: s = num1 + num2; Input num1, num2; Output s.
- Problem 2: Average of two numbers
- Data: two numbers; Goal: compute average and output it.
- Algorithm sketch:
- 1) get(input/read) two numbers.
- 2) compute average
- 3) output average
- Formula: ext{ave} = rac{num1 + num2}{2}
- Problem 3: Employee bonus (25% of salary; salary = hours worked × rate per hour)
- Data: hours worked, rate per hour; compute salary; compute bonus as 25% of salary; output bonus.
- Data/Process outline:
- hours worked = h; rate per hour = r; salary s = h × r; bonus b = 0.25 × s; output b.
- Example variables: Let h = hours worked; r = rate per hour; s = salary; b = bonus; Input h, r; s = h × r; b = 0.25 × s; Output b.
Flowcharting and Flowchart Symbols
- Flowchart is a diagrammatic representation of the sequence of operations to solve a problem.
- Flowcharting Symbols:
- Terminal: Start and End
- Input/Output
- Process
- Decision
- Off-page connector
- On-page connector
- Predefined Process
- Flowcharting Guidelines:
1) A flowchart always begins with START and ends with END.
2) Symbols are interconnected using arrows.
3) Use a comma to separate data and a semicolon to separate instructions.
4) All symbols except the diamond (Decision) may have only one outgoing arrow; they may have multiple incoming arrows.
5) When using circles/loops, the symbol leading to the circle should flow to the symbol where a circle containing a similar character leads.
6) The sequence of symbols matters because it indicates the logical steps to be followed.
7) A flowchart may contain many symbols of the same kind depending on the solution.
8) A flowchart can have many steps; the simplest efficient flowchart is advisable.
Flowchart Examples (Sum Problem)
- Flowchart trace examples show the flow of data through initialization, input, processing, and output.
- Example flow path (Sum):
- start → s = 0; num1 = 0; num2 = 0; Output s → s = num1 + num2 → Input num1, num2 → end
- Textual representation of a sample flowchart (Sum) exists in the notes as a sequence of steps, mirroring the algorithm:
- start
- s = 0
- num1 = 0
- num2 = 0
- Output s
- s = num1 + num2
- Input num1, num2
- end
Flowchart Tracing Practice
- Flowchart Tracing involves predicting the output of a given flowchart for specified inputs.
- Example: Given inputs, determine the outputs A, B, C, D (or other letters) by following the flowchart logic.
- Additional traces show sequences like: A = 0; B = 0; C = 0; D = 0; inputs alter A, B, C, D; final outputs recorded.
- In some trace problems, you may see a table of inputs and the resulting outputs to follow step-by-step.
Lesson 3 – Program Control Structures
Overview
- Program Control Structures are the ways to control the flow of a program’s execution.
- They help make algorithms more clear and organized by dividing code into logical units called control structures.
- Basic types:
- Sequence logic (sequential flow)
- Selection logic (conditional flow)
- Iteration logic (repetitive loop)
- Modular/Procedural control (divide-and-conquer modular approach)
Sequential Logic
- Definition: Steps are executed in a strictly sequential manner, each step exactly once.
- Example: Problems 1 (sum), 2 (average), 3 (bonus) can be implemented in sequence.
Selection / Conditional Logic
- Definition: Execution depends on evaluating conditions; based on true/false outcomes, different instructions execute.
- Form: if (Condition) then Process A else Process B
- Decision symbol in flowcharts represents this construct.
- Condition expressions are typically mathematical comparisons (e.g., equals, less-than, greater-than).
- Example problems:
- Problem 4: Determine if a grade is "passed" or "failed" with passing grade 75.
- Algorithm (concept): If grade ≥ 75 then output "passed" else output "failed".
- Formula form: ext{pass} ext{ if } ext{grade} \ge 75 ext{, else fail}
- Problem 5: Bonus with salary-based rule: If salary ≥ 10000 then bonus = 50% of salary, else 25%.
- Formula: b = egin{cases} 0.50 imes s & ext{if } s \ge 10000 \ \ 0.25 imes s & ext{otherwise} \
\end{cases} - Problem 6: Extended bonus rule with three branches:
- If salary < 10000 → 25%
- If salary > 10000 → 50%
- If salary = 10000 → 35%
- Salary defined as s = h imes r (hours × rate per hour)
- Note: The problem statements illustrate how conditional logic is applied to real tasks such as determining pass/fail and tiered bonuses.
Repetition / Iteration / Loop
- Repetition (loops) allows a set of instructions to be repeated until a condition is met.
- Two types:
- Count-controlled loop: repeats a fixed number of times via a counter.
- Condition-controlled loop: repeats until a condition becomes false or true, not knowing the exact count in advance.
- Typical loop lifecycle: Initialization of counter → test → body → increment/decrement → repeat until termination condition.
Modular / Procedural Programming
- Approach: divide a large task into smaller, manageable tasks (modules or procedures).
- Predefined Processes are used as building blocks in a modular program.
- Example structure (conceptual):
- start
- choice = 0
- Input choice
- if choice = 1 then Compute_AREA
- else if choice = 2 then Compute_PERIMETER
- else print "Invalid Choice"
- end
Problem Solving via Flowcharts and Tracing
- Practice problems include tracing the output of given flowcharts with specific inputs.
- Examples show how to track variables through each step, including initialization, input, processing, and output.
- Typical flowchart tracing tasks in the unit: determine outputs for given inputs such as A, B, C, D, or other variables based on the flow of operations.
Summary of Key Concepts and Formulas
- Data Processing Cycle: input → process → output → store (data to information transformation).
- Salary and Bonus relationships:
- Salary: s = h imes r
- Bonus (base case): b = 0.25 imes s
- Bonus with threshold: b = egin{cases} 0.50 imes s & ext{if } s \,\ge 10000 \ 0.25 imes s & ext{otherwise} \
egin{cases}
- Conditional logic example: ext{If } ext{grade} \ge 75 ext{ then