Posts

Showing posts with the label C++

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm

Image
What is 'Maximal Subarray Problem'(MSP)? Given an array having both positive and negative numbers, we need to find a continuous part of the array whose elements when added gives the largest possible sum. Here are few examples to help you understand the MSP better: 1. consider the array [-1, 2, -1, 3]     Now, here is a list of all the possible subarrays along with the sum of their elements: ELEMENTS SUM start index end index -1 -1 0 0 -1, 2 1 0 1 -1, 2,-1 0 0 2 -1, 2,-1, 3 3 0 3 2 2 1 1 2,-1 1 1 2 2,-1, 3 4 1 3 -1 -1 2 2 -1, 3 2 2 3 3 3 3 3     From, the above table, its obvious that the answer is [ 2,-1, 3].     All other subarrays sum up to a value that is less than 4....

Generic quicksort in C

Image
Recently I've learned how to use void pointers and function pointers in C and make functions generic as a part of MRND . So I've tried developing a generic quicksort function using it. I've implemented it in a header file so that it can be reused. Here's the gist for genericSort.h  The four arguments being sent into the genericQSort function are: void ** arr :  It's an array of pointers which holds the addresses of each element of the array that is to be sorted. int lo: The start index from where the array needs to be sorted. int hi:  The end index till where the array needs to be sorted. int (*compare)( void * , void * ) : A function pointer to the user's implementation of how to compare the sent array. It returns -1, 1 or 0. Here's a C program that makes use of the above function to sort an array of pair s . Here pair is a user-defined data-type which has two members x and y . The array is sorted based on the sum of x and y. H...

Creating own header files for statistics

Image
I was always interested in the subject of 'Probability & Statistics'. So I went ahead and created a header file to help me solve the questions present in the text book I was studying. I've developed two header files: measures.h:  It has all the functions to measure the various measures used in statistics like mean, median, variance, deviation etc. data.h:  This contains the definitions of two data-types ' univariateData ' and ' bivariateData '. These are helpful in processing data right on input and giving out details about the given data. They are immutable(can't be edited). *Note: Don't include  measures.h if data.h is already included because data.h has measures.h declared in it. The cool part is that just 2 lines of code and user input are now enough to analyze any univariate data or bivariate data and display the info! Here's an example: (source: http://www.seattlecentral.edu/qelp/sets/018/018.html ) A...

Developing a Sudoku solver

Image
I've never been great at solving Sudoku because I find it boring to sit down for an hour staring at boxes some filled and others to be filled with numbers. But I think it's an interesting game. I've always thought of developing an algorithm to solve any given Sudoku puzzle. I've had this in my mind from the very first year of my under graduation but I never gave it a serious thought. If you have seen my previous post, it's about backtracking which is a general algorithm used to solve many problems such as the classical N-Queens problem(I've solved it in that post). Yesterday I tried solving a question on the Hackerrank site which required me to use the backtracking algorithm and I was successful in devising an algorithm to solve it. I felt the question was very similar to solving a Sudoku puzzle so I thought of creating a sudoku solver and I've done it! It was simpler than what I thought it would be. All you need to know is how to use backtracking...

Beginner's guide to Solving the N-Queens problem using backtracking method

Image
This post serves as a good introduction to the backtracking method which is used widely in various types of problems to find a possible solution. Here we'll be seeing how to solve the classical N-Queens problem using backtracking method. Backtracking : The Wikipedia page  for Backtracking defines it as "a general algorithm in which, we try to incrementally build candidates to the solutions, and discard a candidate as soon as we get to know that the candidate cannot possibly be completed to a valid solution".  If that sounds complicated and confusing, don't worry! Let us understand it by running through a small example. Consider you are trapped in a haunted house with five rooms named A, B, C, D and E. Initially, you are in room A and you need to reach room E to exit the house. Below is an image of the rooms in the house. The arrows in the image indicate the directions in which you can move. For example, you can move to rooms B and C from A but, you ...

HOW TO CREATE TEXT GAMES USING C! : PART 4 (Rock, Paper, & Scissors game)

Image
This is the fourth part of the series of how to create text games using C. If you haven't seen the previous posts check them out by clicking the links below: PART 1 : Using the rand() function PART 2 : Getting started!   PART 3: Constructing a menu Coming to part 4, here we are going to create the classic Rock, Paper, & Scissors game using the knowledge from the previous three posts. If you haven't gone through the previous posts, I'd recommend you to go through them as they're very easy to understand and take hardly any time for you to complete reading them. So till now we have learned how to use the rand() function, prompting for and scanning the user's actions and also how to construct a menu for a game. Now is the time that we bring together all these small elements together and create a fun game of Rock, Paper, and Scissors(from now on I'll be calling it as RPS for simplicity). So before talking about the code, it's good to think ab...

'Hello, World!' program in 7 different languages

Image
Generally 'Hello World!' program is the first ever program that you'd do in any language. It is a rite of passage into more complex programs in any language. So here's a quick look at how to write 'Hello, World!' program in 5 major languages which are: Java C C++ C# Python SQL Ruby   Java: public class HelloWorld { public static void main ( String [ ] args ) { // Prints "Hello, World" to the terminal window. System . out . println ( "Hello, World!" ) ; } } C: # include " stdio.h " int main ( void ) { printf ( " Hello, World! " ) ; return 0 ; } C++: # include " iostream " using namespace std ; int main ( ) { cout < < " Hello, World! " ; return 0 ; } C#: public class Hello { public static void Main ( ) { System . Console . WriteLine ( " Hello, World! " ) ; } } ...

HOW TO CREATE TEXT GAMES USING C! : PART 3 (Constructing a menu)

Image
This is the third part of the series of how to create text games using C. If you haven't seen the previous posts check them out by clicking the links below: PART 1 : Using the rand() function PART 2 : Getting started!   Coming to part 3, here we are going to discuss how to create a menu for the game. If we look at any game, having a menu screen is quintessential as it provides the user with various options. Generally big games have some of the following options in their menu screen: Play : Enters you into the game Options / Setting : Generally, helps you change the video, audio and other settings Leader Board : To show a record of top scores by players Help / Instructions : Gives instructions for playing the game About : Gives details of the game such as who created the game, copyrights etc. Exit : To exit the game For example, here's a screenshot of the game Assassin's creed: So now coming to our game which we are going to develop using C,...