Pages

Tuesday, June 23, 2015

Program to Implement Linear Search Algorithm

#include<iostream.h>
#include<constream.h>
void read(int a[10],int n)
{
                cout<<"reading\n";
                for(int i=0;i<n;i++)
                                cin>>a[i];
}
void display(int a[10],int n)
{
                for(int i=0;i<n;i++)
                                cout<<a[i]<<"\t";
}
void linearsearch ( int a[10],int n  )
{
                int k,flag=0;
                read(a,n);
                display(a,n);
                cout<<"\nenter an element to be search\n";
                cin>>k;
                for(int i=0;i<n;i++)
                {
                                if(a[i]==k)
                                                flag=1;
                                break;
                }
                if(flag==1)
                                cout << "\nsearching is  successful,element is found at position " << i +1<<endl;
                else
                                cout<<"searching not successful\n";
}
void main()
{
                int a[10], n;
                clrscr();
                cout<<"enter n value..\n";
                cin>>n;
                linearsearch(a,n);
                getch();
}


Saturday, June 20, 2015

How to Use Key Event Args in C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _10._1_keyboard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            label1.Text = "Alt " + (e.Alt ? "Yes" : "No") + "\n" + " Shift"
                + (e.Shift ? " Yes" : "No") + " Ctrl " + (e.Control ? " Yes" : " No")
                + "\n" + "Key Code " + e.KeyCode + "\n" + "Key Value "
                + e.KeyValue + "\n" + "Key Data " + e.KeyData + "\n";

           
        }
    }
}



C# Program to Implement Binary Search Tree using Linked List

using System;
using System.Collections.Generic;
using System.Text;
namespace TreeSort
{
    class Node
    {
        public int item;
        public Node leftc;
        public Node rightc;
        public void display()
        {
            Console.Write("[");
            Console.Write(item);
            Console.Write("]");
        }
    }
    class Tree
    {
        public Node root;
        public Tree()
        {
            root = null;
        }
        public Node ReturnRoot()
        {
            return root;
        }
        public void Insert(int id)
        {
            Node newNode = new Node();
            newNode.item = id;
            if (root == null)
                root = newNode;
            else
            {
                Node current = root;
                Node parent;
                while (true)
                {
                    parent = current;
                    if (id < current.item)
                    {
                        current = current.leftc;
                        if (current == null)
                        {
                            parent.leftc = newNode;
                            return;
                        }
                    }
                    else
                    {
                        current = current.rightc;
                        if (current == null)
                        {
                            parent.rightc = newNode;
                            return;
                        } } } }
        }
        public void Preorder(Node Root)
        {
            if (Root != null)
            {
                Console.Write(Root.item + " ");
                Preorder(Root.leftc);
                Preorder(Root.rightc);
            }
        }
        public void Inorder(Node Root)
        {
            if (Root != null)
            {
                Inorder(Root.leftc);
                Console.Write(Root.item + " ");
                Inorder(Root.rightc);
            }
        }
        public void Postorder(Node Root)
        {
            if (Root != null)
            {
                Postorder(Root.leftc);
                Postorder(Root.rightc);
                Console.Write(Root.item + " ");
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Tree theTree = new Tree();
            theTree.Insert(20);
            theTree.Insert(25);
            theTree.Insert(45);
            theTree.Insert(15);
            theTree.Insert(67);
            theTree.Insert(43);
            theTree.Insert(80);
            theTree.Insert(33);
            theTree.Insert(67);
            theTree.Insert(99);
            theTree.Insert(91);           
            Console.WriteLine("Inorder Traversal : ");
            theTree.Inorder(theTree.ReturnRoot());
            Console.WriteLine(" ");
            Console.WriteLine();
            Console.WriteLine("Preorder Traversal : ");
            theTree.Preorder(theTree.ReturnRoot());
            Console.WriteLine(" ");
            Console.WriteLine();
            Console.WriteLine("Postorder Traversal : ");
            theTree.Postorder(theTree.ReturnRoot());
            Console.WriteLine(" ");
            Console.ReadLine();
        }
    }

}

Thursday, June 18, 2015

How To Create Array of Structures

#include <iostream>
#include <string>
#include<conio.h>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} films [3];

void printmovie (movies_t movie);

int main ()
{
  string mystr;
  int n;

  for (n=0; n<3; n++)
  {
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
  }

  cout << "\nYou have entered these movies:\n";
  for (n=0; n<3; n++)
    printmovie (films[n]);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}

Wednesday, June 17, 2015

Statistical Analysis and Histogram


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Statistiacal_Analysis
{
    class data
    {
        //Function  for Mean-----------------------------------------------------------------------------------------
        public int mean(int[] arr, int num)
        {
            int me = 0;
            for (int i = 0; i < num; i++)
            {
                me = me + arr[i];
            }
            me = me / num;
            return me;
        }

        //------------------------------------------------------------------------------------------------------------------
        //Function for Median----------------------------------------------------------------------------------------
        public void median(int[] arr, int num)
        {
            int me = 0;
            // For Even Size Of The Array----------------------------------------------------------------------------
            if (num % 2 == 0)
            {
                me = ((arr[(num / 2) - 1]) + (arr[num / 2])) / 2;

                Console.WriteLine("The Median Is : " + me);
                Console.WriteLine("=======================================================");
            }
            // For Odd  Size Of The Array---------------------------------------------------------------------------
            else
            {
                me = arr[((num + 1) / 2) - 1];

                Console.WriteLine("The Median is : " + me);
                Console.WriteLine("=======================================================");
            }

        }

        //-----------------------------------------------------------------------------------------------------------------
        //Function  for Mode-------------------------------------------------------------------------------------
        public void mode(int[] array, int size)
        {

            int counter = 1;
            int max = 0;
            int Mode = array[0];
            for (int pass = 0; pass < size - 1; pass++)
            {
                if (array[pass] == array[pass + 1])
                {
                    counter++;
                    if (counter > max)
                    {
                        max = counter;
                        Mode = array[pass];
                    }
                }
                else
                    counter = 1; // reset counter.
            }
            Console.WriteLine("The mode is: " + Mode);
            Console.WriteLine("========================================================");
        }

        //------------------------------------------------------------------------------------------------------------------
        //Function for Standerd Deviation--------------------------------------------------------------------------
        public void stanDev(int[] arr, int num)
        {

            double sd = 0.0;
            double power = 2.0;
            double val = 0.0;
            int Mean = mean(arr, num);
            for (int i = 0; i < num; i++)
            {
                val = (arr[i] - Mean);
                sd = sd + (Math.Pow(val, power)) / (num - 1);
            }

            Console.WriteLine("The Standerd Deviation Is : " + (Math.Sqrt(sd)));
            Console.WriteLine("=======================================================");
        }
        //-----------------------------------------------------------------------------------------------------------------
        //Function For Printing Histogram---------------------------------------------------------------------------
        public void histogram(int[] arr, int num)
        {
            string[][] a = new string[num][];
            for (int i = 0; i < num; i++)
            {
                a[i] = new string[arr[i]];
            }
            Console.Write("Element\t\tValue\t\tHistogram\n");
            Console.WriteLine();
            for (int i = 0; i < num; i++)
            {
                Console.Write(i + "\t\t" + arr[i] + "\t\t");
                for (int j = 0; j < arr[i]; j++)
                {
                    if (j % 4 == 0 && j != 0)
                    {
                        a[i][j] += "|";
                    }
                    else
                    {
                        a[i][j] += "*";
                    }
                   
                    Console.Write(a[i][j]);
                }
                Console.WriteLine();
            }
        }
        //-----------------------------------------------------------------------------------------------------------------
    }
}
//------------------------------------------------- Class data ends here---------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Statistiacal_Analysis
    class Program
    {
        //DRIVER METHOD--------------------------------------------------------------------------------------
        static void Main(string[] args)
        {
            data z = new data();
            string s="";
            // Declarations Of  Variables -----------------------------------------------------------------------
            Random r = new Random();
            int a=0;
            // Dynamically Generated Size In  Array-------------------------------------------------------------
            Console.WriteLine("=======================================================");
            Console.WriteLine("\t\t\t      STAISTICAL DATA ANALYSIS");
            Console.WriteLine("=======================================================");
            Console.Write("Enter The Size OF The Array : ");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("-------------------------------------------------------------------------------");
            int []N=new int[a];

            // Random Numbers Assigned To The Array-----------------------------------------------------------
            for (int i = 0; i < a; i++)
            {
                N[i] = r.Next(0, 9);
                
                Console.WriteLine("The Collected Sample # "+ i +" Is : " + N[i]);                
            }
            Console.WriteLine("=======================================================");
            for (int i = 0; i < a; i++)
            {
                //Sorting The Array In Order To Calculate Mode---------------------------------------------------
                Array.Sort(N);

                //Printing The Sorted Array----------------------------------------------------------------------------
                Console.WriteLine("The Sorted Data Is : " + N[i]);               
            }
          
            //---------------------------------------------------------------------------------------------------------------

            //function call for Mean------------------------------------------------------------------------------------
            Console.WriteLine("=======================================================");
            Console.WriteLine("The Mean Is : "+z.mean(N,a));
            Console.WriteLine("=====================================================");
            //---------------------------------------------------------------------------------------------------------------

            //function call for Median----------------------------------------------------------------------------------
            z.median(N,a);            
            //--------------------------------------------------------------------------------------------------------------
            //function call for Mode------------------------------------------------------------------------------------
            z.mode(N, a);
            //--------------------------------------------------------------------------------------------------------------
            //function call for Standerd Deviation-------------------------------------------------------------------           
            z. stanDev(N, a);            
            //---------------------------------------------------------------------------------------------------------------

            //function call for Histogram-------------------------------------------------------------------------------
            Console.Write("FOR HISTOGRAM PRESS H OR TO CLOSE PRESS ENTER :  ");
            s=Console.ReadLine();
            if (s == "h")
            {
                z.histogram(N, a);
            }
            else
            {
                Environment.Exit(0);
            }

            Console.ReadLine();
            
        }
    }
}

Monday, June 15, 2015

A C# Program To Explain Get Set Accessor


using System;

namespace Example2
{
  class input
   {
     private static int num1, num2, result;
     public void add()
      {
        result = num1 + num2;
        Console.WriteLine("\n\nAdd = {0}", result);
        Console.ReadLine();
      }

     // Creating property for storing value in num1
     public int Number1
      {
        get
         {
           return num1;
         }
        set
         {
           num1 = value;
         }
      }

     // Creating property for storing value in num2
     public int Number2
      {
        get
         {
           return num2;
         }
        set
         {
           num2 = value;
         }
      }
   }

  class Program
   {
     static void Main(string[] args)
      {
        input inp = new input();
        Console.Write("Enter number 1st:\t");
        inp.Number1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter number 2nd:\t");
        inp.Number2 = Convert.ToInt32(Console.ReadLine());

        inp.add();
      }
   }
}

Saturday, June 13, 2015

Write a program to demonstrate private access specifier?


using System;

namespace Example1
{
  class Program
   {
     private void add()
      {
        int num1, num2, result;
        Console.Write("Enter a number:\t");
        num1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("\nEnter second number:\t");
        num2 = Convert.ToInt32(Console.ReadLine());

        result = num1 + num2;
        Console.WriteLine("{0} + {1} = {2}", num1, num2,           result);
      }
     static void Main(string[] args)
      {
        Program p = new Program();
        p.add(); //It is valid, because private add() is           in same class
        Console.ReadLine();
      }
   }
}