Sunday, October 27, 2013

C Sharp Programming: Hospital Management Project

This Hospital Management Project is based on A Level Text Book. Slightly modified on database in order to show relationship.

1. Create a new project in Visual Studio 2008 with name "Hospital_Management". Choose "Windows Forms Application" while creating new project.

2. To create a new data source:
  • Click Data menu > Add New Data Source...
  • Then new Dialog box appears
  • Select "database" icon and click next button
  • Click on "new connection" button
  • Write "Hospital" on Database textbox and click on create button
  • Better to create database file inside project folder
  • Now just click on Next ... Next and Finish button
  • Finally you will see "HospitalDataSet.xsd" file in the solution explorer window of the project.

Friday, October 25, 2013

C Programming: Write a C Program to display size of all variable types using sizeof() function.

Source code

#include<conio.h>

void main()
{
int i;
   char c;
   float f;
   double d;

   printf("The size of integer is %d", sizeof(i));
   printf("\nThe size of character is %d", sizeof(c));
   printf("\nThe size of float is %d", sizeof(f));
   printf("\nThe size of double is %d", sizeof(d));

   getch();
}

Output

The size of integer is 4
The size of character is 1
The size of float is 4
The size of double is 8

Wednesday, October 9, 2013

C++: What do you mean by function overloading in C++? Explain with examples.

Function Overloading in C++

A function name having several definitions which are differentiated by the number or types of their arguments is called function overloading.

Example:

//Source code in C++

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

class printing
{
public:
    void print(int i)
      {
      cout<<"Printing integer: " << i << endl;
      }

      void print(double f)
      {
      cout<<"Printing float: " << f << endl;
      }

      void print(char* c)
      {
      cout<<"Printing character: " << c <<endl;
      }
};

void main()
{
   printing p;

   cout<<"*** OUTPUT ***" << endl;
   p.print(5);
   p.print(500.55);
   p.print("Hello! alevelcomputer.blogspot.com");

   getch();
}

OUTPUT

*** OUTPUT ***
Printing integer: 5
Printing float: 500.55
Printing character: Hello! alevelcomputer.blogspot.com

P2C2: Write a program in C Sharp to input an abbreviation and then output its meaning. If the abbreviation is not in the table, a suitable message should be output.

Input Table

Abbreviation
Meaning
m
metre
kg
kilogram
s
second
a
ampere
k
kelvin

//Source code in C Sharp


using System;
namespace Application_Abbreviation
{
    class Program_Abbreviation
    {
        static void Main(string[] args)
        {
            string abb;

            Console.WriteLine("Enter an abbreviation: ");
            abb = Convert.ToString(Console.ReadLine());

            switch (abb)
            {
                case "m":
                    Console.WriteLine("metre ");
                    break;

                case "kg":
                    Console.WriteLine("kilogram ");
                    break;

                case "s":
                    Console.WriteLine("second ");
                    break;

                case "a":
                    Console.WriteLine("ampere ");
                    break;

                case "k":
                    Console.WriteLine("kelvin ");
                    break;

                default:
                    Console.WriteLine("\nInvalid abbreviation");
                    break;

            }
        }
    }
}

OUTPUT

Enter an abbreviation:
kg
kilogram

Monday, October 7, 2013

CBSE2009AQ1. What is the difference between call by value and call by reference? Give an example in C++ illustrate both.

 Call by value
 Call by reference
  1. A copy of value or argument passed from calling routine to the function.
  2. The original value doesn't change by called function.
  1. The original value or argument passed from calling routine to the function.
  2. The original value can change by called function.

Coding for Call by value:

#include <iostream>
using namespace std;

// function declaration
void swap(int x, int y);


int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;

   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using value*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   return 0;
}

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

Coding for Call by reference:

#include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);


int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using variable reference.*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
   return 0;
}

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

Thursday, October 3, 2013

P3C4: Write a program in C Sharp to implement Linked List for adding, deleting and retrieving data items.

//Source code

using System;

class Subject
{
    public string SubjectName;
    public Subject Next;
}

class ListOfSubjects
{
    private int size;

    public ListOfSubjects()
    {
        size = 0;
        Head = null;
    }
    public int Count
    {
        get { return size; }
    }

    public Subject Head;

    public int Add(Subject NewItem)
    {
        Subject Sample = new Subject();

        Sample = NewItem;
        Sample.Next = Head;
        Head = Sample;
        return size++;
    }

    public Subject Retrieve(int Position)
    {
        Subject Current = Head;

        for (int i = 0; i < Position && Current != null; i++)
            Current = Current.Next;
        return Current;
    }

    public bool Delete()
    {
        if (Head == null)
        {
            Console.WriteLine("The list is empty");
            return false;
        }

        Subject Current;

        Current = Head.Next;
        Head.Next = Current.Next;
        size--;
        return true;
    }

    public bool Delete(int Position)
    {
        if (this.Retrieve(Position) == null)
            return false;

        this.Retrieve(Position - 1).Next = this.Retrieve(Position + 1);
        size--;
        return true;
    }

    public bool Find(Subject ItemToFind)
    {
        Subject Current = new Subject();

        if (ItemToFind == null)
            return false;

        for (Current = Head; Current != null; Current = Current.Next)
        {
            if (Current.SubjectName == ItemToFind.SubjectName)
                return true;
        }

        return false;
    }
}

class Exercise
{
    static int Main()
    {
        ListOfSubjects Subjects = new ListOfSubjects();
        Subject Sub;
        Subject SubjectToFind;

        Sub = new Subject();
        Sub.SubjectName = "Maths";
        Subjects.Add(Sub);

     
        Console.WriteLine(" === Output ===");
        Console.WriteLine("Number of Subjects: {0}", Subjects.Count);

        for (int i = 0; i < Subjects.Count; i++)
        {
            Subject s = Subjects.Retrieve(i);
            Console.WriteLine("\nSubjects Information");
            Console.WriteLine("Subject Name: {0}", Sub.SubjectName);
        }

        SubjectToFind = new Subject();
        SubjectToFind.SubjectName = "Maths";
     

        bool Found = Subjects.Find(SubjectToFind);
        if (Found == true)
            Console.WriteLine("\nItem was found\n");
        else
            Console.WriteLine("\nItem not found\n");

        Console.ReadKey();
        return 0;
    }
}

P3C3: Linked List

A linked list is a data structure which can link other data. E.g. a list of subjects, people's name etc. Linked list is a list of nodes i.e. data items. Each node consists a data part and a link part. The first node of linked list is pointed by a Header node which is also called an external node. Similarly, the last node's link part consists a NULL.

C Sharp can implement the linked list easily by using System.Collections.Generic class.

Source code

using System;
//used for linked list feature
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        LinkedList<string> linked = new LinkedList<string>();

        linked.AddFirst("Maths");
        linked.AddLast("Politics");
        linked.AddLast("Biology");
        linked.AddLast("Physics");
        linked.AddLast("Computing");

        foreach (var item in linked)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
}

OUTPUT

Maths
Politics
Biology
Physics
Computing