Fundamentals of Programming Study Notes

Flowcharting, Algorithms, and Systematic Problem Solving

  • Algorithms:

    • An algorithm is a structured sequence of step-by-step instructions designed to solve a specific problem or perform a task.

    • Algorithms provide a systematic framework to decompose complex problems, ensuring clarity, logical correctness, and computational efficiency.

  • Flowcharts:

    • A flowchart is a visual representation of an algorithm or process using standardized geometric symbols connected by flowlines.

  • Graph Structure Fundamentals:

    • In visual problem modeling, diagrams often rely on graph concepts consisting of Nodes/Vertices (representing entities or states) connected by Edges (representing relationships or transitions).


Introduction to Graphs Diagram
  • Standard Flowchart Symbols:

    • Terminal (Oval / Rounded Rectangle): Indicates the Start or End point of a program or sub-process.

    • Process (Rectangle): Represents an action step, arithmetic operation, or data manipulation.

    • Decision (Rhombus / Diamond): Represents a conditional evaluation branching into alternative paths (e.g., Yes/No or True/False).

    • Input/Output (Parallelogram): Denotes input receiving operations or output display processes.

    • On-Page Connector (Circle / Pentagon): Represents a jump or connection point within the same diagram page.

    • Flowline (Arrowed Line): Dictates the directional flow of control.

  • Algorithm Example 1: Determining if a Number is Even or Odd:

    1. Start: Initiate execution.

    2. Preparation: Initialize necessary variables.

    3. Input Number: Read integer input num\text{num} from the user.

    4. Process: Compute remainder using modulo operation: ans=num mod 2\text{ans} = \text{num} \bmod 2

    5. Decision: Is ans==0\text{ans} == 0?

      • If Yes: Branch to print "Even".

      • If No: Branch to print "Odd".

    6. Print Even: Output that the value is even.

    7. Print Odd: Output that the value is odd.

    8. End / Stop: Terminate process.


Flowchart Even or Odd Check
  • Algorithm Example 2: Student Grade Pass/Fail Evaluation:

    1. Start: Begin process.

    2. Input Grade for Quiz: Receive numerical grade for quiz.

    3. Input Grade for Activity: Receive numerical grade for activity.

    4. Input Grade for Exam: Receive numerical grade for exam.

    5. Calculate Average Grade: Compute average using the formula:         Average=Quiz+Activity+Exam3\text{Average} = \frac{\text{Quiz} + \text{Activity} + \text{Exam}}{3}

    6. Decision: Is Average≥75\text{Average} \ge 75?

      • If Yes: Display "PASSED".

      • If No: Display "FAILED".

    7. Stop: Terminate algorithm execution.


Flowchart Grade Pass or Fail Check
  • Core Characteristics of an Algorithm:

    • Finiteness: An algorithm must always terminate after a finite number of steps.

    • Definiteness: Each step must be precisely defined and unambiguous.

    • Effectiveness: Operations must be sufficiently basic that they can be performed accurately and in finite time.

    • Efficiency: The algorithm must optimize resource usage, such as processing time and memory execution.

  • Key Benefits of Flowcharting:

    • Easy to Understand: Simplifies logical tracking through pictorial representation.

    • Visual Representation: Provides a clear macro view of system operations.

    • Improved Communication: Facilitates logic sharing across multi-disciplinary teams.

    • Debugging: Assists in isolating syntax and logical branching errors prior to coding.

    • Documentation: Serves as a blueprint for system maintenance and updates.

Software Development Life Cycle (SDLC)

  • Definition:

    • The Software Development Life Cycle (SDLC) is a structured framework that defines the sequential phases of software production to ensure high-quality software, delivery within deadlines, and effective resource management.

  • Sequential Stages of SDLC (Waterfall Perspective):

    1. Systems Requirements: Gathering functional and technical requirements from stakeholders.

    2. Analysis: Analyzing feasibility, system constraints, and input/output requirements.

    3. Design: Architecting system workflows, databases, flowcharts, and interface layouts.

    4. Coding: Writing source code using programming languages like C based on design specs.

    5. System Test: Executing test protocols to uncover syntax errors, runtime exceptions, and logical bugs.

    6. Maintenance: Updating, patching, and modifying software post-deployment to adapt to user feedback or environment changes.


SDLC Waterfall Model
  • Importance of SDLC in Problem-Solving:

    • Provides a Structured Approach to handle complex projects.

    • Establishes Clarity and Focus for project requirements.

    • Enables systematic Risk Management throughout the development cycle.

    • Ensures rigorous Quality Assurance prior to deployment.

    • Generates comprehensive project Documentation.

    • Encourages structured User Involvement.

    • Offers systematic Flexibility for Changes.

    • Guarantees long-term Post-Implementation Support.

Evolution of Programming Languages and C Fundamentals

  • Evolutionary Paradigms of Programming Languages:

    • Early Languages (Machine Code & Assembly): Hardware-dependent languages written in binary or low-level mnemonic opcodes.

    • High-Level Languages (Fortran, COBOL): Introduced mathematical notation and business-oriented abstractions, improving readability and usability.

    • Structured Programming (C): Introduced procedural modularity, block structures, efficient memory pointer access, and control structures.

    • Object-Oriented Languages (C++, Java): Enhanced code modularity, encapsulation, inheritance, and reusability.

    • Scripting Languages (Python, JavaScript): Optimized for rapid development, duck typing, automated memory management, and cross-platform versatility.

  • Origins and Significance of the C Language:

    • Developed in the early 1970s at Bell Labs by Dennis Ritchie.

    • Combines the speed and low-level hardware control of assembly language with the structured control flow of high-level languages.

    • Serves as the foundation for operating systems (such as UNIX and Linux), compilers, embedded systems, and modern object-oriented computing paradigms.

  • The C Translation and Build Process:

    • Source File: Human-readable source code written in files ending with .c.

    • Preprocessing: The preprocessor evaluates directives starting with # (e.g., #include, #define), expands macros, and strips comments to yield a preprocessed output file (.i file).

    • Compilation: The compiler translates preprocessed high-level C code into platform-specific assembly instructions (.s file).

    • Assembly: The assembler converts assembly code into relocatable machine object code (.o or .obj file).

    • Linking: The linker resolves symbol references, links runtime startup files along with compiled library functions, and outputs the binary executable file (.exe or .out).

    • Program Execution: The Operating System loads the executable into memory, sets up stack/heap memory, and transfers control to the entry point.


C Compilation Workflow

Basic Structure and Style of a C Program

  • Importance of Programming Style:

    • C is free-format regarding whitespace and indents; however, structured style is essential for code maintainability.

    • Manageability: Organized programs are easy to modify as codebases scale.

    • Understanding: Descriptive identifiers assist developers in identifying variable roles.

    • Readability: Consistent indentation and alignment make logic tracing and debugging faster.

    • Core Axiom: Good Style=Clear Code+Easier Maintenance+Fewer Misunderstandings\text{Good Style} = \text{Clear Code} + \text{Easier Maintenance} + \text{Fewer Misunderstandings}.

  • Comparing Unformatted vs. Formatted C Code:

    • Unformatted Code: c int main(){int age;printf("Age:");scanf("%d",&age);return 0;}         

    • Formatted Code: c int main() { int age; printf("Age: "); scanf("%d", &age); return 0; }         

  • Anatomy of a C Source File:

    1. Preprocessor Directive: Informs the preprocessor to load standard header files.

    2. main() Function: Mandatory entry point function.

    3. Variable Declarations: Allocates memory locations with designated data types.

    4. Input/Output Statements: Communicates with the user via standard input/output streams.

    5. Successful Program End: Returns an integer code (return 0;) to the OS.

  • The main() Function Execution Flow:

    • Execution always starts at main(), regardless of its physical line position in the source file.

    • The empty parentheses () signify that main() takes no parameters in its basic form.

    • Execution lifecycle: Operating System→main() entry point→Program statements run→return 0 (successful finish)\text{Operating System} \rightarrow \text{main() entry point} \rightarrow \text{Program statements run} \rightarrow \text{return 0 (successful finish)}.

  • Comments in C:

    • Notes embedded within code that are ignored by the compiler.

    • Single-line Comment: Starts with // and extends to the end of the line. c // This is a single-line comment printf("Hello");         

    • Multi-line Comment: Enclosed within /* and */, spanning across single or multiple lines. c /* This is a multi-line comment that can span multiple lines. */ printf("Hello");         

    • Best Practice: Comments should explain the intended logic or rationale of code rather than repeating obvious operational syntax.

  • Preprocessor Directives and Macro Constants:

    • Directives begin with # and are processed before compilation.

    • #include <stdio.h>: Includes the Standard Input Output library containing definitions for functions like printf() and scanf().

    • #define: Creates symbolic constants or macro replacements across source code prior to compilation. ```c

      include

      define STUD_NO "204-1234

      "

      define CONTACT "09651234567

      "

      int main() { printf(STUD_NO); printf(CONTACT); return 0; }         ```

    • Naming Convention: Symbolic constants created via #define should be written in UPPERCASE letters to distinguish them from standard variables.

C Declarations, Statements, Keywords, and Literals

  • Declarations:

    • Informs the compiler about the name, type, and properties of identifiers prior to their usage.

    • Variable Declaration: Specifies type and identifier: data_type variable_name; (e.g., int age; float price; char grade;).

    • Function Declaration (Prototype): Specifies return type, name, and parameter types: return_type name(parameters); (e.g., int add(int a, int b);).

    • Type Declaration (typedef): Creates an alias for an existing data type: typedef existing_type new_name; (e.g., typedef int Integer; Integer score = 95;).

  • C Statements:

    • An instruction that commands the computer to perform an action.

    • Statements terminate with a semicolon ;, which serves as a command terminator. c int age = 20; // declaration statement age = age + 1; // assignment statement printf("%d", age); // function call statement return 0; // return statement &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  • Keywords and Reserved Words:

    • Predefined identifiers reserved by C that possess fixed special meanings. They cannot be used as variable or function names.

    • Data Types: int, char, float, double, void

    • Decision Control: if, else, switch, case, default

    • Loops: for, while, do, continue, break

    • Program Control: return, goto, sizeof

    • Custom Types: struct, union, enum, typedef

    • Invalid Example: int return = 10; (Fails compilation because return is a reserved keyword).

  • Literals vs. Variables:

    • Variable: A named memory location whose stored value can change during program execution.

    • Literal: A fixed, explicit value written directly in code that does not change.

    • Analogy: A variable acts as a storage box, whereas a literal is the concrete content placed inside the box.

    • Categories of Literals:

      • Integer: Whole numerical values (e.g., 100, -25, 0).

      • Floating-point: Decimal or scientific notation values (e.g., 3.14, 12.5, 1.23e4).

      • Character: Single character enclosed in single quotes (e.g., 'A', ' ', '%').

      • String: Sequence of characters enclosed in double quotes (e.g., `