Wednesday, 13 September 2017

Basic Implementation of Array as a Pointer

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int arr[5];
  cout<<"Enter 5 nos. ";
  for(int i=0;i<5;i++)
     { cin>>*(arr+i);
      }
  for(i=0;i<5;i++)
     { cout<<*(arr+i);
     }
     getch();
}

Saturday, 26 August 2017

Program to Read and Write multiple objects in a Binary file

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

struct book
{ char name[30];
  char pname[30];
  long copsold;
};

void main()
{ clrscr();
  book b[10];
  fstream abc;
  abc.open("tanuj.dat",ios::binary,ios::noreplace);
  for(int i=0;i<10;i++)
     {
       cout<<"Enter book name ";
       gets(b[i].name);
       cout<<"Enter publisher's name ";
       gets(b[i].pname);
       cout<<"Ente rno. of copies sold ";
       cin>>b[i].copsold;
       abc.write((char*)&b[i],sizeof(b[i]));
     }
abc.close();
  abc.open("tanuj.dat",ios::binary,ios::in);
  for(i=0;i<10;i++)
  { abc.read((char*)&b[i],sizeof(b[i]));
    puts(b[i].name);
    puts(b[i].pname);
    cout<<b[i].copsold;
  }
  abc.close();
  getch();
}

Sunday, 20 August 2017

Program to Calculate no. of words starting with 'a' or 'A' in a File

#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{ clrscr();
  int count=0,wr=0;
  char str[100];
  char word[20];
  ofstream obj;
  obj.open("tanuj",ios::out);                                         // for writing purpose
  cout<<"Enter anything (100 word limit) ";
  gets(str);
  obj<<str;
  obj.close();
  ifstream abc;
  abc.open("tanuj",ios::in);                                             // for reading purpose
  if(!abc)
    { cout<<"File not found";
      exit(0);
    }
  cout<<"The content in it was ";
  puts(str);
  while(!abc.eof())
       { abc.getline(word,20,' ');
if(word[0]=='a'||word[0]=='A')
  { wr++;
    count++;
  }
else wr++;
       }
  cout<<"\n Total no. of words is "<<wr;
  cout<<"\n Total no. of words starting with a or A is "<<count;
  cout<<"\n Total no. of spaces is "<<wr-1;
  abc.close();
  getch();
}

Program to create a File and write data in it .(FILE HANDLING)

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{ clrscr();
  char word;
  ofstream obj;
  obj.open("tanuj.txt");  
  cout<<"Enter name \n";
  do
    { word=getche();
      obj<<word;
    } while(word!=';');
  cout<<"Thankyou for entering data ";
  obj.close();
  getch();
}

Sunday, 9 July 2017

HW Program 1

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class student
 { private:
  int id;
  char name[30];
  int marks[5];
  int avg;
   public:
  student();
  student(student &si);
  void readstudent();
  void calcavg();
  void display();
 };

void student::student()         //Default Constructor (Outline)
 { id=0;
   strcpy(name,"AAA");
   for(int i=0;i<5;i++)
      { marks[i]=0;
      }
   avg=0;
 }

void student::student(student &si) // Copy constructor (outlined)
 { id=si.id;
   strcpy(name,si.name);
   for(int i=0;i<5;i++)
      { marks[i]=si.marks[i];
      }
   avg=si.avg;
 }

void student::readstudent()
 { cout<<"Enter Student ID ";
   cin>>id;
   cout<<"Enter Student name ";
   gets(name);
   cout<<"Enter students' marks in 5 subjects ";
   for(int i=0;i<5;i++)
      { cin>>marks[i];
      }
 }

void student::calcavg()
 { for(int i=0;i<5;i++)
      { avg+=marks[i];
      }
   avg=avg/5;
 }

void student::display()
     { cout<<"Name :";
       puts(name);
       cout<<"Id : \t "<<id;
       cout<<"\n Average marks = "<<avg;
     }
void main()
{ clrscr();
  student s1,s2;
  s1.readstudent();
  s1.calcavg();
  student(s2)=s1;
  s1.display();
  s2.display();
  getch();
}

Program for a "Movie Counter"

#include<iostream.h>
#include<conio.h>
class movcount
{ private:
 int tp;
 int tpass;
 long tamount;
 int tticket;
  public:
 void counter();
 void showdetail();

};
void movcount::counter()
{ char x;
  int count=0,pass=0;
  long amount=0;
  cout<<"Enter 't' ( with ticket ) \n 'p' ( with pass ) \n Or any other key to Calculate ";
  start:
  cin>>x;
  if (x=='t'||x=='p')
     { if (x=='t')
 { amount=amount+250;
   count++;
 }
       else if (x=='p')
 { count++;
   pass++;
 }
       goto start;
     }
  else
     { tamount=amount;
       tp=count;
       tpass=pass;
       tticket=count-pass;
     }
}
void movcount::showdetail()
{ cout<<"Total number of people visited = "<<tp<<"\n";
  cout<<"Total amount collected = "<<tamount<<"\n";
  cout<<"Total number of people with pass = "<<tpass<<"\n";
  cout<<"Total number of people with tickets ="<<tticket;
}
void main()
{ clrscr();
  movcount m1;
  m1.counter();
  m1.showdetail();
  getch();
}

Sunday, 22 January 2017

To search any element in array

#include<conio.h>
#include<iostream.h>
# define r 10
void main()
{ clrscr();
  int a[r];
  int w,x,y,z;
  for(int i=0;i<r;i++)
     { cin>>a[i];
     }
  cout<<"which element do you want to search ? ";
  cin>>x;
  for(i=0;i<r;i++)
     { if (x==a[i])
 { cout<<x<<" is present in the Array ";
   y=sizeof(x);
   cout<<" Size of "<<x<<" is "<<y<<"\n";
   cout<<"Index of "<<x<<" is "<<i<<"\n";

 }
    }
  getch();
}

To count no. of even and odd elements in an Array

#include<conio.h>
#include<iostream.h>
# define r 10
void main()
{ clrscr();
  int a[r];
  int b=0,c=0;
  for (int i=0;i<r;i++)
      { cin>>a[i];
if(a[i]%2==0)
 { b++;
 }
else { c++;
    }
      }
  cout<<"Number of odd elements = "<<c<<"\n";
  cout<<"Number of even elements = "<<b;
  getch();
}

To print double of each element in an Array

#include<conio.h>
#include<iostream.h>
# define r 3
void main()
{ clrscr();
  int a[r];
  for (int i=0;i<r;i++)
      { cin>>a[i];
      }
  for (i=0;i<r;i++)
      { cout<<2*a[i]<<" - ";
      }
  getch();
}

To find sum of elements of array

#include<conio.h>
#include<iostream.h>
# define R 3
void main()
{ clrscr();
  int a[R];
  int sum=0;
  for(int i=0;i<R;i++)
     { cin>>a[i];
       sum+=a[i];
     }
  cout<<"Sum = "<<sum;
  getch();
}

To print reverse of an Array

#include<iostream.h>
#include<conio.h>
# define R 5
void main()
{ clrscr();
  int a[R];
  int i;
  for(i=0;i<R;i++)
     { cin>>a[i];
     }
  for(i=R-1;i>=0;i--)
     { cout<<a[i];
     }
  getch();
}

Basic Implementation of Array as a Pointer

#include<iostream.h> #include<conio.h> void main() { clrscr();   int arr[5];   cout<<"Enter 5 nos. ";   f...