Tag Archives: C++

Illustrating Binary Countdown Protocol with C++ Program

The Binary Countdown Protocol is a contention-resolution MAC (Medium Access Control) protocol used on shared broadcast channels. When multiple stations want to transmit simultaneously, each station broadcasts its address as a binary number, bit by bit from the most significant bit (MSB) downward. Stations with a 0 bit at a position where another station has a 1 bit drop out of the contention. The station whose complete binary address survives the entire comparison wins the channel and transmits its frame. This guarantees that the station with the highest binary address always wins each contention round.

This C++ program simulates the Binary Countdown Protocol. Each frame is treated as an 8-bit binary number. The program converts each frame to its decimal equivalent (which represents the station’s binary address), then announces the frames in descending priority order — highest decimal value first — as they would be granted channel access.

Continue reading Illustrating Binary Countdown Protocol with C++ Program

Implementation of Distance Vector Routing (DVR) Algorithm in C++

Distance Vector Routing (DVR) is a decentralised routing protocol where each router maintains a table containing its estimated shortest distance to every other router in the network. Routers exchange these distance vectors with their neighbours, and each router updates its own table whenever a shorter path is discovered. The algorithm continues iterating until no table changes occur — a state known as convergence. DVR is the conceptual basis for real-world protocols such as RIP (Routing Information Protocol).

This C++ program simulates DVR on a user-defined network. The user specifies the number of nodes and the distances between directly connected node pairs. The program iteratively updates routing tables until convergence and then prints each node’s final routing table showing the shortest distance and the next-hop node to reach every destination.

Continue reading Implementation of Distance Vector Routing (DVR) Algorithm in C++

Demonstrating Deadlock with Resource Allocation

Deadlock is a situation in a distributed or multi-process system where a set of processes are permanently blocked, each waiting for a resource that another process in the set holds. Detecting which processes are causing a deadlock is a critical operating system responsibility.

This C program implements a deadlock detection algorithm using a resource allocation graph. It uses a claim matrix (maximum resource needs), an allocation matrix (currently held resources), and availability vectors to identify processes that are involved in a deadlock.

Continue reading Demonstrating Deadlock with Resource Allocation

C++ Implementation of Substitution Cipher

This simple C++ implementation of a substitution cipher—specifically the Caesar cipher—demonstrates how basic cryptographic techniques can be used with file handling.

The Caesar cipher is one of the oldest and simplest substitution cipher techniques. It works by shifting the letters in a message by a fixed number of positions in the alphabet. Unlike transposition ciphers, which rearrange character positions, substitution ciphers replace characters with others based on a defined scheme.

This blog post demonstrates a C++ implementation that reads a message from a file, performs Caesar cipher encryption or decryption, and writes the result to another file.


How It Works

  1. Input and Output Files:
    • Input is read from a file named anip.txt.
    • Output is written to a file named anop.txt.
  2. User Choices:
    • You can choose between encryption and decryption.
    • Provide a key (shift amount), e.g., 2.
  3. Caesar Cipher Logic:
    • For encryption, each letter is shifted forward in the alphabet by the key.
    • For decryption, each letter is shifted backward by the key.
    • Non-alphabet characters are left unchanged.
Continue reading C++ Implementation of Substitution Cipher

Implementation of Bottom-Up (Shift-Reduce) Parsing in C++

Bottom-up parsing is a strategy used by compilers to analyse source code by building the parse tree from the leaves (terminal symbols) up to the root (start symbol). The most common bottom-up technique is shift-reduce parsing, which uses a stack and a set of production rules. At each step the parser either shifts the next input symbol onto the stack, or reduces the top of the stack by replacing a substring that matches the right-hand side of a production rule with the corresponding left-hand side symbol.

This C++ program implements a simple shift-reduce parser. It reads a set of production rules and an input string from the user, then processes the string step by step, displaying the stack contents, remaining input, and the action taken (Shifted or Reduced) at every stage.

Continue reading Implementation of Bottom-Up (Shift-Reduce) Parsing in C++

Implementing Lexical Analyser in C++

Lexical analysis (also called scanning or tokenisation) is the very first phase of a compiler. The lexical analyser reads the raw source text character by character and groups characters into meaningful units called tokens. Each token belongs to a category: an identifier (variable or function name), a literal (numeric constant), or a terminal (operator or punctuation symbol found in a predefined database file).

This C++ program reads an expression string (terminated by $) from the user, classifies each character, and builds a Uniform Symbol Table (UST) that records every token together with its type (LIT, IDN, or TER) and a pointer (sequential index within its type). The results are written to four files and then displayed on the console.

Required file: Before running the program you must create D:andb.txt containing all operator/punctuation characters that should be recognised as terminals (e.g. =+-*/()/), one or more per line.

Continue reading Implementing Lexical Analyser in C++

Implementing Code Generator in C++

Code generation is the final back-end phase of a compiler. It takes the intermediate representation (IR) of the program — typically a sequence of three-address instructions — and translates each one into the equivalent target machine instructions. For a register-based architecture like the 8086, this means emitting MOV, ADD, SUB, MUL, DIV, and assignment instructions that use the AX and BX registers.

This C++ program reads three-address IR instructions from an input file (AIP.TXT), generates the corresponding 8086-style assembly code, and writes it to an output file (ANOP.TXT). Each IR instruction has the form:

operator  operand1  operand2  result
Continue reading Implementing Code Generator in C++