Posts

Showing posts with the label C language program

Water Jug Problem using Breath First Search

Image
Water Jug Problem AI Problem Statement: In a Water Jug Problem you are given two jugs, one 4-gallon and one 3-gallon, a pump which has unlimited water which you can use to fill the jug, and the ground on which water may be poured. Neither jug has any measuring markings on it. How can you get exactly 2 gallons of water in the 4-gallon jug? This problem can be solve using multiple techniques of Artificial Intelligence (AI). Here we are going to solve the Water Jug Problem using the Breath First Search (BFS) Algorithm. Here is the code for the current problem statement.

Water Jug Problem using Depth First Search

Image
Problem Statement: In a Water Jug Problem you are given two jugs, one 4-gallon and one 3-gallon, a pump which has unlimited water which you can use to fill the jug, and the ground on which water may be poured. Neither jug has any measuring markings on it. How can you get exactly 2 gallons of water in the 4-gallon jug? This problem can be solve using multiple techniques of Artificial Intelligence (AI). Here we are going to solve the Water Jug Problem using the Depth First Search (DFS) Algorithm. To solve the problem with Breath First Search (BFS), check it here. Here is the code for the current problem statement. Firstly, let include all the header files. Now we have to define a structure for the nodes which we are going to use it for the further process. Below code snippet of code is for the same. #include<stdio.h> #include<conio.h> struct node { int x, y; struct node *next; }*root, *left, *right; Now we have to check weather the node whi...

Merge Sort

Image
Merge Sort in C In  computer science ,  merge sort  (also commonly spelled  mergesort ) is an efficient, general-purpose,  comparison-based   sorting algorithm . Most implementations produce a  stable sort , which means that the implementation preserves the input order of  equal  elements in the sorted output. Mergesort is a  divide and conquer algorithm  that was invented by  John von Neumann  in 1945. Algorithm Conceptually, a merge sort works as follows: Divide the unsorted list into  n  sublists, each containing 1 element (a list of 1 element is considered sorted). Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.

DFS(Depth First Search) & BFS(Breath First Search) in C

Image
DFS v/s BFS Depth First Search Depth First Search for a graph is similar to  Depth First Traversal of a tree . The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. Pseudocode: Input : A graph  G  and a vertex  v  of G Output : All vertices reachable from  v  labeled as discovered A recursive implementation of DFS:

Heap Sort

Heap Sort in C Heap-sort can be thought of as an improved selection sort:  like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a  heap  data structure rather than a linear-time search to find the maximum. The heapsort algorithm can be divided into two parts. In the first step, a heap is built out of the data. The heap is often placed in an array with the layout of a complete  binary tree . The complete binary tree maps the binary tree structure into the array indices; each array index represents a node; the index of the node's parent, left child branch, or right child branch are simple expressions. For a zero-based array, the root node is stored at index 0; if  i  is the index of the current node, then 

C program of Bubble Sort

Image
Bubble Sort Algorithm Bubble sort , sometimes referred to as  sinking sort , is a simple  sorting algorithm  that repeatedly steps through the list to be sorted, compares each pair of adjacent items and  swaps  them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a  comparison sort , is named for the way smaller or larger elements "bubble" to the top of the list.

Find Days and Months

Image
C program to find the number of days and months on the basis of given input In this post, I shared a C program to find the total number of months and the remaining days on the basis of the given input from the user.

C program for simple Arithmetic Operation

Image
C program to find the Addition, Subtraction, Division, Multiplication and Modulo of two numbers. In this post, I shared a code to find the simple arithmetic operation results for the given two numbers.

Implementation of Binary Search Tree in C

Image
Binary Search Tree A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties − The left sub-tree of a node has a key less than or equal to its parent node's key. The right sub-tree of a node has a key greater than to its parent node's key. Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree.

Implementation of Circular Linked List in C

Image
Circular Linked List Before going towards the circular linked list, I hope that you have the knowledge about the Doubly Linked List. If not, then click   here . Circular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required.

C program to implement Double Linked List in C

Image
Doubly Linked List Before going towards the doubly linked list, I hope that you have the knowledge about the Singly Linked List. If not, then click here . Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list.

Implementation of Queue using Linked List in C

Image
Queue With Linked List We had seen how to implement simple queue using array. If you had not seen, just go through it once  Queue with Array Now, let us see the implementation of queue using linked list.

Implementation of Stack using Linked List in C

Image
Stack With Linked List We had seen how to implement simple stack using array. If you had not seen, just go through it once Stack with Array Now, let us see the implementation of stack using linked list.

C program to implement the Circular Queue

Image
Circular Queue with its Operation Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. Graphical representation of a circular queue is as follows... Implementation of Circular Queue To implement a circular queue data structure using array, we first perform the following steps before we implement actual operations. Step 1 :  Include all the  header files  which are used in the program and define a constant  'SIZE'  with specific value. Step 2:  Declare all  user defined functions  used in circular queue implementation. Step 3:  Create a one dimensional array with above defined SIZE ( int cQueue[SIZE] ) Step 4:  Define two integer variables  'front'  and ' rear ' and initialize both with  '-1' . ( int front = -1, rear = -1 ) Step 5:  Implement...

C program to implement Stack

Image
C program to do operations on Stack using Array Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Mainly the following three basic operations are performed in the stack: Push:  Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop:  Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

C program with the use of strlen() function.

Image
Use of strlen() Function. In this post, I had shared a program with the use of strlen() function. strlen() is a string function which gives the length of the string in form of output. Let us check the code :)

C program to count the words in a line.

Image
Count the total number of words in a line. In this post, I had shared a C program that will count the number of words in the line you had inserted. Let us observe the code :) #include <stdio.h> #include <conio.h> void main() { char s[50],ch; int i,c=0; clrscr(); printf("Enter the string: "); for(i=0;ch!='\n';i++) { ch=getchar(); s[i]=ch; } s[i]='\0'; for(i=0;s[i]!='\0';i++) { if(s[i]==' ') { c++; while(s[i]==' ') i++; } } c++; printf("\nTotal words: %d",c); getch(); } Happy Coding :)

C program to print the given numbers in Ascending Order

Image
C program to print numbers in Ascending Order In this post, i will share a C program to to print the given numbers in ascending order using simple for loop. Let us check out the code :)

C program to Add two 3X3 matrix

Image
Addition of two 3X3 matrix In this post i will share a c program with you that helps you to add 2 3X3 matrix. So lets us check out the code :)

C program to print the given number in reverse order

Image
Printing the number in Reverse Order In this post, i had shared a c program that will help you guys to print the inputted number into reverse order. For E.g. If the number is 123456789               Output will be:    987654321 Let us check out the code: