Saturday, October 30, 2010

18.Program to demonstrate reversing of an input text using a function with string arguments.

// Program  to demonstrate reversing of  an input text
// using a function with string arguments.


#include <stdio.h>
#include <iostream.h>
#include<conio.h>
/* strlen - return length of string s
 */
int strlen(char s[])
    {
    int i;
    for (i = 0; s[i] != '\0'; ++i);
    return (i);
    }

/* reverse - reverses order of characters in a string
 */
void reverse(char s[])
    {
    char t;
    short i, j;

    for (i = 0, j = strlen(s) - 1; i < j; ++i, --j)
t = s[i], s[i] = s[j], s[j] = t;
    }

main()
    {
    char line[BUFSIZ];  /* the line of input text */

    cout<<endl;
    cout<<"Input String to obtain in reverse");
    cin.getline(line,BUFSIZ);
    reverse(line);
    cout<<endl;
    cout<<line;
    getch();
}

Friday, October 22, 2010

17.Matrices : To implement cell value of a defined matrix

// Program for  : Matrices : To implement cell value of a defined matrix
// Input: Row & Col. and Output : Cell element of the points Row, Col.

#include <stdlib.h>
#include <iostream.h>
#include<conio.h>
int main()
{
clrscr();
  int x, y;                    // Loop Counter
  int x1, y1, repeat;
  //         x, y           Think like that if you input x = 1, y = 3
  //                        You will output "69", that how a matrix works
  int matrix[4][4] = { 12, 4, 23, 6,
      45, 78, 3, 57,
      69, 23, 9, 2,
      34, 11, 5, 99 };
  cout<<"My Matrix: "<< endl << endl;
  for( x = 0; x < 4; x++)
  {
       for( y = 0; y < 4; y++)
       {
   cout<< matrix[x][y] << " ";
       }
       cout<<endl;
  }
  cout<< endl <<"Both x and y have a max point of 3" << endl;
  do
  {
       cout<< endl <<"Enter in x and y points[ x <space> y ]: ";
       cin>> x1 >> y1;

       cout<<"Point ("<< x1 <<", " << y1 <<") = " << matrix[x1][y1] << endl
  << endl;
       cout<<"Would You like Output new points (0 to quit)? ";
       cin>> repeat;
  }while( repeat != 0);
  cout<<endl;
  cout<<"Press any key to continue";
  getch();
  return 0;
}

16.Program to Calculate Polynomial. Loading the libraries.

// Program to  Calculate Polynoms

//Loading the libraries
#include <iostream.h>
#include <math.h>
#include<conio.h>
//Defining variables
int result=0; //here will be stored the result
int *arr; //here will be stored the numbers
int power; //this is the maximum power
int x; //the value for "x"

//Beginning the main function
void main()
{
 cout << "What s the maximum power in the polynom? : ";
 cin >> power;
 cout << endl;

 arr=new int[power]; //creates an array with "power" cells
 
 //Asking the user to enter the values...
 for(int n=0;n<power+1;n++)
 {
  cout <<"A"<<power-n<<" = ";
  cin >> arr[power-n];
 }

 cout << "The polynom is: ";

 //Prints the polynom on the screen
 for(int go=power; go >-1; go--)
 {
  if(go > 0)
   cout <<arr[go]<<"*x^"<<go<<" + ";
  else
   cout <<arr[go]<<"*x^"<<go;
 }
 cout <<endl;
 cout <<"Enter value for X : ";
 cin >> x;

 //Calculating the polynom
 for(int cal=0; cal < power+1; cal++)
 {
  result=result+(arr[cal]*(pow(x,cal)));
 }

 cout <<" Y(x)= "<< result<<endl; // showing the result
 getch();
}

15.Program to display Fibonaccis Heap

// Program to display  Fibonaccis Heap

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

void main()
{
clrscr();
   unsigned int to,a,n,n1,n2,sum;

   cout << "Enter the limit: n=";
   cin >> to;
   to--;

   n1=n2=1;
   a=2;
   sum=2;

   cout << "Fibonacci's numbers: " << endl;
   cout << "1 1 ";

   while(a<=to)
   {
      n=n1+n2;
      cout << n << " ";
      n1=n2; n2=n;
      sum=sum+n;
      a++;
   }

   cout << endl << endl ;
   cout << "Sum of all numbers= "<< sum << endl;
   getch();
}

Thursday, October 21, 2010

14.Declaration of a Class and manipulation of member function. Display of the result of students with computed grade of marks.

//  Declaration of a Class and manipulation of member function.
//  Display of the result of students with computed grade of marks.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
class result // Declaration of the Class
{
private:
int serialno;
int rollnum;
int total;
int percent;
public:
void inputdata(int ser,int rol,int tot,int per)
{
serialno = ser;
rollnum = rol;
total = tot;
percent = per;
}
void display(void)
{
clrscr();
gotoxy(30,5);
cout<<"R E S U L T ";
cout<<endl;
// Music creation through sound command
for(double d=0;d<=800;d++)
{sound (d);
delay(5);  }
nosound();
// Sound command de-activated
char grade[20];
cout<<endl<<"The serial number is "<<serialno;
cout<<endl<<"The roll number is "<<rollnum;
cout<<endl<<"The total marks is "<<total;
cout<<endl<<"The percent marks is "<<percent;
if (percent<40) strcpy(grade,"Fail");
if (percent>=40 && percent<60) strcpy(grade,"Pass");
if (percent>=60 && percent<80) strcpy(grade,"First");
if (percent>=80) strcpy(grade,"Excellent");
cout<<endl<<"The grade obtained is "<<grade;
cout<<endl;
}
};
main()
{
result studrecord;
int ser;
int rol;
int tot;
int per;
clrscr();
cout<<"Enter the serial number"<<endl;
cin>>ser;
cout<<"Enter the rollnumber"<<endl;
cin>>rol;
cout<<"Enter the total"<<endl;
cin>>tot;
per=tot/5;
clrscr();
studrecord.inputdata(ser,rol,tot,per);
studrecord.display();
cout<<endl;
cout<<"Press any key to return ";
getch();
clrscr();// Screen cleared for the next program output.
return 0;
 }

13.Program to depict the usage of Binary Search Technique.

// Program to depict the usage of Binary Search Technique.
// It is assumed that the list of numbers in an array is
// stored in ascending order.

#include<iostream.h>
#include<conio.h>
main()
{
int num[10],m,min,max,mid,pp;
clrscr();
cout<<"Enter any ten numbers as elements ofan array in";
 cout<<" ascending order "<<endl;
for (int i=0;i<10;i++)
{
cin>>num[i];
}
cout<<endl<<"Enter number to search ";
cin>>m;
cout<<endl;
min = 0;
max = 9;
pp = -1;
while ((min<= max)&&(pp == -1))
{
mid = (min+max)/2;
if (num[mid] == m)
pp = mid;
else
if (num[mid]<m)
min = mid + 1;
else
max = mid - 1;
}
if (pp>-1)
cout<<"The element "<<m<<" lies in the array"<<" at "<<pp+1<<" position";
else
cout<<"The element does not lie in the array";
cout<<endl;
cout<<"Program over....press any key to continue";
getch();
return 0;
}

Friday, October 15, 2010

ADD if you like bye this product

12.Program to input any string upto 500 characters and convert the same into binary

// Program to input any string upto 500 characters
// and convert the same into binary
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

char *entry, letter, choice[2];
int ascii, len, binary[8], total;
void prog();

int main()
{
      clrscr();
      prog();
      return 0;
}

void prog()
{
   entry = new char[501];
   cout<<"Enter string to convert (up to 500 chars): ";
   cin.getline(entry, 500);
   len = strlen(entry);  /* get the number of characters in entry. */
   for(int i = 0; i<len; i++)
   {
      total = 0;
      letter = entry[i]; /* store the first letter */
      ascii = letter;    /* put that letter into an int, so we can
   see its ASCII number */
      while(ascii>0) /* This while loop converts the ASCII # into binary,
stores it backwards into the binary array. */
      {
if((ascii%2)==0)
{
   binary[total] = 0;
   ascii = ascii/2;
   total++; /* increasing by one each time will yeild the
number of numbers in the array. */
}
else
{
   binary[total] = 1;
   ascii = ascii/2;
   total++;
}
      }
      total--;
      while(total>=0)
      {
cout<<binary[total];
total--;
      }
   }
   delete[] entry; /* free up the memory used by entry */
   cout<<endl<<"Do again(1 = yes, 2= no)?: ";
   cin.getline(choice,3);
   if(choice[0] == '1')
      prog();
   else
      exit(0); /* quits the program */
}

11.Program to define a class point and implement operations

// Program to define a class point and implement operations

#include<iostream.h>
#include<math.h>
#include<conio.h>
class Point{
public:
     Point();
void setPoint(float,float);
void printPoint();
float getx();
float gety();

private:
   float x;
   float y;

      };
  Point::Point(){x=0.0;y=0.0;}
void Point::setPoint(float a,float b)
  {x=a;y=b;}
void Point::printPoint()
     {
     cout<<'('<<x<<","<<y<<')';
     }
float Point::getx(){return x;}
float Point::gety(){return y ;}

  void main()
{
clrscr();
Point p1,p2;
char ch;

cout<<"Initially,the points are :";
  p1.printPoint();

cout<<",";

  p2.printPoint();
cout<<endl;
p1.setPoint(3.5,4.5);
p2.setPoint(7.5,9.5);

  cout<<"After setting the points are:";

p1.printPoint();
cout<<",";
p2.printPoint();
cout<<endl;
  float d=0.0;
d=sqrt((p2.getx()-p1.getx())*(p2.getx()-p1.getx())+(p2.gety()-p1.gety())*(p2.gety()-p1.gety()));
cout<<"d="<<d<<endl;

cout<<"Enter any key to continue";
getch();
}

Thursday, October 14, 2010

10. Program to get the day the month starts on, and the number of days in the month to output a calendar.

// Program to get the day the month starts on, and
//the number of days in the month to output a calendar.

//-------------------Includes--------------------
#include <iostream.h>
#include<conio.h>

//--------------Function Prototypes--------------
int ShowDateLine(int day, int EndDay, int BoxWidth);
void ShowRestOfBox(int BoxWidth, int BoxHeight);
void ShowDates(int BoxWidth);
void Divider(int BoxWidth);

//---------------------Main----------------------
int main()
{
    int StartDay, EndDay, day, counter, weeks;
    const int BoxWidth = 8, BoxHeight = 3;
    char end;
    cout << "1. Sun  2. Mon  3. Tue  4. Wed " << endl;
    cout << "5. Thu  6. Fri  7. Sat" << endl << endl;
    do
    {
        cout << "Enter the starting day: ";
        cin >> StartDay;
        if ((StartDay < 1) || (StartDay > 7))
        {
            cout << "\aError, must be between 1 and 7.\n";
}
    }
    while ((StartDay < 1) || (StartDay > 7)); /* Error checking */
    day = StartDay * -1 + 2;
    cout << "Enter the amount of days in the month: ";
    cin >> EndDay;
    cout << endl;
    weeks = (EndDay + StartDay - 2) / 7;
weeks++;
    //----------------Draw Calendar------------------
    ShowDates(BoxWidth);
    Divider(BoxWidth);
    for (counter = 0; counter < weeks; counter++)
    {
        day = ShowDateLine(day, EndDay, BoxWidth);
        ShowRestOfBox(BoxWidth, BoxHeight);
        Divider(BoxWidth);
    }
    cout<<"Press any key to continue"<<endl;
getch();
}

//------------------Functions--------------------
int ShowDateLine(int day, int EndDay, int BoxWidth)
/* Outputs a '*' then it outputs the days till it reaches the 'EndWeek' value.
If day reaches the EndDay value, then it will output a space instead of a day.
After each date it will output a '*', then proceed 'BoxWidth' spaces ahead. */
{
    int counter, EndWeek;
    cout.setf(ios::right);
    cout << "*";
    EndWeek = day + 7;
    for (counter = 0; counter < 7; counter++, day++)
    {
        cout.width(BoxWidth - 1);
        if ((day < 1) || (day > EndDay))
        {
            cout << " ";
        }
        else
        {
            cout << day;
        }
        cout << "*";
    }
    cout << endl;

    return day;
}
//-----------------------------------------------
void ShowRestOfBox(int BoxWidth, int BoxHeight)
/* Outputs a '*' every 'BoxWidth' length, for a total of 8 '*'.
It does that 'BoxHeight' times. */
{
    int countera, counterb;
    cout.setf(ios::right);
    for (countera = 1; countera < BoxHeight + 0; countera++)
    {
        cout << "*";
        for (counterb = 0; counterb < 7; counterb++)
        {
             cout.width(BoxWidth);
             cout << "*";
        }
        cout << endl;
    }
}
//-----------------------------------------------
void ShowDates(int BoxWidth)
/* Outputs the all 7 days of the week. It takes the 'BoxWidth' and outputs the
day in the
middle of where the box will be. */
{
  cout.setf(ios::right);
  cout.width(BoxWidth / 2 + 2);
  cout << "Sun";
  cout.width(BoxWidth);
  cout << "Mon";
  cout.width(BoxWidth);
  cout << "Tue";
  cout.width(BoxWidth);
  cout << "Wed";
  cout.width(BoxWidth);
  cout << "Thu";
  cout.width(BoxWidth);
  cout << "Fri";
  cout.width(BoxWidth);
  cout << "Sat" << endl;
}
//-----------------------------------------------
void Divider(int BoxWidth)
//Outputs a bar of '*' 'BoxWidth' * 7 + 1
{
    int length, counter;
    length = BoxWidth * 7 + 1;
    for (counter = 0; counter < length; counter++)
    {
        cout << "*";
    }
    cout << endl;
}

9. Program to print the corresponding ASCII character

// Program to print the  corresponding ASCII character

#include <stdio.h>
#include <fstream.h>
#include <stdlib.h>
#include<conio.h>
main()
    {
    clrscr();
    char line[512]; /* input string */
    char sym;       /* ASCII code number */
    cout<<"Please enter the number to be factored: ";
    cin.getline(line, 512);
    sym = atoi(line);
    cout<<int(sym)<<"  is the ASCII code for ";
    if (sym == 0)
cout<<"null\n";
    else if (sym == ' ')
cout<<"blank\n";
    else if (sym == 127)
cout<<"del\n";
    else if (sym >=  1 && sym <=  31)
cout<<"CONTROL-\n"<< sym + 64;
    else if (sym >= '0' && sym <=  '9')
cout<<"digit \n"<< sym;
    else if (sym >= 'A' && sym <=  'Z')
cout<<"UPPERCASE \n"<< sym;
    else if (sym >= 'a' && sym <= 'z')
cout<<"lowercase \n"<< sym;
    else
cout<<"punctuation mark \n"<< sym;
getch();
    }

// Program to input a string and print the character with the maximum frequency in the string.

// Program to input a string and print the character with the
// maximum frequency in the string.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
main()
{
clrscr();
char name[40],ans;
int l=0,count=0,max=0;
cout<<"Enter any word "<<endl;
gets(name);
l = strlen(name);
cout<<endl;
for(int a=0;a<l;a++)
{
for (int b=0;b<l;b++)
{
if (name[a]==name[b] && name[a]!=' ')
count = count+1;
}
if (max<count)
{
max=count;
ans=name[a];
}
count=0;}
cout<<endl<<"The character with maximum frequency is ";
cout<<ans<<" with occurring "<<max<<endl;
getch();
return 0;
}

Tuesday, October 12, 2010

7.This C++ program displays the month

// This C++ program displays the month
// of any year and also has controls to view the
// previous & the Next  months.

# include <iostream.h>
# include <stdio.h>
# include <conio.h>
void main()
{
    clrscr();
    int b1,b2,o;
  cout<<"  ENTER THE MONTH  : ";
cin>>b1;
cout<<"  ENTER THE YEAR  : ";
cin>>b2;
a:cout<<"\n";
     o = b1-1;
    int
i,j,k=0,l=0,m=0,n=0,b3,c=0,c1,c2,c3,a[12]={31,28,31,30,31,30,31,31,30
,31,30,31};
    int ab[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
    char *z[12] =
{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
char choice;
    for (o=0;o<=11;o++)
    {
     if (b1 == ab[o])
     {
      cout<<"\n         "<<z[o]; cout<<"       "<<b2;
     }
    }
if(b2%4==0)
a[1] = 29;
    else
        a[1] = 28;
if(b2>=2000)
b3=b2-2000;
else
{
b3=2000-b2;
b3*=2;
}
if(b1==12)
n=12;
if(b1==1)
{
k=11;
l=1;
for(i=0;i<=k;i++)
m+=a[i];
}
for(i=0;i<(b1-1);i++)
c+=a[i];

   c2=(c+b3)%7;
   textcolor(10);
cout<<"\n\n\n\n";
cout<<"  SU  MO  TU  WE  TH  FR  SA\n\n";
for(j=0;j<c2;j++)
cout<<"    ";
for(j=1;j<=a[b1-1];j++)
{
if(j<10)
cout<<"   "<<j;
else
cout<<"  "<<j;
if((j+c2)%7==0)

cout<<"\n\n";
}
cout<<"\n";


    cout<<"\n\n Use these arrow keys to display the Sequential Year or Month :   \n";
    cout<<"**************************************************************\n";
    cout<<" |     Up    = Next Year  |   Down = Previous Year   | \n";
    cout<<" |     Right = Next Month   |   Left = Next Month   | \n";
    cout<<"************************************************************** \n";
    choice = getch();
    if (choice==0)
    {
     choice = getch();
     switch(choice)
     {
      case 80  : {b2-=1; goto a; break;}
      case 72  : {b2+=1;goto a; break;}
      case 75 :  {
 if (b1!=1)
                    b1-=1;
                  else
                   {b1=12; b2-=1;}
                 goto a; break;}
      case 77 : {
                 if (b1!=12)
                   b1+=1;
                 else
                   {b1 = 1; b2+=1;}
                 goto a; break;}
     }
    }
 getch();
}