Sunday 16 December 2012

C++ PROGRAMMS

TO SPECIFY STRING FUNCTION
#include <iostream.h>
#include <string.h>

using namespace std;

int main()
{
Get_Name(string); // Get's your name.
system("Pause");
return 0;
}

String Get_Name(string name); // String to get user's name.
{
string name;
cout << "What is your name?" << endl;
cin >> name;
return name;
}
Using a Union
A C++ union is a limited form of the class type. It can contain access specifiers (public, protected, private), member data, and member functions, including constructors and destructors. It cannot contain virtual functions or static data members. It cannot be used as a base class, nor can it have base classes. Default access of members in a union is public.
A C union type can contain only data members.
In C, you must use the union keyword to declare a union variable. In C++, the union keyword is unnecessary:


A database
A database is a collection of information that is organized so that it can easily be accessed, managed, and updated. In one view, databases can be classified according to types of content: bibliographic, full-text, numeric, and images.
In computing, databases are sometimes classified according to their organizational approach. The most prevalent approach is the relational database, a tabular database in which data is defined so that it can be reorganized and accessed in a number of different ways. A distributed database is one that can be dispersed or replicated among different points in a network. An object-oriented programming database is one that is congruent with the data defined in object classes and subclasses.
Computer databases typically contain aggregations of data records or files, such as sales transactions, product catalogs and inventories, and customer profiles. Typically, a database manager provides users the capabilities of controlling read/write access, specifying report generation, and analyzing usage. Databases and database managers are prevalent in large mainframe systems, but are also present in smaller distributed workstation and mid-range systems such as the AS/400 and on personal computers. SQL (Structured Query Language) is a standard language for making interactive queries from and updating a database such as IBM's DB2, Microsoft's SQL Server, and database products from Oracle, Sybase, and Computer Associates.
A database Normalization

Normalization is the process of efficiently organizing data in a database. There are two goals of the normalization process: eliminating redundant data (for example, storing the same data in more than one table) and ensuring data dependencies make sense (only storing related data in a table). Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored.

/* Write a C++ Data structure program to perform the following operations:
a) Insert an element into a binary search tree.
b) Delete an element from a binary search tree.
c) for a key element in a binary search tree. */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;

main()
{
            int ch,y;
            for(i=1;i<40;i++)
            tree[i]=-1;
            while(1)
            {
cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:";
                        cin >> ch;
                        switch(ch)
                        {
                        case 1:
                                     cout <<"enter the element to insert";
                                     cin >> ch;
                                     insert(1,ch);
                                     break;
                        case 2:
                                     cout <<"enter the element to delete";
                                     cin >>x;
                                     y=search(1);
                                     if(y!=-1) delte(y);
                                     else cout<<"no such element in tree";
                                     break;
                        case 3:
                                     display(1);
                                     cout<<"\n";
                                     for(int i=0;i<=32;i++)
                                     cout <<i;
                                     cout <<"\n";
                                     break;
case 4:
                                     cout <<"enter the element to search:";
                                     cin >> x;
                                     y=search(1);
                                     if(y == -1) cout <<"no such element in tree";
                                     else cout <<x << "is in" <<y <<"position";
                                     break;
                        case 5:
                                     exit(0);
                        }
            }
}

void insert(int s,int ch )
{
            int x;
            if(t==1)
            {
                        tree[t++]=ch;
                        return;
            }
            x=search1(s,ch);
            if(tree[x]>ch)
                        tree[2*x]=ch;
            else
                        tree[2*x+1]=ch;
            t++;
}
void delte(int x)
{
            if( tree[2*x]==-1 && tree[2*x+1]==-1)
                        tree[x]=-1;
            else if(tree[2*x]==-1)
                  {    tree[x]=tree[2*x+1];
                        tree[2*x+1]=-1;
                  }
            else if(tree[2*x+1]==-1)
                  {    tree[x]=tree[2*x];
                        tree[2*x]=-1;
                  }
            else
            {
              tree[x]=tree[2*x];
              delte(2*x);
            }
            t--;
}

int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}

void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}

int search1(int s,int ch)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}

OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:3
no element in tree:
0123456789011121314151617181920212223242526272829303132
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:1
Enter the element to insert 10
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:4
Enter the element to search: 10
10 is in 1 position
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:5
C++ Selection Sort


#include <iostream>

using namespace std;

/*
* Program to read a list of integers from standard input and print them in
* sorted order.  This uses a simple selection sort.
*
* Author: Tom Bennet
*/

/*
* Swap function for sorting.
*/
void swap(int *a, int *b)
{
        int tmp;                                /* Exchange temp value. */

        tmp = *a;
        *a = *b;
        *b = tmp;     
}

/*
* Sort the array.  It receives a pointer to the start of the data and the
* number of items.
*/
void sort(int *data, int size)
{
        int minloc;                     /* Loc of minimum. */

        for(minloc = 0; minloc < size - 1; ++minloc) {
                int scan;
                for(scan = minloc + 1; scan < size; ++scan)
                        if(data[minloc] > data[scan])
                                swap(&data[minloc], &data[scan]);
        }
}

/*
* Main program.  Reads the integers into an array, sorts them, and
* prints them out.
*/
const int MAX_NUM_INTS = 100;
int main()
{
        int ints[MAX_NUM_INTS];         // Where the numbers go.
               
        // Read them in.
        int i;
        for(i = 0; i < MAX_NUM_INTS && cin >> ints[i]; ++i);
        int numints = i;

        // Sort them.
        sort(ints, numints);

        // Print them.
        cout << "==================" << endl;
        for(int i = 0; i < numints; ++i)
                cout << ints[i] << endl;
        cout << "==================" << endl;
}
To convert a decimal number into binary
#include <iostream>
#include <conio .h>
using namespace std;
void main()
{
            int dec,rem,i=1,sum=0;
            cout< <"Enter the decimal to be converted:"<<endl;
            cin>>dec;
            do
            {
                        rem=dec%2;
                        sum=sum + (i*rem);
                        dec=dec/2;
                        i=i*10;
            }while(dec>0);
            cout< <"The binary of the given number is:"<<sum<<endl;
            getch();
} 
 
 
Program to read a file and count blank space, characters,numbers & speical char
 
/* to read a file and count blank space, characters,numbers & speical char */

#include

main()

{ char filename[20],ch;

FILE *fp;

int x=0,y=0,z=0,p=0,q=0;

clrscr();

printf("\nEnter the name of the file");

scanf("%s",filename);

fp=fopen(filename,"r");

if(fp==NULL)

{ printf("\nError in reading");

getch();

return;

}

while((ch=fgetc(fp))!=EOF)

{ if(ch==' ')

x=x+1;

else if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))

y=y+1;

else if(ch>=0 && ch<=9)

z=z+1;

else if(ch=='\n' || ch=='\r' || ch=='\t')

p=p+1;

else

q=q+1;

}

fclose(fp);

printf("\nBlank space=%d",x);

printf("\nAlphabets =%d",y);

printf("\nNumbers =%d",z);

printf("\nWhite spaces=%d",p);

printf("\nSpecial char=%d",q);

getch();

}

No comments:

Post a Comment