Fundamentals of Problem Solving and C Programming
Von Neumann Architecture
Origins: The Von Neumann Architecture was created by John von Neumann. It represents the first written description of how an electronic computer should store and process information.
Publication: It was published on June 30, 1945, as part of the ‐‐First Draft of a Report on the EDVAC (Electronic Discrete Variable Automatic Computer).‐‐
Core Components: The architecture comprises five fundamental elements:
ALU (Arithmetic Logic Unit): Performs mathematical and logical operations.
Single Shared Memory: Used for storing both programs and data.
Single Memory Bus: A shared path for data transfer.
Input/Output: Systems for interacting with the external environment.
Control Unit: Manages the computer's resources and coordinates tasks.
Legacy: This design remains the fundamental basis for all modern computers and most electronic devices.
Central Processing Unit (CPU) and Registers
Primary Components:
Control Unit
Arithmetic Logic Unit (ALU)
Registers
Memory Unit
Registers Definition: Registers are small, high-speed storage areas located within the CPU. They are used to hold data and instructions temporarily during processing.
Key Registers and Functions:
Program Counter (PC): Holds the memory address of the next instruction to be fetched from memory.
Current Instruction Register (CIR): Holds the specific instruction currently being decoded and executed.
Accumulator (ACC): Used by the ALU to hold data currently being processed as well as the results of calculations.
Memory Address Register (MAR): Holds the address of the data or instruction to be fetched from memory, or the destination address in memory for data transfer.
Memory Data Register (MDR): Stores the actual instructions or data being fetched from or written to the memory address specified in the MAR.
Communication Systems and the System Bus
System Bus: A communication system where only one device can transmit data at any given time. It consists of three specific types of buses:
Data Bus: Transfers actual data between components. This bus is bidirectional.
Address Bus: Carries the memory or I/O controller addresses to be accessed. This bus is unidirectional from the CPU to other components.
Control Bus: Transmits control signals (like read/write signals or interrupt requests) from the CPU to components and sends status signals back to the CPU. This bus is bidirectional.
Fetch-Decode-Execute Cycle
Concept: Programs are stored in memory as instructions alongside data.
Process: The processor periodically fetches instructions from the main memory, decodes what the instruction means, and subsequently executes the required operations.
Memory Hierarchy and Caches
Hierarchical Structure (from fastest/most expensive to slowest/cheapest):
Registers: Smallest capacity, highest speed, highest price, located in the CPU.
Cache: Small, fast memory located on the CPU chip or very close to it. It stores frequently accessed data.
Main Memory (DRAM): Larger and slower than cache; used for currently running programs and data.
Flash Memory / Solid State Drives / Hard Drives: Large capacity, slow speed, lowest price. Non-volatile storage for long-term data preservation.
Calculated Cache Levels:
L1 Cache: The smallest and fastest, typically on the CPU core. Often split into Instruction Cache () and Data Cache ().
L2 Cache: Larger and slightly slower than L1.
L3 Cache: The largest and slowest of the CPU caches, but still significantly faster than main memory.
Parallelism vs. Concurrency
Parallelism: Involves using multiple processing units to perform tasks simultaneously and independently.
Concurrency: Refers to managing multiple tasks on even a single CPU core by switching between tasks before they are necessarily completed.
Design: Programs can be parallel, concurrent, both, or neither.
Number Systems in Computing
Decimal (Base-10): Standard system for everyday calculations using digits through .
Binary (Base-2): The foundation of digital systems using digits and .
Octal (Base-8): Uses digits through to simplify binary representation.
Hexadecimal (Base-16): Uses digits and letters to represent values . Essential for simplifying large binary data.
Duodecimal (Base-12): A less common system with specific niche applications.
Number System Conversions
Decimal to Binary ( to Binary):
Integer Part ():
(Remainder )
(Remainder )
(Remainder )
(Remainder )
Read remainders bottom-up: .
Fractional Part ():
(Integer )
(Integer )
Read integers top-down: .
Result: .
Binary to Decimal ( to Decimal):
Calculations:
Sum: .
Decimal to Octal ( to Octal):
Integer Part ():
(Remainder )
(Remainder )
Result: .
Fractional Part ():
(Integer )
Result: .
Final: .
Octal to Decimal ( to Decimal):
Calculations:
Sum: .
Decimal to Hexadecimal ( to Hexadecimal):
Integer Part (): Remainder (which is ).
Fractional Part (): (Integer ).
Result: .
Hexadecimal to Decimal ( to Decimal):
Calculations: .
Hexadecimal to Binary ():
Result: .
Binary to Hexadecimal ():
Group by 4 bits from right: (), (), ().
Result: .
Binary to Octal ():
Group by 3 bits: (), (), ().
Result: .
Octal to Binary ():
, , .
Result: .
Practice Problems: Number Systems
Easy Level:
Convert to Decimal.
Convert to Decimal.
Convert to Decimal.
Convert to Binary.
Convert to Octal.
Medium Level:
Convert to Decimal.
Convert to Binary.
Convert to Octal.
Convert to Hexadecimal.
Convert to Binary.
Advanced Level:
Convert to Octal.
Convert to Binary.
Convert to Hexadecimal.
Convert to Binary.
Convert to Hexadecimal.
Convert to Octal.
Convert to Decimal.
Convert to Binary, Octal, and Hexadecimal.
Convert to Decimal.
Types of Programming Languages
Classification by Abstraction:
High-Level Languages: Human-readable, portable, and requires a compiler or interpreter (e.g., Python, Java, JavaScript).
Low-Level Languages: Close to machine code, provides high hardware control but is difficult to write (e.g., Machine code).
Assembly Languages: Uses symbolic instructions. An assembler converts it to machine code.
Classification by Paradigm:
Procedural: Follows step-by-step commands (e.g., Java, Pascal, BASIC).
Functional: Based on mathematical functions (e.g., Scala, Haskell, F#, Lisp, Erlang).
Object-Oriented (OOP): Programs are collections of objects (e.g., Python, Ruby, C++, Java, C#, Swift, Kotlin, Objective-C).
Scripting: Interpreted at runtime without pre-compilation (e.g., Python, Perl, PHP, Bash, JavaScript, PowerShell).
Logic: Uses logical statements and relationships (e.g., Prolog, Alma-0, Absys, Mercury).
Language Processors
Compiler: Reads the entire high-level source program as a whole and translates it into machine language in one go (e.g., C, C++, C#).
Interpreter: Translates and executes one statement at a time immediately before moving to the next line.
Assembler: Translates assembly language into machine code.
Structure of a C Program
Six Basic Sections:
Documentation: Contains descriptions, programmer name, and creation date using comments (
//or/* ... */).Preprocessor Section: Includes header files (e.g.,
#include <stdio.h>).Definition: Defines symbolic constants (e.g.,
#define PI 3.14).Global Declaration: Contains global variables, static variables, and function prototypes accessible throughout the program (e.g.,
int num = 18;).Main() Function: The mandatory starting point for every C program, enclosed in braces
{ }.Sub Programs: Where user-defined functions are implemented.
C Preprocessors and Macros
Purpose: Modifies source code before compilation begins.
Macro Directives:
Constant Macros: Replaces identifiers with values (e.g.,
#define LIMIT 5).Macros with Arguments: Function-like macros that expand into reusable snippets without function call overhead (e.g.,
#define AREA(l, b) (l * b)).
Data Types in C
Primary / Basic Data Types:
Integer (
int): Represents decimal integers; further classified as signed or unsigned.Character (
char): Represents characters; size is bits.Real: Represents decimal or integer numbers on the number line (Float, Double).
Void: Represents the absence of type.
Pointer: Holds the memory address of another variable using operators
&and*.
User-Defined Data Types:
Typedef: Creates an identifier for an existing type (e.g.,
typedef int numbers;).Enumerated (
enum): Assigned a value from a specific set of values.
Derived Data Types:
Array: A collection of homogeneous data types stored in contiguous memory.
Structure (
struct): A collection of non-homogeneous (different) data types.Union: Similar to structures but shares the same memory space for all members.
Variables and Constants in C
Variable Features: Named memory locations, stored in RAM, whose value can change at runtime. Syntax:
data_type variable_name = value;.Variable Types:
Local: Declared inside a function.
Global: Declared outside functions.
Static: Retains its value between multiple function calls.
External (
extern): Shared across multiple source files.Automatic: Default for local variables within a block.
Constants: Fixed values that cannot be modified after definition.
Literal Constants: Direct values like , , or
"Hello".Const Keyword: Variables defined with
const(e.g.,const int MAX = 100;).Preprocessor Constants: Defined using
#define PI 3.14159.
Operators and Bitwise Logic
Operator Groups: Arithmetic, Assignment, Comparison, Logical, and Bitwise.
Order of Operations (Highest to Lowest):
()- Parentheses*,/,%- Multiplicative+,-- Additive>,<,>=,<=- Comparison==,!=- Equality&&- Logical AND||- Logical OR=- Assignment
Bitwise Operators:
&(Binary AND): Copies a bit to the result if it exists in both operands. (e.g., ).|(Binary OR): Copies a bit if it exists in either operand. (e.g., ).^(Binary XOR): Copies a bit if it is set in one operand but not both. (e.g., ).~(Binary One's Complement): Unary operator that flips bits. (e.g.,~60yields bits representing ).<<(Binary Left Shift): Moves bits left by a specified number of positions (e.g., ).>>(Binary Right Shift): Moves bits right (e.g., ).
Storage Classes in C
auto:
Scope: Local.
Lifetime: Block exit.
Default Value: Garbage.
Storage: Stack memory.
register:
Scope: Local.
Lifetime: Block exit.
Storage: CPU register (for speed).
Note: Cannot use the
&address operator on register variables.
static:
Scope: Local to block or global to file.
Lifetime: Entire program execution.
Default Value: Zero.
Storage: Data segment.
extern:
Scope: Global (cross-file).
Lifetime: Entire program execution.
Default Value: Zero.
Storage: Data segment.