Wednesday 30 July 2014

Program to print pattern.

//Program to print pattern.

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  for(int i=1;i<=5;i++)
{
  cout<<"\n";
  for(int j=4;j>=i;j--)
  cout<<"  ";
  for(int k=1;k<=i;k++)
  cout<<k<<" " ;
  for(int l=k-2;l>=1;l--)
  cout<<l<<" ";
}
  getch();
}

Output:


Monday 28 July 2014

File handling

//Menu driven program
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student{
     int roll;
     char name[20];
     float marks;
     public:
     void getdata();
     void display();
     int retroll()
     {
     return roll;
     }
     void update(float m)
     {
     marks=m;
     }
   };
void student::getdata()
{
  cout<<"\nRoll no : ";
  cin>>roll;
  cout<<"\nName : ";
  gets(name);
  cout<<"\nMarks : ";
  cin>>marks;
}
void student::display()
{
  cout<<"\nRoll no : "<<roll<<"\nName : "<<name<<"\nMarks : "<<marks<<endl;
}
void create()
{
  fstream  file;
  file.open("stud.dat",ios::app|ios::binary);
  student s;
  char c;
  do
{
  s.getdata();
  file.write((char*)&s,sizeof(s));
  cout<<"\nWish to enter more (y/n)? : ";
  cin>>c;
} while(c=='y'||c=='Y');
  file.close();
}
void display_data()
{
  fstream file;
  file.open("stud.dat",ios::in | ios::binary);
  student s;
  while(file.read((char*)&s,sizeof(s)))
  s.display();
  file.close();
}
void Delete()
{
  int grn;
  cout<<"Enter roll no to be deleted : ";
  cin>>grn;
  fstream file,temp;
  student s;
  file.open("stud.dat",ios::in|ios::binary);
  temp.open("temp.dat",ios::out|ios::binary);
  while(file.read((char*)&s,sizeof(s)))
{
  if(s.retroll()!=grn)
  temp.write((char*)&s,sizeof(s));
}
  file.close();
  temp.close();
  remove("stud.dat");
  rename("temp.dat","stud.dat");
}
void modify()
{
  int grn,found=0;
  float nmarks;
  cout<<"\nEnter roll number : ";
  cin>>grn;
  fstream file;
  student s;
  file.open("stud.dat",ios::in|ios::binary|ios::out);
  while(file.read((char*)&s,sizeof(s)))
 {
  if(s.retroll()==grn)
 {
  found=1;
  cout<<"\nEnter new marks of student : ";
  cin>>nmarks;
  break;
}
}
  if(found==0)
{
  cout<<"\nRecord does not exist!!";
  getch();
  file.close();
  return;
}
  s.update(nmarks);
  int p=file.tellg();
  int t=p-sizeof(s);
  file.seekg(t,ios::beg);
  file.write((char*)&s,sizeof(s));
  s.display();
  file.close();
}
void menu()
{
  cout<<"\t\tMAIN MENU\n\n"
    <<"\tPress 1 : To add record\n"
    <<"\tPress 2 : To display record\n"
    <<"\tPress 3 : To delete record\n"
    <<"\tPress 4 : To modify data\n"
    <<"\tPress 5 : To exit\n";
}
void main()
{
  int opt;
  float nmarks;
  do
{
  clrscr();
  menu();
  cout<<"\n\tEnter your option : ";
  cin>>opt;
switch(opt)
{
  case 1:clrscr();
cout<<"\nFill following details :\n";
create();
break;
  case 2:clrscr();
cout<<"\nDetails are :\n " ;
display_data();
getch();
break;
  case 3:clrscr();
Delete();
cout<<"\nNew file is :\n";
display_data();
getch();
break;
  case 4:clrscr();
modify();
getch();
break;
  case 5:cout<<"\n\tExit!!";
break;
  default:cout<<"\n\tInvalid option!!";
 getch();
}
} while(opt!=5);
  getch();
}




Sunday 27 July 2014

Merge Sort

//Program to sort an array using merge sort.
#include<iostream.h>
#include<conio.h>
void merge(int a[],int,int);
void merge_sort(int a[],int ,int,int);
void main()
{
   clrscr();
   int A[10],N,i;
   cout<<"\nEnter number of elements in array : ";
   cin>>N;
   cout<<"\nElements are : ";
   for(i=0;i<N;i++)
{
   cin>>A[i];
}
   merge(A,0,N-1);
   cout<<"\nSorted array is : ";
   for(i=0;i<N;i++)
{
   cout<<"  "<<A[i];
}
   getch();
}
void merge(int a[10],int beg,int end)
{
   int mid=(beg+end)/2;
   if(beg<end)
{
   merge(a,beg,mid);
   merge(a,mid+1,end);
   merge_sort(a,beg,mid,end);
}
}
void merge_sort(int a[10],int beg,int mid,int end)
{
   int i=beg,j=mid+1,k=beg,temp[10];
   while(i<=mid&&j<=end)
{
   if(a[i]<a[j])
   temp[k++]=a[i++];
   else
   temp[k++]=a[j++];
}
   if(i>mid)
{
   while(j<=end)
   temp[k++]=a[j++];
}
   else
{
   while(i<=mid)
   temp[k++]=a[i++];
}
  for(int s=beg;s<k;s++)
  a[s]=temp[s];
}

Output:






Wednesday 23 July 2014

Pascal triangle

//Pascal triangle
#include<iostream.h>
#include<conio.h>
void main()
{
   clrscr();
   int a[7][7]={0},i,j,row=2,col;
   a[0][0]=1;
   a[1][0]=1;
   a[1][1]=1;
   while(row<=7)
 {
   a[row][0]=1;
   for(col=0;col<=row;col++)
   a[row][col]=a[row-1][col] + a[row-1][col-1];
   row++;
 }

   for(i=0;i<7;i++)
{
  for(int k=6;k>i;k--)
  cout<<"  ";
  for(j=0;j<=i;j++)
  cout<<"  "<<a[i][j];
  cout<<"\n";
}

  getch();
 }

Output:












Tuesday 22 July 2014

Insertion sort


//Program to sort an array using Insertion sorting.
#include<iostream.h>
#include<conio.h>
void insert(int* ,int );
void main()
{
clrscr();
int A[10],i,N;
cout<<"\nEnter number of elements in array : ";
cin>>N;
cout<<"\nElements are : " ;
for(i=0;i<N;i++)
cin>>A[i];
insert(A,N);
cout<<"\nSorted array is : ";
for(i=0;i<N;i++)
cout<<"  "<<A[i];
getch();
}
void insert(int a[10],int n)
{
for(int i=1;i<=n-1;i++)
{
int item=a[i];
int hole=i;
while(hole>0 && a[hole-1]>item)
{
a[hole]=a[hole-1];
hole=hole-1;
}
a[hole]=item;
}
}


Output:


Monday 21 July 2014

Program to print hollow triangle

#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
cout<<"\n";
for(i=1;i<=11;i++)
cout<<" ";
cout<<"*";
for( i=1;i<=5;i++)
{
if(i<5)
{
cout<<"\n";
for(int j=4;j>=i;j--)
cout<<"  " ;
cout<<" * ";
for(int k=1;k<=2*i-1;k++)
cout<<"  ";
cout<<" * ";
 }
else
{
cout<<"\n";
for(int s=1;s<=8;s++)
cout<<" * ";
}
}
getch();
}

Output: