Skip to main content

C++

Implementation of Cyclic Redundancy Check Algorithm in C++

Cyclic Redundancy Check (CRC) is one of the most widely used error-detection techniques in data communications. The sender treats the data frame as a binary number, appends zeros equal to one less than the generator polynomial length, and then divides the extended frame by the generator using XOR (modulo-2) division. The remainder — called the CRC bits — is appended to the original frame before transmission. At the receiver, the same division is performed on the received frame. If the remainder is zero, the frame arrived without errors; a non-zero remainder indicates corruption. This C++ program demonstrates both the sender-side CRC generation and the receiver-side error check for a user-supplied frame and generator.

Implementation of Dijkstra Algorithm in C++

Dijkstra's Algorithm is a classic greedy algorithm for finding the shortest path from a single source vertex to all other vertices in a weighted graph with non-negative edge weights. It maintains a set of visited vertices and a distance array. At each step it selects the unvisited vertex with the smallest known distance, marks it visited, and relaxes (updates) the distances of its neighbours. This process repeats until all vertices have been visited. This C++ program reads a cost matrix from the user (entering -1 for absent edges and 0 for self-loops), runs Dijkstra's algorithm from a user-specified source vertex, and prints the shortest distance and parent vertex for every node.

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

This is an outdated post! Please click here to see latest version of Implementation of Distance Vector Routing (DVR) Algorithm in C++ The Distance Vector Routing (DVR) Algorithm is a fundamental routing algorithm used in computer networks to determine the shortest path between nodes. It is based on the Bellman-Ford algorithm and operates by sharing routing tables among directly connected nodes to update their knowledge about the shortest paths. In this blog post, we will discuss the implementation of the DVR algorithm in C++, go through the code step by step, and explain its output.

C++ Program to Copy Text from One File to Another

File I/O is a fundamental skill in systems programming. This C++ program demonstrates how to copy the contents of one text file to another using standard C file functions: fopen(), fgetc(), fputc(), feof(), and fclose(). The program opens the source file in read mode and the destination file in write mode, then reads characters one at a time from the source and writes each character to the destination until the end of the source file is reached. This approach is a classic example of character-level file copying — simple, portable, and easy to trace. It also shows basic error handling: if the source file cannot be opened (e.g., path does not exist), the program reports an error instead of proceeding.

Implementation of Hamming Code in C++

The Hamming Code is an error-detection and error-correction technique developed by Richard Hamming. It introduces redundancy bits (also called parity bits) into a data frame at specific positions that are powers of 2 (positions 1, 2, 4, 8, …). Each redundancy bit covers a set of data bits determined by the binary representation of their positions. At the receiver's end, the syndrome bits are recalculated and XOR-compared with the received parity bits — a non-zero result identifies the exact bit position where an error occurred. This C++ program takes a data frame and the number of redundancy bits as input, inserts parity bits at the appropriate positions, simulates a single-bit error at a user-specified location, recalculates the syndrome, and reports the error position.

Implementation of K-Means Algorithm in C++

The K-Means algorithm is one of the most widely used unsupervised machine learning algorithms. It partitions a dataset into K clusters such that each data point belongs to the cluster whose mean (centroid) it is closest to. Unlike KNN, K-Means does not use labelled data — it discovers natural groupings entirely on its own through iteration. In this post we walk through a well-commented C++ implementation of K-Means that clusters 10 integer values into 2 groups. We cover the algorithm, explain each line of code, trace through the iterative process, and analyse the output in detail. What is the K-Means Algorithm? K-Means works by alternating between two steps until the cluster centres stop changing: Assignment step: Assign each data point to the cluster whose mean it is closest to. Update step: Recalculate the mean of each cluster based on its current members. The algorithm converges when the means no longer change between iterations — meaning the clusters have stabilised. In this implementation: K = 2 (two clusters) Distance metric: absolute difference (1D Manhattan distance) Dataset: 10 integers entered by the user Initial means: provided by the user

Implementation of Nearest Neighbour Algorithm in C++

The Nearest Neighbour Algorithm is one of the foundational concepts in Data Warehousing and Mining (DWM). It is used to analyse a graph or dataset and classify its nodes into two groups based on a given threshold distance. A node is placed in the close neighbourhood (K1) if it is within the threshold, and in the far neighbourhood (K2) if it exceeds it. In this post we walk through a well-commented C++ implementation that reads an adjacency matrix of 5 vertices, accepts a starting vertex and a distance threshold, and partitions all connected vertices into the two neighbourhood sets K1 and K2. What is the Nearest Neighbour Algorithm? Given a graph represented as an adjacency matrix where adjMatrix[i][j] stores the distance (edge weight) between vertex i and vertex j, the algorithm works as follows for a chosen source vertex s: Read the distance from s to every other vertex from the adjacency matrix row corresponding to s. For each vertex v: if the distance is greater than 0 (there is an edge) and less than or equal to the threshold, add v to K1. If the distance exceeds the threshold, add v to K2. Also print all direct connections along with their distances. A value of 0 on the diagonal represents the distance from a vertex to itself (no self-loops). A large value like 99 is typically used to represent no direct connection (infinity).

Implementation of K-Nearest Neighbors (KNN) Algorithm in C++

The K-Nearest Neighbors (KNN) algorithm is one of the simplest and most intuitive supervised machine learning algorithms. It classifies a new data point based on the majority class among its K closest neighbors in the training dataset. There is no explicit training phase — the algorithm memorizes the entire dataset and makes decisions at prediction time, which is why it is also called a lazy learner. In this post, we will walk through a C++ implementation of the KNN algorithm that classifies a person's height as short, medium, or tall using a small training set of 12 records. We cover the algorithm step by step and explain the output in detail. What is the KNN Algorithm? KNN works on a very simple principle: similar things exist in close proximity. Given an unlabeled data point, the algorithm: Calculates the distance between the new point and every point in the training dataset. Sorts all training points by distance (ascending). Picks the K nearest points (the threshold). Assigns the class that appears most frequently among those K neighbors. This program uses a single feature — height — and the distance metric is the simple absolute difference (Manhattan distance in 1D). Gender is stored in the dataset but is not used as a feature for classification in this implementation.

Implementation of Apriori Algorithm in C++

Apriori is a cornerstone algorithm in association rule mining, widely used in market basket analysis (e.g., identifying which items are frequently bought together in stores). The algorithm’s power lies in its ability to uncover hidden patterns in large transactional datasets. What Does the Apriori Algorithm Do? The Apriori algorithm is used for discovering frequent itemsets—groups of items that appear together in a dataset with frequency above a specified threshold (called minimum support). These itemsets form the basis for generating association rules—for example, “if someone buys bread and butter, there’s a good chance they’ll also buy jam.”

Illustrating Working of FIFO Page Replacement Algorithm in C++

In this post, we implement the FIFO (First In, First Out) Page Replacement Algorithm in C++. When the OS needs to load a new page into memory but all frames are occupied, FIFO evicts the page that has been in memory the longest — the one that arrived first. It is one of the simplest page replacement strategies and serves as a baseline for comparing more sophisticated algorithms. What is FIFO Page Replacement? Physical memory is divided into frames. When a process references a page not currently in a frame (a page fault), it must be loaded. If all frames are full, an existing page must be evicted. FIFO chooses the oldest resident page for eviction, regardless of how frequently it has been used. Page Hit — Referenced page is already in a frame. No disk I/O needed. Page Fault (Miss) — Referenced page is not in any frame. Must load from disk. FIFO Queue — Tracks the order in which pages were loaded. Front = oldest; back = newest. A notable weakness of FIFO is Bélady’s Anomaly — adding more frames can sometimes cause more page faults, counter-intuitively.