Thursday, 31 January 2013

Process scheduling by SJF's...!!!


         In my last post we have seen how to write a program to First Come First Serve.Today we shall write a C program for computation of process basing on Shortest Job First.

This process is same as that of first come first serve but the completion of process will depend on burst time of the process. So the process which has last burst time will completes it's execution first and followed by next process which has next least burst time.

The inputs for this program is the list of processes names with their burst times. now we shall declare array proc_name[100] to store process names and  burst[100] to store burst times of the processes.

int n,i,j,t,tot=0,wait[100],burst[100];
char process[100],s[10];
float avg;
printf("\nEnter number of processes:");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
      printf("\nEnter name of %d process:",i);
      scanf("%s",process[i]);
      printf("\nEnter burst time of %d process:",i);
      scanf("%d",&burst[i]);
   }

The above declaration of variables contain which are used for loop controlling and avg is to find the average waiting time of a process. 

After declaration and intake of the input variables we need to check the burst times of the process and need to swap the process following the ascending order of the burst times. Here we shall use bubbles sorting technique to swap those process.The code of the swapping is as follows:

for(i=1;i<=n;i++)
   {
     for(j=i+1;j<=n;j++)
      {
 if(burst[i]>burst[j])
  {
   t=burst[i];
   burst[i]=burst[j];
   burst[j]=t;

   strcpy(s,process[i]);
   strcpy(process[i],process[j]);
   strcpy(process[j],s);
  }
      }
   }
After swapping of the process's we need to find the waiting time of each process by the formula  wait[i]=wait[i-1]+burst[i-1]; and we find total waiting time of all the processes by using the for loop as follows:
for(i=2;i<=n;i++)
   {
      wait[i]=wait[i-1]+burst[i-1];
      tot=tot+wait[i];
   } 
There fore after writing all the code for all sub parts we shall now write program in a completer manner:

 #include<stdio.h>
#include<conio.h>
void main()
{
   int n,i,j,t,tot=0,wait[100],burst[100];
   char process[100],s[10];
   float avg;
   clrscr();
   printf("\nEnter number of processes:");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
      printf("\nEnter name of %d process:",i);
      scanf("%s",process[i]);
      printf("\nEnter burst time of %d process:",i);
      scanf("%d",&burst[i]);
   }
   for(i=1;i<=n;i++)
   {
     for(j=i+1;j<=n;j++)
      {
 if(burst[i]>burst[j])
  {
   t=burst[i];
   burst[i]=burst[j];
   burst[j]=t;

   strcpy(s,process[i]);
   strcpy(process[i],process[j]);
   strcpy(process[j],s);
  }
      }
   }
   wait[1]=0;
   for(i=2;i<=n;i++)
   {
      wait[i]=wait[i-1]+burst[i-1];
      tot=tot+wait[i];
   }
   avg=(float)tot/(float)n;
   printf("\nTotal waiting time=%d",tot);
   printf("\nAverage waiting time=%f",avg);
   getch();
}

Wednesday, 30 January 2013

process scheduling by FCFS


    Today we are going write a c- program for one of the process scheduling techniques we have learnt. So let us write the program for the first technique First Come First Serve.
This technique computes the process depending on, which process comes first. It doesn’t depend upon the burst time of the process. In order to write the program first we need to know about the inputs, process to be done and then the output. Input of this program is process name and burst time of that process. In order to store those inputs we shall use arrays for process name and burst time of processes. We need to compute the waiting time of each process so, let us use an array wait. We need to calculate total waiting time of all the process and need to find out average waiting time. Therefore total set of variables required are:
int n,i,j,tot=0,wait[100],burst[100];
   char process[100];
   float avg;    
The variables n is used to get the size of total number of processes. Variables i,j for loop controlling. The procedure after getting inputs is we need calculate the waiting time of each process and add them to total waiting time. Waiting time of each process is given by formula: wait[i]=wait[i-1]+burst[i-1] . For this purpose we shall use “for” loop as follows:
for(i=2;i<=n;i++)
   {
      wait[i]=wait[i-1]+burst[i-1];
      tot=tot+wait[i];
   }

Here we finding total waiting time in the same for loop. Avg waiting time is calculated by dividing total waiting time by total number of processes .Hence total program is as follows:
#include<stdio.h>
#include<conio.h>
void main()
{
   int n,i,j,t,tot=0,wait[100],burst[100];
   char process[100];
   float avg;
   clrscr();
   printf("\nEnter number of processes:");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
      printf("\nEnter name of %d process:",i);
      scanf("%s",process[i]);
      printf("\nEnter burst time of %d process:",i);
      scanf("%d",&burst[i]);
   }
   wait[1]=0;
   for(i=2;i<=n;i++)
   {
      wait[i]=wait[i-1]+burst[i-1];
      tot=tot+wait[i];
   }
   avg=(float)tot/(float)n;
   printf("\nTotal waiting time=%d",tot);
   printf("\nAverage waiting time=%f",avg);
   getch();
}

Tuesday, 29 January 2013

Library program in cpp


Today we shall write a cpp program for a library usage….!!!!

              Here the program mainly consists of get the input data about number of books present in the library, displaying those books back to show a student about number of books, issuing of the book, returning of the book.
Now we shall learn to write the program step by step such that we may not get confused and understand it easily. Here our first task is to get the knowledge about class and the variable, functions to be present in the class. By observing the program we came know that we need variables to store the author, title, publisher, price, number of books and rack no of the book. These all are the variables which we need to know them because we need to get the information about the library. To get these variables we need functions. Therefore the class of the program is as follows:
 class book
{
     public:
                char author[20],title[20],publisher[20];    //These are the variables need to get the data.
                float price;                                //These variables are called as member variables.
                int stock,rackno;

     public:                                        //These are functions in the class.
                void getdata();                             //Called as member fuctions.
                void display();
                void issue();
};

So, we have created a class. Now our task is to make functions to get the data. We get these inputs through CIN and COUT statements as follows:
void book:: getdata()            //In this function we requesting to enter
                                                                // the details of all the books present in the class
{

     cout<<"\n**Enter the author of the book:";
     cin>>author;
     cout<<"\n**Enter the title of the book:";
     cin>>title;
     cout<<"\n**Enter the publisher of the book:";
     cin>>publisher;
     cout<<"\n**Enter the price of the book:";
     cin>>price;
     cout<<"\n**Enter the stock present:";
     cin>>stock;
     cout<<"\n**Enter rack no of the book:";
     cin>>rackno;
}

If a user wants to know about the details of the total number of books present in the library we need to show him details of the book. The display function of the program is as follows:
void book:: display()                //To display all the books present in the library.
{
     cout<<"\n-- Author of the book @ " << author;
     cout<<"\n-- Title of the book  @ "<<title;
     cout<<"\n-- Price of the book  @ "<<price;
     cout<<"\n-- Publisher name     @ "<<publisher;
     cout<<"\n-- Stock present      @ "<<stock;
     cout<<"\n-- Rack no is         @ "<<rackno;
}

We have completed all our tasks of getting the input data and display the data.The main function of our program includes cases to issues the book and submission of the book. We shall now write themain function of  our program is as follows:
void main()
{
     book ob[10];
     int a,b,n=0;
     clrscr();

     do
      {

                cout<<"\n\n\t\t   **************Welcome to library******************";
                cout<<"\n\n\t\t\t|--->1.Setdata\n\n\t\t\t|--->2.Display total books present in the library\n\n\t\t\t|--->3.Issue a book\n\n\t\t\t|--->4.Return the book\n\n\t\t\t|--->5.exit";
                cout<<"\n\n\t\t\t Enter your choice from above menu:";
                int ch;
                cin>>ch;
                switch(ch)
                                {

                                                case 1:  cout<<"\n%Enter how many books you need to enter:";

                                                                 cin>>n;

                                                                 for(int i=1; i<=n;i++)
                                                                   {
                                                                      cout <<"\n\t\t\t***********Enter the detail of book " << i << "**********\n";
                                                                      ob[i].getdata();
                                                                   }
                                                                 break;

                                                case 2:  if(n==0)
                                                                  {
                                                                     cout<<"\n\t\t\t**********No books in library**********";
                                                                  }
                                                                 for(i=1;i<=n;i++)
                                                                   {
                                                                      cout <<"\n\t\t\t**********Details of the book " << i << "**********\n";
                                                                      ob[i].display();
                                                                   }
                                                                 break;


/*This is to issue books to students*/

                                                case 3:  cout<<"\nEnter the title and author of the book need to be issued:";
                                                                 char ititle[20],iaut[20];
                                                                 cout<<"\nTitle:";
                                                                 cin>>ititle;
                                                                 cout<<"\nAuthor:";
                                                                 cin>>iaut;
                                                                 for(i=1;i<=n;i++)
                                                                    {

                                                                                a=strcmp(ititle,ob[i].title);

                                                                                b=strcmp(iaut,ob[i].author);

                                                                                if(a==0 && b==0)
                                                                                   {

                                                                                                ob[i].stock=ob[i].stock-1;
                                                                                                break;
                                                                                   }

                                                                   }
                                                                 cout<<"Book found and in the rack"<<ob[i].rackno<<"\n no.of copies available are"<<ob[i].stock;

                                                                 break;
/*This function is to return books and update the number of books*/
                                                case 4: cout<<"Enter your library number:";
                                                                int libno;
                                                                cin>>libno;
                                                                cout<<"Enter title and author of the book:"<<"\nTitle";
                                                                char rtitle[20],rauthor[20];
                                                                cin>>rtitle;
                                                                cout<<"\nAuthor";
                                                                cin>>rauthor;
                                                                for(i=1;i<=n;i++)
                                                                  {
                                                                    a=strcmp(rtitle,ob[i].title);

                                                                    b=strcmp(rauthor,ob[i].author);

                                                                    if(a==0 && b==0)
                                                                     {
                                                                       ob[i].stock=ob[i].stock+1;
                                                                       break;
                                                                     }
                                                                  }
                                                                break;



                                                case 5:  exit(0);
                                }
                }while(1);

}

The complete program is as follows:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
class book
{
     Public:
     char author[20],title[20],publisher[20];    //These are the varriables need to get the data.
                float price;                                //These variables are called as member variables.
                int stock,rackno;

     public:                                        //These are fuctions in the class.
                void getdata();                             //Called as member fuctions.
                void display();
                void issue();
};

void book:: getdata()            //In this function we requesting to enter
                                                                // the details of all the books present in the class
{

     cout<<"\n**Enter the author of the book:";
     cin>>author;
     cout<<"\n**Enter the title of the book:";
     cin>>title;
     cout<<"\n**Enter the publisher of the book:";
     cin>>publisher;
     cout<<"\n**Enter the price of the book:";
     cin>>price;
     cout<<"\n**Enter the stock present:";
     cin>>stock;
     cout<<"\n**Enter rack no of the book:";
     cin>>rackno;
}

void book:: display()                //To display all the books present in the library.
{
     cout<<"\n-- Author of the book @ " << author;
     cout<<"\n-- Title of the book  @ "<<title;
     cout<<"\n-- Price of the book  @ "<<price;
     cout<<"\n-- Publisher name     @ "<<publisher;
     cout<<"\n-- Stock present      @ "<<stock;
     cout<<"\n-- Rack no is         @ "<<rackno;
}

int issue(char,char,book);

void main()
{
     book ob[10];
     int a,b,n=0;
     clrscr();

     do
      {

                cout<<"\n\n\t\t   **************Welcome to library******************";
                cout<<"\n\n\t\t\t|--->1.Setdata\n\n\t\t\t|--->2.Display total books present in the library\n\n\t\t\t|--->3.Issue a book\n\n\t\t\t|--->4.Return the book\n\n\t\t\t|--->5.exit";
                cout<<"\n\n\t\t\t Enter your choice from above menu:";
                int ch;
                cin>>ch;
                switch(ch)
                                {

                                                case 1:  cout<<"\n%Enter how many books you need to enter:";

                                                                 cin>>n;

                                                                 for(int i=1; i<=n;i++)
                                                                   {
                                                                      cout <<"\n\t\t\t***********Enter the detail of book " << i << "**********\n";
                                                                      ob[i].getdata();
                                                                   }
                                                                 break;

                                                case 2:  if(n==0)
                                                                  {
                                                                     cout<<"\n\t\t\t**********No books in library**********";
                                                                  }
                                                                 for(i=1;i<=n;i++)
                                                                   {
                                                                      cout <<"\n\t\t\t**********Details of the book " << i << "**********\n";
                                                                      ob[i].display();
                                                                   }
                                                                 break;


/*This is to issue books to students*/

                                                case 3:  cout<<"\nEnter the title and author of the book need to be issued:";
                                                                 char ititle[20],iaut[20];
                                                                 cout<<"\nTitle:";
                                                                 cin>>ititle;
                                                                 cout<<"\nAuthor:";
                                                                 cin>>iaut;
                                                                 for(i=1;i<=n;i++)
                                                                    {

                                                                                a=strcmp(ititle,ob[i].title);

                                                                                b=strcmp(iaut,ob[i].author);

                                                                                if(a==0 && b==0)
                                                                                   {

                                                                                                ob[i].stock=ob[i].stock-1;
                                                                                                break;
                                                                                   }

                                                                   }
                                                                 cout<<"Book found and in the rack"<<ob[i].rackno<<"\n no.of copies available are"<<ob[i].stock;

                                                                 break;
/*This function is to return books and update the number of books*/
                                                case 4: cout<<"Enter your library number:";
                                                                int libno;
                                                                cin>>libno;
                                                                cout<<"Enter title and author of the book:"<<"\nTitle";
                                                                char rtitle[20],rauthor[20];
                                                                cin>>rtitle;
                                                                cout<<"\nAuthor";
                                                                cin>>rauthor;
                                                                for(i=1;i<=n;i++)
                                                                  {
                                                                    a=strcmp(rtitle,ob[i].title);

                                                                    b=strcmp(rauthor,ob[i].author);

                                                                    if(a==0 && b==0)
                                                                     {
                                                                       ob[i].stock=ob[i].stock+1;
                                                                       break;
                                                                     }
                                                                  }
                                                                break;



                                                case 5:  exit(0);
                                }
                }while(1);

}

Monday, 28 January 2013

Programming using C++

Today we shall a write a program to find area and perimeter of the rectangle by using classes,constructors, functions and array of objects....The question to the program is as follows..

  create a class rectangle has the attributes length and breadth each of which default to one.It has member function that calculate the perimeter and area of the rectangle. It has set and get functions for both length and breadth.The set function should verify the length and breadth it has integer numbers are between 0 and 20.

Program to the above problem is::


#include<iostream.h>
#include<conio.h>
class rectangle
{
   int length,width;
   public:
   void perimeter();
void area();
void display();
void setdata();
};

rectangle :: rectangle()
{
   cout<<"Constructor called":
length=1;
width=1;
}

void rectangle :: setdata()
{
  cout<<"\n  Enter the length and width of the rectangle:";
cin>>length>>width;
if(-32768<length && length<20 && width>-32768 && width<32768)
{
 }
else
{
    cout<<"Enter valid values for length and width";
}

if((length>0 && length<20) && (width>0 && width<20))
{
}
else

cout<<"\nPlease enter valid length and width between 0 and 20":
cout<<"Enter length of the rectangle":
cin>>length;
cout<<"Enter width of the rectangle":
cin>>width;
}
}

void rectangle :: display()
{

cout<<"Given length and breadth of the rectangle are:";
cout<<length<<width;
}

void rectangle :: area()
{
 int area;
area=length*width;
cout<<"\n area of the given rectangle is"<<area;
}

void rectangle :: perimeter()
{
   perimeter=2*(length+width);
cout<<"Perimeter of the given rectangle is:"<<perimeter;
}

void main()
{
   clrscr();
rectangle ob;
ob.setdata();
ob.display();
ob.area();
ob.perimeter();
getch();
}


Wednesday, 2 January 2013

Introduction to basics of object oriented programming

Introduction to basics of object oriented programming

          Before learning Object Oriented Programming you should have some basics of c-language. Because here we are learning c++ which is a object oriented language so, which is developed from c language. You can also learn c++ without the knowledge of c-language but you can’t convert a problem in to programme without the knowledge of C.
          Let us begin your course by learning the basic input and output statements. The basic input statement in C language is “scanf”, in the same way the basic input statement in C++ is “cin”.
Ø The syntax for this input statement is    cin>>variable_name;
        In c++ we need not specify the format of the variable. We can directly specify the name of the variable. Care should be taken that we should add quotations to the variable name.
   For example we need to enter 5 into a variable named as var. then the input statement is as follows cin>>var; therefore during the compilation complier asks for input for the variable.
The basic output statement in c language is “printf”, in the same way the output statement in c++ is “cout”.
Ø The syntax for cout statement is cout<<”output statement”;
         Here we are two different situations for output statement has 1) the output statement is string 2) output statement is value stored in a variable. To illustrate this example, consider that we need display the value given by the user.
Ø Then the output statement is cout<<”given input values is”<<var;
 This explained as the string which is to be printed is given in the quotations and the variable which is containing this value is given without the quotations.  


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int var;
  cout<<"Enter the value in to the variable:";
  cin>>var;
  cout<<"Given input value is:"<<var;
  getch();
}