Monday, September 14, 2020

C++ Program for Preorder, Inorder, PostOrder traversal in Binary Tree

#include <iostream>

using namespace std;


struct Node

{

int data;

Node *left;

Node *right;

};

Node *root=NULL;


Node* createNode(int value)

{

Node *newnode = new Node;

newnode->data = value;

newnode->left = NULL;

newnode->right = NULL;

return newnode;

}


void inOrder(Node *node)

{

if (node == NULL)

return;

else

{

inOrder(node->left);

cout<<node->data<<" ";

inOrder(node->right);

}

}


void preOrder(Node *node)

{

if (node == NULL)

return;

else

{

cout<<node->data<<" ";

preOrder(node->left);

preOrder(node->right);

}

}


void postOrder(Node *node)

{

if (node == NULL)

return;

else

{

postOrder(node->left);

postOrder(node->right);

cout<<node->data<<" ";

}

}


int main()

{

root = createNode(20);

root->left = createNode(100);

root->right = createNode(3);

root->left->left = createNode(50);

root->left->right = createNode(15);

root->left->left->left = createNode(222);

root->right->left = createNode(250);

root->right->right = createNode(35);

cout<<"\n Inorder : \n";

inOrder(root);

cout<<"\n Preorder: \n";

preOrder(root);

cout<<"\n Postorder: \n";

postOrder(root);

return 0;

} 


Output: 


Thursday, April 23, 2020

Arrays and basic functions associated with Array in JavaScript

About Arrays in JavaScript:

An array is a list of elements which is to store a large amount of element under a single name. The length of the array in JavaScript is not fixed.

=> Let's define an array with numbers stored form 1 to 5.

let number = [1, 2, 3, 4, 5];

=> You can define an array with string elements.

let animal = ["dog", "Cat", "monkey", "tiger"];

=> We can also define an array with mixed elements.

let data  = [1, 2, "hello", 4];

How to access any index element in JavaScript:
For example, we want to access the second element of the data array;
we will use 

data[1]; 

because of indexing in javascript array start from 0.


Now let's perform some operation on arrays by using build-in methods;

1. Length of the array:

' let numbers = [1, 2, 3, 4];
numbers.length; 
// 4





2. Map function to loop over the array and perform an action on each element in this case double all element and store it in the new array:

let numbers = [1, 2, 3, 4];

let doubleNumbers =  numbers.map(function(num) {


    return 2 * num;

});

console.log(doubleNumbers);

// [2. 4. 6. 8]

So, in the above map function, we are performing the anonymous function which is doubling all the element and returning it to doubleNumbers.


3. Adding a new element at the end of the array (push function):

let numbers = [1, 2. 3. 4];/div>


numbers.push(5);

console.log(numbers);

// [1, 2, 3, 4, 5];


4. Remove element from the end of Array (pop function):

let numbers = [1, 2, 3, 4];

numbers.pop();

console.log(numbers);

// [1, 2, 3]
















Wednesday, April 22, 2020

How to create Function in JavaScript

What are the Functions?
The function is a set of statement that perform some particular task.

How to use Functions in JavaScript?
For that first, you have defined a function and then call it to use it.

Function definition:
The function definition is done by using the function keyword that the name of the function and then with parenthesis with the number of parameters separated by commas, then curly braces including the code to be executed.

For Example: Let's define a function which return the sum of two numbers passed to it.

function add(a, b) 
{ 
 return a + b;
}

So, above is the function which on call will return the sum of a and b.
Let's break down each line of the function.

So the first line is funciton add(a, b)

The a and b are not compulsory so they are optional.
then there are set of parenthesis in which the code is written here it says

return a + b;

which mean add a and b and return it to the line which called the function.

Calling a Function:

The function is called by using the name of the function with parenthesis including parameters in between the parenthesis.

This is how you call a function;

add(10, 29);

You can also define a function without a name and that function is known as an anonymous function.

The syntax is almost the same.

const add = function(a, b) 
{ 
 return a + b;
}

So here the above function is doing the same work and it doesn't have any name and we are assigning it to add.

Anonymous functions will be used a lot when you do JavaScript programming.

Here the below is the screenshot from chrome javascript console which you can open by pressing F12 when you are in chrome.









Friday, March 13, 2020

Program of Stack using Array in C++

// Stack With array

#include <iostream>
using namespace std;

int stack[100];

int top = -1;

void push(int x)
{
if (top == 99)
{
cout<<"\nOverflow";
}
else
{
stack[++top] = x;
}
}

void pop()
{
if (top == -1)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<stack[top]<<" deleted";
top--;
}
}

void show()
{
if (top == -1)
{
cout<<"\n Stack is empty";
}
else
{
cout<<"\nTopmost element: " <<stack[top];
}
}

int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print top";
cout<<"\n4. Exit";
cout<<"\nEnter Your Choice : ";
cin>> ch;
if (ch == 1)
{
cout<<"\nEnter element : ";
cin>>x;
push(x);
}
else if (ch == 2)
{
pop();
}
else if (ch == 3)
{
show();
}

} while (ch != 4);

return 0;
}



Wednesday, March 11, 2020

Program of Binary Search in C++

// Binary Search

#include<iostream>
using namespace std;

int binary(int arr[], int size, int item){

    int beg = 0, last = size - 1, mid;
 
while (beg <= last){
   
    mid = (beg + last) / 2;
   
    if (arr[mid] == item)
    return mid;
else if (item > arr[mid])
beg = mid + 1;
else
last = mid - 1;
}
return -1;
}


int main(){

int size, item, output;

cout<<"\nEnter Size of Array : ";
cin>>size;
int arr[size];

cout<<"\nEnter "<<size<<" Elements : ";
for(int i=0;i<size;i++){
cin>>arr[i];
}

cout<<"\nEnter Element to Search : ";
cin>>item;
output = binary(arr,size,item);

if(output != -1)
cout<<"\nElement "<<item<<" Found at index "<<output;
else
cout<<"\nElement Not Found !";

return 0;
}


Program of Linear Search in C++

// Linear Search

#include<iostream>
using namespace std;

void linear(int arr[], int size, int item){

    for(int i = 0; i < size; i++)
{
    if(arr[i] == item)
{
    cout<<"\nElement "<<item<<" Found at index "<<i;
    return;
}
}
cout<<"\n\lement Not Found !";
}


int main(){

int size, item;

cout<<"\nEnter Size of Array : ";
cin>>size;
int arr[size];

cout<<"\nEnter "<<size<<" Elements : ";
for(int i = 0; i < size; i++){
cin>>arr[i];
}
cout<<"\nEnter Element to Search : ";
cin>>item;
linear(arr,size,item);

return 0;
}


Program of Insertion Sort in C++

// Insertion Sort
#include<iostream>
using namespace std;

void insertion(int arr[], int size)
{
int temp, j;

for(int i = 1; i <= size - 1; i++)
{
        temp=arr[i];
        j=i-1;
       
while((temp < arr[j]) && (j >= 0))
{
            arr[j+1] = arr[j];
            j = j - 1;
        }
        arr[j+1] = temp;
    }
}
int main(){

int size;

cout<<"\nEnter Size of Array : ";
cin>>size;
int arr[size];

cout<<"\nEnter "<<size<<" Elements : ";
for(int i = 0; i < size; i++){
cin>>arr[i];
}
insertion(arr,size);
cout<<"\nArray After Insertion() : ";
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}

return 0;
}


Saturday, January 12, 2019

Books Review Website

Wednesday, September 26, 2018

Collatz Conjecture Program in C++

#include<iostream>
using namespace std;

int collatz(int n)
{
if (n == 1)
{
return 0;
}
else if(n % 2 == 0)
{

return 1 + collatz(n / 2);
}
else
{
return 1 + collatz(3 * n + 1);
}
}


int main(void)
{
int num;
cout<<"\n Enter any Number : ";
cin>>num;
cout<<"\n Answer : "<<collatz(num)<<"\n";
}

Program of Quick Sort in C++


#include<iostream>
using namespace std;

void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

int partition(int arr[], int start, int end)
{
int pivot = arr[end];
int i,j;

j = start;
for (i = start; i < end; i++)
{
if (arr[i] <= pivot)
{
swap(&arr[i], &arr[j]);
j++;
}

}
swap(&arr[j], &arr[end]);
return j;
}

void quicksort(int arr[], int start, int end)
{
int part;

if (start < end)
{
part = partition(arr, start, end);

quicksort(arr, start, part - 1);
quicksort(arr, part + 1, end);
}
}

int main()
{
int size;
cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter "<<size<<" elements in the array : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

quicksort(arr, 0, size - 1);

cout<<"\n Sorted Array : ";
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}

return 0;
}


Monday, September 24, 2018

Selection Sort Program in C++

//Selection Sort Program
#include<iostream>
using namespace std;

#include<conio.h>

void SelectionSort(int arr[], int size)
{
int min,pos;
for (int i = 0; i < size - 1; i++)
{
min = arr[i];
pos = i;
//Finding the smallest element
for (int j = i + 1; j < size; j++)
{
if (arr[j] < min)
{
min = arr[j];
pos = j;
}
}
//Swaping the smallest element to its accurate position
arr[pos] = arr[i];
arr[i] = min;
}
}

int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter the Elements in the array : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

SelectionSort(arr, size);

cout<<"\n Sorted Array : ";
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}

getch();
return 0;
}


Program of Bubble Sort in C++

//Bubble Sort Program
#include<iostream>
using namespace std;
#include<conio.h>

//Function of Bubble Sort
void BubbleSort(int arr[], int size)
{
int temp;

for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;

int arr[size];

cout<<"\n Enter the elements to be sorted : ";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}

BubbleSort(arr, size);

cout<<"\n Sorted Array : ";
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}

getch();
return 0;
}


Thursday, September 20, 2018

C++ Program with output Functions to Construct Binary Search Tree from given Array and Output

//Binary Search Tree Program
#include <iostream>
using namespace std;

//Node
struct Node
{
int data;
Node* left, *right;
}*root = NULL;

// Finding position for the current node and Inserting it
void Insert_In_Tree(Node* ptr, Node* newnode)
{

if (root == NULL)
{
root = newnode;
}

else
{
if (newnode->data <= ptr->data)
{
if (ptr->left == NULL)
{
ptr->left = newnode;
}

else
{
Insert_In_Tree(ptr->left, newnode);
}
}

else
{
if(ptr->right == NULL)
{
ptr->right = newnode;

}

else
{
Insert_In_Tree(ptr->right, newnode);
}
}
}

return;
}


//Node creation for Tree
void MakeNode(int arr[], int size)
{
Node *newnode;
for (int i = 0; i < size; i++)
{
newnode = new Node;
newnode->data=arr[i];
newnode->left = newnode->right = NULL;
Insert_In_Tree(root, newnode);
}

}

//Simple Binary Tree Traversal Functions.
void preorder(Node *ptr)
{
if (ptr==NULL)
{
return;
}
else
{
cout<<ptr->data<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}

void Inorder(Node *ptr)
{
if(ptr==NULL)
{
return;
}
else
{
Inorder(ptr->left);
cout<<ptr->data<<" ";
Inorder(ptr->right);
}
}

void Post_order(Node *ptr)
{
if(ptr==NULL)
{
return;
}
else
{
Post_order(ptr->left);
Post_order(ptr->right);
cout<<ptr->data<<" ";
}
}

int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\n Enter the elements in the array : ";
for (int i = 0; i < size; i++)
cin>>arr[i];
MakeNode(arr,size);

cout<<"\n Preorder : ";
preorder(root);
cout<<"\n Inorder : ";
Inorder(root);
cout<<"\n Post Order : ";
Post_order(root);
cout<<"\n";

return 0;
}



C++ program for Constructing Binary Search Tree from a given list of element in the array.

//Binary Search Tree Program
#include <iostream>
using namespace std;

//Node
struct Node
{
int data;
Node* left, *right;
}*root = NULL;

// Finding position for the current node and Inserting it
void Insert_In_Tree(Node* ptr, Node* newnode)
{

if (root == NULL)
{
root = newnode;
}

else
{
if (newnode->data <= ptr->data)
{
if (ptr->left == NULL)
{
ptr->left = newnode;
}

else
{
Insert_In_Tree(ptr->left, newnode);
}
}

else
{
if(ptr->right == NULL)
{
ptr->right = newnode;

}

else
{
Insert_In_Tree(ptr->right, newnode);
}
}
}

return;
}


int main()
{
int size;

cout<<"\n Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\n Enter the elements in the array : ";
for (int i = 0; i < size; i++)
cin>>arr[i];
MakeNode(arr,size);

return 0;
}

For  Program with Output Just insert the Below Functions Click here


Wednesday, September 19, 2018

Merge Sort Program in C++

//MERGE SORT PROGRAM IN C++

#include<iostream>
using namespace std;

void merge(int arr[], int start, int size, int mid)
{
int s1, s2, i, j;

//Assigning size to the two array to be merged.
s1 = mid - start + 1;
s2 = size - mid;

int arr1[s1], arr2[s2];

//Initailizing those array
for (i = 0; i < s1; i++)
arr1[i] = arr[start + i];
for (j = 0; j < s2; j++)
arr2[j] = arr[mid + 1 + j];
i = 0;
j = 0;

int k = start;

//Merging them in sorted array
while (i < s1 && j < s2)
{
if (arr1[i] <= arr2[j])
{
arr[k] = arr1[i];
i++;
}
else
{
arr[k] = arr2[j];
j++;
}
k++;
}

while (i < s1)
{
arr[k] = arr1[i];
i++;
k++;
}

while (j < s2)
{
arr[k] = arr2[j];
j++;
k++;
}
}

void mergesort(int arr[],int start, int size)
{
int mid;

if (size - start + 1 < 2)
{
return;
}

else
{
mid = (start + size)/2;
//Sorting left array
mergesort(arr, start, mid);
//Sorting right array
mergesort(arr, mid + 1,size);
merge(arr, start, size, mid);
}
}

int main()
{
int arr[10], size;
cout<<"\n Enter the size of array : ";
cin>>size;
cout<<"\n Enter the element in the array : ";
for (int i = 0; i < size; i++)
cin>>arr[i];

mergesort(arr, 0, size-1);
cout<<"\n Sorted Array is : ";
for (int i = 0; i < size; i++)
cout<<arr[i]<<" ";
return 0;
}

Friday, August 31, 2018

Program to Add two Binary Numbers in C++

//Program to Add two Binary Numbers in C++ by if else
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
char a[20],b[20],c[21];
int i,j,x,y,l=0,z;
cout<<"\n Enter first Binary number : ";
cin>>a;                                                                             //use gets(a);
cout<<"\n Enter second binary number : ";
cin>>b; //Use gets(b);
for(i=0;a[i]!='\0';i++)
{
x=i;
}
for(j=0;b[j]!='\0';j++)
{
y=j;
}
    if(x!=y)
    {
    z=x-y;
    if(z>0)
{
for(i=y,j=x;i>=0,j>=(0+z);i--,j--)
{
b[j]=b[i];
}
y=x;
for(i=0;i<z;i++)
{
b[i]='0';
}
}
else
{
z=abs(z);
for(i=x,j=y;i>=0,j>=(0+z);i--,j--)
{
a[j]=a[i];
}
x=y;
for(i=0;i<z;i++)
{
a[i]='0';
}
}
}


for(i=x,j=y;i>=0,j>=0;i--,j--)
{
if(a[i]=='1'&&b[i]=='1'&&l==1)
{
c[i]='1';
l=1;
}
else
if(a[i]=='1'&&b[j]=='1'&&l==0)
{
c[i]='0';
l=1;
}
else
if(a[i]=='1'&&b[j]=='0'&&l==0)
{
c[i]='1';
l=0;
}
else
if(a[i]=='1'&&b[j]=='0'&&l==1)
{
c[i]='0';
l=1;
}
else
if(a[i]=='0'&&b[j]=='1'&&l==0)
{
c[i]='1';
l=0;
}
else
if(a[i]=='0'&&b[j]=='1'&&l==1)
{
c[i]='0';
l=1;
}
else
if(a[i]=='0'&&b[j]=='0'&&l==1)
{
c[i]='1';
l=0;
}
else
{
c[i]='0';
l=0;
}
}
if(l==1)
{
for(i=x+1;i>0;i--)
{
c[i]=c[i-1];
}
c[0]='1';
}
cout<<"\n Binary addition is : ";
for(i=0;i<=(x+1);i++)
cout<<c[i];
return 0;
}

Program to Calculate Factorial of a number using Recursion in C++

//Program to Calculate Factorial of a number using Recursion in C++
#include<iostream>
using namespace std;
int fac(long long int fact,int n)
{
if(n==1)
return fact;
else
{
fact=fact*n;
n--;

return fac(fact,n);

}
}
int main()
{
int x;
long long int f;
cout<<"\n Enter a number : ";
cin>>x;
f=fac(1,x);
cout<<"\n Fact = "<<f;
return 0;
}

Simple Student Record Management Implenation in C++

Features of Program:


  • Add
  • Display Record by Roll no Search.
  • Modify Data
  • Delete Data
  • Show all Record.
Simple Class and File Handling is used.

Click here to Download.


Program to Implement of Circular Queue using Array in C++

//Program to Implement of Circular Queue using Array in C++
#include <iostream>
#define MAX 5
using namespace std;
class Cqueue
{
    private:
        int queue[MAX];
        int front, rear;
    public:
        Cqueue()
        {
            rear = front = -1;
        }
        void insert(int item)
        {
            if ((front == 0 && rear == MAX-1) || (front == rear+1))
            {
                cout<<"Queue Overflow \n";
                return;
            }
            if (front == -1)
            {
                front = 0;
                rear = 0;
            }
            else
            {
                if (rear == MAX - 1)
                    rear = 0;
                else
                    rear = rear + 1;
            }
            queue[rear] = item ;
        }
        void del()
        {
            if (front == -1)
            {
                cout<<"Queue Underflow\n";
                return ;
            }
            cout<<"Element deleted from queue is : "<<queue[front]<<endl;
            if (front == rear)
            {
                front = -1;
                rear = -1;
            }
            else
            {
                if (front == MAX - 1)
                    front = 0;
                else
                    front = front + 1;
            }
        }
        void display()
        {
            int front_pos = front, rear_pos = rear;
            if (front == -1)
            {
                cout<<"Queue is empty\n";
                return;
            }
            cout<<"Queue elements :\n";
            if (front_pos <= rear_pos)
            {
                while (front_pos <= rear_pos)
                {
                    cout<<queue[front_pos]<<"  ";
                    front_pos++;
                }
            }
            else
            {
                while (front_pos <= MAX - 1)
                {
                    cout<<queue[front_pos]<<"  ";
                    front_pos++;
                }
                front_pos = 0;
                while (front_pos <= rear_pos)
                {
                    cout<<queue[front_pos]<<"  ";
                    front_pos++;
                }
            }
            cout<<endl;
        }
};
int main()
{
    int choice, item;
    Cqueue cq;
    do
    {
        cout<<"1.Insert\n";
        cout<<"2.Delete\n";
        cout<<"3.Display\n";
        cout<<"4.Quit\n";
        cout<<"Enter your choice : ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Input the element for insertion in queue : ";
            cin>>item;
            cq.insert(item);
    break;
case 2:
            cq.del();
    break;
        case 3:
            cq.display();
    break;
case 4:
    break;
default:
    cout<<"Wrong choice\n";
}
}
    while(choice != 4);
    return 0;
}

Dynamic Array


Dynamic Array

In dynamic array array the memory to Array will be allocated at run-time.
Here we will use pointers to do that.

Down below is the simple program to Delclare and Initialize a Dynamic Array.

#include<iostream>
using namespace std;
int main()
{
int *arr,i; //Line 1
arr=new int[5]; //Line 2
cout<<"\n Enter the elements : ";
for(i=0;i<5;i++)
{
cin>>*(arr+i); //Line3
}
cout<<"\n Array is : ";
for(i=0;i<5;i++)
{
cout<<" "<<*(arr+i);
}
delete arr;
return 0;
}

Explaintion :-
  • Line 1- Pointer of integer type is declared.
  • Line 2- Memory for storing 5 integer is allocated.
arr will contain the address of the first element in the array.
i.e, arr=&arr[0]
  • Line 3- The array is being initialised like this:-
*arr=arr[0]
*(arr+1)=arr[1] = With each increment in pointer the the next address is pointed.
.
.
*(arr+4)=arr[5]

This is how Dynamic array works.

For any doubts Comment Below.