Friday, November 29, 2013

P3C4: C Sharp Program for Binary search

using System;

public class Program_Binary_Search
{
    int binary_search(int[] a, int size, int item)
    {
        int first = 0, last = size - 1, middle;
        while (first <= last)
        {
            middle = (first + last) / 2;

            if (item == a[middle])
                return middle;
            else if (item < a[middle])
                last = middle - 1;
            else
                first = middle + 1;
        }
        return -1;
    }
     
    public static void Main()
    {
     
        int[] b = new int[] {2,4,5,7,8,9,12,15};
        int size=8, item;
     
        Console.WriteLine("Enter a number to search: ");
        item =Convert.ToInt32(Console.ReadLine());


        Program_Binary_Search pbs = new Program_Binary_Search();
        int p = pbs.binary_search(b, size, item);
     
        if (p ==-1)
            Console.WriteLine("\n{0} is not present in the array", item);
        else
            Console.WriteLine("\n{0} is present in the array at index no: {1}", item, p);

        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();
    }
}

OUTPUT

Enter a number to search:
7

7 is present in the array at index no: 3

Press any key to exit...

P3C4: C Sharp Program for Linear search or Serial search

//Source Code
using System;

public class Program_Linear_Search
{
    int linear_search(int[] a, int size, int item)
    {
        int i = 0;
        while(i<size && a[i]!=item)
            i++;

        if(i<size)
            return i; //returns index number of the item in the array
        else
            return -1; //item doesn't present in the array
    }
     
    public static void Main()
    {
     
        int[] b = new int[] {2,4,5,7,8,9,12,15};
        int size=8, item;
     
        Console.WriteLine("Enter a number to search: ");
        item =Convert.ToInt32(Console.ReadLine());


        Program_Linear_Search pls = new Program_Linear_Search();
        int p = pls.linear_search(b, size, item);
     
        if (p ==-1)
            Console.WriteLine("\n{0} is not present in the array", item);
        else
            Console.WriteLine("\n{0} is present in the array at index no: {1}", item, p);

        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();
    }
}

OUTPUT

Enter a number to search:
5

5 is present in the array at index no: 2

Press any key to exit...

Thursday, November 28, 2013

C++: Find out any 5 possible output from the program below.

// C++ Source code
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>

void main()
{
randomize();
   char city[][10]={"Kathmandu", "Lalitpur", "Bhaktapur", "Pokhara", "Lumbini"};
   int fly;

   int count=1;
   while(count<=5)
   {
    for(int i=0; i<3; i++)
    {
    fly=random(2) + 1;
       cout<<city[fly]<<" : ";
    }
      cout<<endl;
      count++;
   }
   getch();
}

OUTPUT

Bhaktapur : Lalitpur : Lalitpur :
Lalitpur : Lalitpur : Lalitpur :
Bhaktapur : Bhaktapur : Bhaktapur :
Bhaktapur : Bhaktapur : Bhaktapur :
Bhaktapur : Lalitpur : Bhaktapur :

C++: An example (source code) of class with member functions.

// C++ Source code
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class flight
{
long flightCode;
   char description[25];

   public:
    void addInfo()
      {
       cin>>flightCode;
         gets(description);
      }

      void showInfo()
      {
         cout<<endl<<"*** OUTPUT ***"<<endl;
       cout<<flightCode<<":"<<description<<endl;
      }
};

void main()
{
flight f;
   f.addInfo();
   f.showInfo();

   getch();
}

OUTPUT

1234.5678
Welcome to my C++ Source Codes Page

*** OUTPUT ***
1234:Welcome to my C++ Source Codes Page