Posts

Showing posts with the label Tutorials

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....

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 use 'HackeRank' to develop your skills

Image
So before going any further, what is HackeRank ? HackerRank is a place where programmers from all over the world come together to solve problems in a wide range of Computer Science domains such as algorithms, machine learning, or artificial intelligence etc.     It is one among the many other online coding practice websites which will help you to develop your coding skills. You can devise an algorithm, code it and check whether it's right or wrong at a go! The best part is that you need not provide any test cases, test cases are provided and checked by the site. All you need to do is to just submit your solution and you'd know whether your solution works or not. Here's a step by step procedure explaining how to use HackerRank: 1. Creating an account : Creating an account is straightforward, on the main page you have three fields to fill in your name, email, and password. After filling those details and clicking the 'sign up and start coding' button, yo...

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...

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,...

HOW TO CREATE TEXT GAMES USING C! : PART 2 (Getting started!)

Image
So, in the previous post I've described a simple program to generate a random number within a range ( 1 to 6 for a dice) .  Now, any type or genre of a game requires actions of the player. So in this part, I bring in another basic element of a game into the code that is actions of the player So how do we make a player act ? The answer is to simply alert him with messages ( as there's no way to reach out to his throat and demand him ) to act using the console output functions such as printf()  and take the actions he performed using the console input functions such as scanf() . So, now using the  printf() and  scanf()  I've constructed a simple guess game! //let's create a number guessing game when a dice is rolled #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int number,guess; srand(time(NULL)); //randomly generate number number = (rand()%6)+1; ...