# TagAlgorithms

## A naïve way to find the winner at the ‘Five in a row’ board game – C Source Code

Five in a row is a relatively easy game for two players. The aim is to get five of one's pieces in a row (horizontal, vertical, or diagonal). Below is a simple algorithm in C to find the winner.

## Convert a char array of HEX to ASCII in C

This function solves the case when we have a message which is represented as sequence of bytes in hex format in a string and we want to convert it back.

## All-around function to find mins/maxs, in order, distinct elements in array

It this post you can find a function in C which is a able to find X minimum or maximum elements in a given array, order the output either ascended or descended and get either distinct elements or duplications. Of course this function is far away from optimal solutions as it is better to use specific algorithms depending on your needs, however for fast experimentation it is useful. You can use...

## Find masked pattern in 1D array – C source code

The following example finds the first occurrence of pattern in a given array and returns the address of that position. Moreover it takes into account a mask, which is helpful for inconsistent pattern formats like : 2,3,X,X,3,1 where X should be ignored. Below you will find the source code and a working sample.

## A simple generic C Stack for static or dynamic memory usage

Following the  previous post about Queues, In this post i publish a simple modular stack written in C which can be used either by assigning a static space for the elements of a dynamic one (malloc etc). In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes...

## A simple generic C Queue for static or dynamic memory usage

In this post i publish a simple modular queue written in C which can be used either by assigning a static space for the elements of a dynamic one (malloc etc). Queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order. The only requirement is that the memory space which will hold the queue items have to be allocated in prior. Below is...

## The problem of “Beautiful Arrays” with C Source code

An array is called beautiful if for every pair of numbers , , (), there exists an  such that . Note that can be equal to or too. Input First line of the input contains an integer T denoting the number of test cases. T test cases follow. First line of each test case contains an integer n denoting number of elements in a. Next line contains n space separated integers denoting the array a. Output...

## How to left-trim a C string

In several cases we would like to remove any leading white-spaces in a C-string. The following code snippet does this in the simplest possible way. #include <string.h> #include <stdio.h> #include <stdlib.h> void str_ltrim_whsp(char* arg) { if (arg == NULL) return; if (strlen(arg) == 0) return; int nhead = 0; while (arg[nhead++] == ' '); nhead--; memmove(arg, &arg[nhead]...

## Merge C strings to a new one

This code snippet provides an essential functionality to merge c strings. Using the build-in c-string function strcpy we are able to merge a lot of different strings to one without worrying about the available memory space. This function requires the Linux Library va_args  char * str_merge(int count, ...) { va_list ap; int i; char *va_tmp; int len = 1; va_start(ap...

## Memory Management Algorithms for MMU-Less Systems

There are several different approaches for memory management that can solve the fragmentation problem on MMU-less embedded systems. Each algorithm is classified according to the way that it finds a free block of the most appropriate size. There are five categories extendedly analyzed in M. Masmano el al. [1], Sun et al. [2] and P. R. Wilson [10] works: Sequential Fit, Segregated Free Lists...