Trivial C, C++ Programs.

Author Archives: m1k3y

Merge Sort in C++

Been a while since I posted some code here, this one I’m posting to help a junior from NSIT out.   #include <iostream.h>     int *Arr, maxSize;   void createArr() { cout<<"\n\n Enter the maximum size of array : "; cin>>maxSize; Arr = new int[maxSize]; }   void fillArr() { cout<<"\n\n Enter the elements [...]

Queues Using Linked Lists in C++

And NO, there is no meaning to adding a special operation called ‘Create’ the queue. When you push in the first element, the queue is created. Of course I didn’t bother to check for exceptions. This is a straight off the text book program, suck on it bitches.   #include<iostream.h> #include<conio.h>   struct node { [...]

Queues Using Arrays (Static) in C++

It does what it says. A first-in first-out structure, implement in the simplest possible way. Again, no bound checks. Sanitize your inputs please. #include <iostream.h>   int *Queue, front, rear, maxSize;   void createQueue(); void push(); void pop(); void dispQueue();   int main() { int choice; while(choice != 5) { cout<<"\n\n Enter your choice :" [...]

Stacks Using Arrays (Static) in C++

This is a general implementation of the stack data structure in C++. I’ve made it a ‘menu-driven’ program, for all you menu-loving whores. Nobody cares if it is rendered unreadable. Also, I’ve skipped bound checks in this program, so remember to sanitize your inputs or you may get sucked into a vortex. Also, since I’m [...]

Fibonacci Series (Using Recursion) in C++

This is a simple 2 statement function that takes n as an argument and returns the nth Fibonacci number. Can be used to display the Fibonacci series upto the integer size limit.   #include <iostream.h> #include <conio.h>   unsigned int fibonacci (unsigned int n) { if (n <= 2) { return 1; } else { [...]

Linear Search in C++

Oh yeah, they ask us to make Linear Search TOO. This code is as simple as it gets. Sorry, but making it any simpler would make my eyes bleed. Here you go.   #include <iostream.h> #include <conio.h>   int linearSearch(int*, int, int, int);   int main() { int *sArray, mSize, i, elem, index;   cout<<"\n [...]

Binary Search in C++

Now the important thing to note here is that Binary Search only has a meaning when your input is sorted in some way, using some parameter as basis for sorting. This program implements binary search for a set of integers, sorted in ascending order. Binary search on an unsorted input makes no sense as if [...]