Friday, November 25, 2011

19)a smalll prog To implement linked queue with options to add, delete, display and exit.




#include<iostream.h>
#include<conio.h>
#include<process.h>

struct node
{ int info;
  node* next;
};

void main()
{
 clrscr();
 node* front,*rear;
 char ch;
  do
   {
    front=rear=NULL;
     cout<<"\n";
     cout<<"1 to add\n";
     cout<<"2 to delete\n";
     cout<<"3 to display\n";
     cout<<"enter choice";
     cin>>ch;

  switch(ch)
   {
    case 1: rear=addQ(front,rear);
             if (front==NULL)
             front=rear;
             break;
    case 2: front=delQ(front,rear)
 if(front==NULL)
             rear=NULL;
             break;
    case 3: display();
              break;
    case 4:exit(0);
             break;
  }

}while(ch<4);
getch();
}

node*addQ(node*front,node*rear)
{
  node*ptr;
  ptr=new node;
  cout<<"enter info:";
  cin>>ptr----->info;
  ptr------>next=NULL;
   if(rear==null)
     front=rear=ptr;
   else
     { rear------>next=ptr;
        rear=ptr;
     }
 return rear;
}

node *delQ(node*front,node*rear)
{node*ptr;
 if(front==NULL)
   cout<<"empty!";
 else if(front==rear)
   { ptr=front;
     front=rear=NULL;
     delete ptr;
   }
   else
   {
    ptr=front;
    front=front----->next;
    delete ptr;
   }
return front;
}
void display()
{ node*ptr;
  if(front==NULL)
  cout<<"empty";
   else
     { ptr=front;
       while(ptr!=NULL)
         { cout<<ptr------->info;
           ptr=ptr-------->next;
         }
      }
}

/* OUTPUT

1 to add
2 to delete
3 to display
enter choice1
enter info13

1 to add
2 to delete
3 to display
enter choice3
13

1 to add
2 to delete
3 to display
enter choice2

1 to add
2 to delete
3 to display
enter choice3
empty

1 to add
2 to delete
3 to display
enter choice4
*/

Friday, August 5, 2011

kie here's anoder prog..11) To store Book Number, Name, Price and Author. Display the file and delete a record by reading the Book Number.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class book
{  private: float price;
   char bname[20];
   char author[20];
   int bno;

public:
void getdata()
{cout<<"\nEnter book no      : ";   cin>>bno;
 cout<<"\nEnter book name    : ";    gets(bname);
 cout<<"\nEnter Author name  : ";    gets(author);
 cout<<"\nEnter Price        : ";    cin>>price;
}

void putdata()
{cout<<"\nBook no      : "<<bno;
 cout<<"\nBook    Name : "<<bname;
 cout<<"\nAuthor       : "<<author;
 cout<<"\nPrice        : "<<price;
}
int retbno(){return(bno);}
}bk;

void create()
{ int n; cout<<"\nEnter no: of records"; cin>>n;
  ofstream fout;
  fout.open("Book.dat",ios::binary|ios::out);
   for(int i=0;i<n;i++)
   {bk.getdata();
    fout.write((char*)&bk,sizeof(bk));
    }
}


void delete_()
{ cout<<"\nEnter book no: of record to be deleted : ";
  int no; cin>>no;
  ifstream fin;
  fin.open("Book.dat",ios::binary|ios::in);
  ofstream fout;
  fout.open("temp.dat",ios::binary|ios::out);
  while(fin)
  {fin.read((char*)&bk,sizeof(bk));
   if(bk.retbno()!=no)
    {fout.write((char*)&bk,sizeof(bk));}
  }
    fin.close();
    fout.close();
    remove("Book.dat");
    rename("Temp.dat","Book.dat");
}

void read_and_disp()
{
 ifstream fin;
 fin.open("Book.dat");

  while(fin.read((char*)&bk,sizeof(bk)))
   {bk.putdata();break;}

}

void main()
{clrscr();
 create();
 delete_();
 read_and_disp();
 getch();
 }

Wednesday, August 3, 2011

ohk..now this is the mah first programs using files.. Its a program to To create a Binary File to store Roll Number, Name and Marks in 3 subjects of 10 students, read data from the file and display it.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class student
{ private: int roll;
  char name[20];
  float m1,m2,m3;
  public:
void getdata()
{cout<<"\nEnter roll no : ";
 cin>>roll;
 cout<<"\nEnter name : ";
 gets(name);
 cout<<"\nEnter mark for English  : ";
 cin>>m1;
 cout<<"\n               Maths    : ";
 cin>>m2;
 cout<<"\n               Computer : ";
 cin>>m3;
}

void putdata()
{cout<<"\nStudent Roll : "<<roll;
 cout<<"\n        Name : "<<name;
 cout<<"\n        Mark of English   : "<<m1;
 cout<<"\n                Maths     : "<<m2;
 cout<<"\n                Computer  : "<<m3;
 cout<<"\n        Total marks       : "<<m1+m2+m3;
 cout<<"\n        Average marks     : "<<(m1+m2+m3)/3;

}



}stud;

void create()
{
  ofstream fout;
  fout.open("Student.dat");
  int n; cout<<"\nEnter no:of students  : "; cin>>n;

  for(int i=0;i<n;i++)
   {stud.getdata();
    fout.write((char*)&stud,sizeof(stud));
    }
}

void read_and_disp()
{
 ifstream fin;
 fin.open("Student.dat");
 while(fin)
  {fin.read((char*)&stud,sizeof(stud));
   stud.putdata();}

}

void main()
{clrscr();
 create();
 read_and_disp();
 getch();
 }

Saturday, March 5, 2011

a structure..student to find name, roll no, and total marks and display it in descending order ot total marks.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>


struct student
{int rno;
 char name[20];
 float total;
 };


void readarray(student s[10],int n)
{int i;
 cout<<"\nenter name";
   cout<<"\nenter rno and total marks\n";
     for(i=0;i<n;i++)
{cin>>s[i].name;
cin>>s[i].rno>>s[i].total;}
 }

void disparray(student s[10],int n)
{int i;
for(i=0;i<n;i++)
cout<<"\n"<<s[i].rno<<"\t"<<s[i].name<<"\t"<<s[i].total;
}

void sort(student s[10],int n)
{ int i,j,flag=0;
  student temp;
  for(i=0;((i<n-1)&&(!flag));i++)
  {flag=1;
  for(j=0;j<n-i-1;j++)
   { if(s[j].total<s[j+1].total)
    {temp=s[j];
     s[j]=s[j+1];
     s[j+1]=temp;
     flag=0;
    }
   }
  }
}
void main()
{clrscr();
 student s[10];
 int n;
 cout<<"\nenter no:";
 cin>>n;
 readarray(s,n);
 sort(s,n);
 disparray(s,n);




getch();
}

a structure program about country names and findin the country name and then displaying all the other details.


#include<iostream.h>
#include<conio.h>
#include<string.h>
struct list
{char country[20];
 char capital[10];
 float pc;
 };

void readarray(list c[],int n)
{cout<<"\nenter country name capital and percapita income";
 for(int i=0;i<n;i++)

 cin>>c[i].country>>c[i].capital>>c[i].pc;

}

void searchcountry(list c[],int n)
{char cou[20];
 cout<<"\nenter the country to be searched";
 cin>>cou;
 for(int i=0;i<n;i++)
 {if(strcmp(c[i].country,cou)==0)
   cout<<"\t"<<c[i].capital<<"\t"<<c[i].pc; }}
void main()
{
clrscr();
list c[10];
int n;
cout<<"\nenter the no:";
cin>>n;
readarray(c,n);
searchcountry(c,n);
getch();
}

Monday, February 28, 2011

the biggest program that i hav ever made....also a project for school..


#include<iostream.h>
#include<conio.h>
#include<process.h>


void rowsum(int a[10][10],int m,int n)
{int i,j,s;
   for(i=0;i<m;i++)
    {s=0;
    for(j=0;j<n;j++)
    s+=a[i][j];
     cout<<"\nrow sum of "<<i<<"row ="<<s;
     cout<<"\n";}
}


void columnsum(int a[10][10],int m,int n)
{int i,j,s;
   for(j=0;j<n;j++)
    {s=0;
    for(i=0;i<m;i++)
    s+=a[i][j];
     cout<<"\ncolumn sum of"<<j<<"column ="<<s;
     cout<<"\n";}
}


void readmatrix(int a[10][10],int m,int n)
   {int i,j;
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    cin>>a[i][j];
   }
void dispmatrix(int a[10][10],int m,int n)
{   int i,j;
    for(i=0;i<m;i++)
     {cout<<"\n";
    for(j=0;j<n;j++)
     {cout<<a[i][j];
    cout<<"\t";
     }
     }
}


void summatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
  {int i,j;
   for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   c[i][j]=a[i][j]+b[i][j];
  }


void diffmatrix(int a[10][10],int m,int n,int b[10][10],int c[10][10])
  {int i,j;
   for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   c[i][j]=a[i][j]-b[i][j];
  }




int smd(int a[10][10],int n)
   {int i,j,sum=0;
    for(i=0;i<n;i++)
     for(j=0;j<n;j++)
      if(i==j)
sum+=a[i][j];
 return(sum);
}

int sod(int a[10][10], int n)
   {int i,j,sum=0;
     for(i=0;i<n;i++)
      for(j=0;j<n;j++)
       if(i+j==n-1)
sum+=a[i][j];
 return(sum);
}


int sut(int a[10][10],int n)
   {int i,j,sum=0;
     for(i=0;i<n;i++)
      for(j=0;j<n;j++)
       if(j>i)
sum+=a[i][j];
  return(sum);
}

int slt(int a[10][10],int n)
   {int i,j,sum=0;
     for(i=0;i<n;i++)
      for(j=0;j<n;j++)
       if(i>j)
sum+=a[i][j];
   return(sum);
}

void sumposneg(int a[10][10],int m,int n)
{int i,j,sp=0,sn=0;
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
  if(a[i][j]<0)
   sn+=a[i][j];
  else
   sp+=a[i][j];

   cout<<"\nsum of positive  no ="<<sp;

   cout<<"\nsum of negative  no ="<<sn;

}



void transpose(int a[10][10],int b[10][10],int m,int n)

{int i,j;
  for(i=0;i<m;i++)
  for(j=0;j<n;j++)
  b[j][i]=a[i][j];
}



void main()

{clrscr();
int A[10][10],B[10][10],C[10][10],m,n,p,q;
int a[10][10],sum;
char choice,ch1;
do{

cout<<"this is a menu driven program to access..various functions on 2d arrays\n";
cout<<"enter a to find the rowsum of a 2d array\n";
cout<<"enter b to find the columnsum of a 2d array\n";
cout<<"enter c to find the sum of two matrixes\n";
cout<<"enter d to find the difference of two matrixes\n";
cout<<"enter e to find the sum of main diagonal elements of a matrix\n";
cout<<"enter f to find the sum of off diagonal elements of a matrix\n";
cout<<"enter g to find the sum of upper triangle elements of a matrix\n";
cout<<"enter h to find the sum of lower triangle elements ot a matrix\n";
cout<<"enter i to find the sum of positive and negative no's of a 2d array separately\n";
cout<<"enter j to find the transpose of a matrix\n";
cout<<"enter k to exit this program\n";



cin>>choice;

switch(choice)
{
case 'a':

cout<<"enter m and n\n";
cin>>m>>n;
cout<<"enter an array\n";
readmatrix(a,m,n);
dispmatrix(a,m,n);
rowsum(a,m,n);
break;
 case 'b':cout<<"enter m and n\n";
 cin>>m>>n;
 cout<<"enter an array\n";
 readmatrix(a,m,n);
 dispmatrix(a,m,n);
 columnsum(a,m,n);
 break;
 case 'c':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
cout<<"sum not possible";
else{
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);

summatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"summatrix";
cout<<"\n";
dispmatrix(C,m,n);
  }
break;


case 'd':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter p and q";
cin>>p>>q;
if((m!=p)&&(n!=q))
cout<<"sum not possible";
else{
cout<<"enter 1st matrix";
readmatrix(A,m,n);
cout<<"enter 2nd matrix";
readmatrix(B,m,n);

diffmatrix(A,m,n,B,C);
cout<<"1st matrix";
dispmatrix(A,m,n);
cout<<"\n";
cout<<"2nd matrix";
dispmatrix(B,p,q);
cout<<"\n";
cout<<"diffmatrix";
cout<<"\n";
dispmatrix(C,m,n);
  }
break;

case 'e':
cout<<"enter m and n";
cin>>m>>n;
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
 sum=smd(a,n);
cout<<"sum ="<<sum;  }
break;


case 'f':
cout<<"enter m and n";
cin>>m>>n;
cout<<"\nenter an array";
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=sod(a,n);
cout<<"sum ="<<sum;  }
break;

case 'g':
cout<<"enter m and n";
cin>>m>>n;
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
cout<<"\nenter an array";
dispmatrix(a,m,n);
sum=sut(a,n);
cout<<"sum ="<<sum;}
break;


case 'h':
cout<<"enter m and n";
cin>>m>>n;
cout<<"\nenter an array";
readmatrix(a,m,n);

if(m!=n)
cout<<"sum not possible";
else{
dispmatrix(a,m,n);
sum=slt(a,n);
cout<<"sum ="<<sum;}
break;

case 'i':
cout<<"enter m and n";
cin>>m>>n;
cout<<"enter an array";
readmatrix(a,m,n);
dispmatrix(a,m,n);
sumposneg(a,m,n);
break;

case 'j':
 cout<<"enter m and n";
 cin>>m>>n;
 cout<<"enter an array";
 readmatrix(a,m,n);
 dispmatrix(a,m,n);
 transpose(a,B,m,n);
 dispmatrix(B,n,m);
 break;
case 'k': exit(0);
 break;

default:cout<<"enter a valid choice\n";
}



cout<<"\nenter y to repeat n to exit";
cin>>ch1;
if(ch1=='n'||ch1=='N')
exit(0);

}
while(ch1=='y'||ch1=='Y');
   getch();
   }

Thursday, February 10, 2011

this is a very very modified menu driven program to find the sum of all the diagonal operations....


#include<iostream.h>
#include<conio.h>
#include<process.h>
int smd(int a[10][10],int n)
{int i,j,sum=0;
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      if(i==j)
        sum+=a[i][j];
          return(sum);
                          }
int sod(int a[10][10], int n)
  {int i,j,sum=0;
       for(i=0;i<n;i++)
         for(j=0;j<n;j++)
            if(i+j==n-1)
                sum+=a[i][j];
                  return(sum);
                                    }
int sut(int a[10][10],int n)
     {int i,j,sum=0;
           for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                if(j>i)
                 sum+=a[i][j];
                    return(sum);
     }
int slt(int a[10][10],int n)
{int i,j,sum=0;
   for(i=0;i<n;i++)
    for(j=0;j<n;j++)
     if(i>j)
       sum+=a[i][j];
          return(sum);
   }

void main()
{
clrscr();
int a[10][10],i,j,n,m,sum,k=0;
char choice,choice1;
cout<<"enter the no:of rows and colums"; endl;
cin>>m>>n;
if(m!=n)
   {cout<<"sum not possible"; endl;
    exit(0);}
    cout<<"enter the matrix"; endl;
for(i=0;i<n;i++)
  for(j=0;j<n;j++)
    cin>>a[i][j];
     cout<<"the matrix is\n";
for(i=0;i<n;i++)
 {for(j=0;j<n;j++)
{cout<<a[i][j];
 cout<<"\t";}
 cout<<"\n"; }

cout<<"choose the operation to be done\n";
cout<<"choose 1 for sum of main diagonal elements\n";
cout<<"choose 2 for sum of off diagonal elements\n";
cout<<"choose 3 for sum of upper triangle elements\n";
cout<<"choose 4 for sum of lower triangle elements\n";
 do{
cin>>choice;
switch (choice)
{case '1':sum=smd(a,n);
break;
case '2':sum=sod(a,n);
break;
case '3':sum=sut(a,n);
break;
case '4':sum=slt(a,n);
break;
default:
cout<<"please enter a choice";

}

cout<<"Therfore the sum obtained by doing the chosen operation is  "<<sum;
cout<<"\npress y  to repeat any other key to exit";
cin>>choice1;
if(choice1!='y')
exit(0);
else
k=1;
}while(k==1);
getch();
}