Showing posts with label cpp source codes. Show all posts
Showing posts with label cpp source codes. Show all posts

Wednesday, 15 January 2014

Extracting even numbers from an array and storing them in another array

#include<iostream.h>
#include<conio.h>
void main()
{
int src[20],des[20];
int n;
   clrscr();
cout<<"Enter the no.of elements in source array..";
cin>>n;
for(int i=0,j=0;i<n;i++)
{
cout<<"enter value:"<<i+1<<"\n";
cin>>src[i];
if(src[i]%2==0)
{
des[j++]=src[i];
}
}
cout<<"Destination array elements are..:";
for(i=0;i<j;i++)
{
cout<<"\n"<<des[i];
}
getch();
}

Sunday, 5 January 2014

An introduction to Classes

->A class is a way to bind data and its associated functions together.
->It allows the data and functions to be hidden from external use.
->While defining a class we create a abstract data type that can be treated as built-in data type
->Generally a class specification has two parts
     1.Class declaration.
     2.Class function definition
->The class declaration defines the type and scope of it's members.
->Class function definition defines how class functions are implemented.

The general form of class declaration is:
class class_name
{
   private:
     variable declarations;
     function declarations;
   public:
     variable declarations;
     function declarations;
};
->The keyword class specifies is an abstract data of type class_name
->The keywords public,private are visibility modes.
->Data hiding(usage of private)is the key feautre of object-oriented programming. 

Function Overloading

//Function overloading refers to use of same thing for different purposes

#include<iostream.h>
#include<conio.h>
int volume(int s)
{
return(s*s*s);
}
double volume(double r,double h)
{
return(3.414*r*r*h);
}
float volume(float l,float b,float h)
{
return(l*b*h);
}
void main()
{
clrscr();
cout<<"volume of cube is:"<<volume(2)<<"\n";
cout<<"volume of cylinder is:"<<volume(3.4,2.1)<<"\n";
cout<<"volume of cuboid is:"<<volume(1.5,2.5,3.5);
getch();
}

Default Arguments

/*c++ allows us to call functions without specifying all its arguments,
In such cases function assings default values to parameters*/
#include<iostream.h>
#include<conio.h>
int sum(int a,int b=10,int c=20)
{
return(a+b+c);
}
int sum(int a=10,int b=20)
{
return(a+b);
}
void main()
{
int a,b,c;
clrscr();
cout<<"Enter the values of three intgers..";
cin>>a>>b>>c;
cout<<"sum1 is:"<<sum(a,b,c)<<"\n";
cout<<"sum2 is:"<<sum()<<"\n";
getch();
}

Saturday, 4 January 2014

Inline functions

//An inline function is a function that replaces function call with corresponding code

#include<iostream.h>
#include<conio.h>
inline int mul(int a,int b)
{
 return(a*b);
}
inline int div(int a,int b)
{
 return(a/b);
}
void main()
{
int a,b;
clrscr();
cout<<"Enter two integers.";
cin>>a>>b;
cout<<"product of a and b is:"<<mul(a,b)<<"\n";
cout<<"quotient of a and b is:"<<div(a,b)<<"\n";
getch();
}

Use of manipulaltors

//manipulators are used to format data display
//Associtivity is from right to left

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
cout<<setw(10)<<"First Name"<<setw(10)<<"Last Name"<<endl;
cout<<setw(10)<<"John"<<setw(10)<<"Pal"<<endl;
cout<<setw(10)<<"Aditya"<<setw(10)<<"Roy"<<endl;
cout<<setw(10)<<"Nathan"<<setw(10)<<"Bracken"<<endl;
getch();
}

Usage of new and delete keywords

//->new and delete keywords are used to assign and free memory dynamically

#include<iostream.h>
#include<conio.h>
void main()
{
int *arr,len;
clrscr();
cout<<"enter the size of array..";
cin>>len;
arr=new int[len];//allocates memory for array arr
for(int i=0;i<len;i++)
{
cout<<"enter array element.."<<i+1<<"\n";
cin>>arr[i];
}
cout<<"array elements are..";
for(i=0;i<len;i++)
{
cout<<"\t"<<arr[i];
}
delete arr;
getch();
}

Friday, 3 January 2014

c++ program to find median of an array

#include<iostream.h>
#include<conio.h>
#define MAX 100
void main()
{
int arr[MAX],n;
static int count;//auto-initialise to zero
clrscr();
cout<<"Enter the size of array:";
cin>>n;
cout<<"Enter the elements of array";
for(int i=0;i<n;i++)
{
cin>>arr[i];
count++;//finds length of  array
}
cout<<"length of array is:"<<count<<"\n";
if(count%2==0)//if length of an array is even
{
 cout<<"Median value is:"<<(arr[(count/2)-1]+arr[(count/2)])/2;
}
else//if length is odd
{
cout<<"Median value is:"<<arr[count/2];
}
getch();
}

Wednesday, 1 January 2014

c++ program to sort an array without using relational operators

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10];//array declaration
clrscr();
cout<<"enter the elements of array..";
for(int i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"before sorting...";
for(i=0;i<10;i++)
{
cout<<"\t"<<a[i];
}
for(i=0;i<10;i++)
{
for(int j=1;j<10;j++)
{
 int c=a[j]-a[j-1];
 if(c-abs(c))
 {
int t=a[j-1];
a[j-1]=a[j];
a[j]=t;
 }
}
}
cout<<"after sorting...";
for(i=0;i<10;i++)
{
cout<<"\t"<<a[i];
}
getch();
}

c++ program for comparing two numbers without using relational operators

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
clrscr();
cout<<"enter any two integers";
cin>>a>>b;
c=a-b;
if(!c)
{
        //if c is zero
cout<<"both are equal";
}
else if(c-abs(c))
 {
/if c returns a -ve value
cout<<"b is greater..";
 }
else
{
//if c returns a +ve value
cout<<"a is greater..";
}
getch();
}

Thursday, 7 November 2013

c++ program for operations on doubly linked lists

#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
node *plink,*slink;
};
struct node *head=NULL;
void insbeg(int key)
{
if(head==NULL)
{
head=new node;
head->data=key;
head->plink=NULL;
head->slink=NULL;
}
else
{
node *p;
p=new node;
p->data=key;
p->slink=head;
p->plink=NULL;
head->plink=p;
head->plink->slink=p;
head=p;
}
}
void insend(int key)
{
if(head==NULL)
{
head=new node;
head->data=key;
head->plink=NULL;
head->slink=NULL;
}
else
{
node *temp=head;
while(temp->slink!=NULL)
{
 temp=temp->slink;
}
node *temp2;
temp2=new node;
temp->slink=temp2;
temp2->slink=NULL;
temp2->plink=temp;
temp2->data=key;
}
}
void del(int key)
{
if(head->data==key)
{
node *temp=head;
head=head->slink;
head->plink=head;
temp->slink=temp->plink=head;
free(temp);
}
else
{
struct node *temp=head;
while(temp->slink->data!=key)
{
temp=temp->slink;
}
struct node *temp2=temp->slink;
temp->slink=temp2->slink;
temp->slink->plink=temp;
temp2->slink=temp2->plink=head;
free(temp2);
}
}
void display()
{
if(head==NULL)
{
cout<<"list empty:";
}
else
{
node *temp=head;
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
temp=temp->slink;
}
}
}

void main()
{
int val,n,z;
clrscr();
do
{
cout<<"1.ins @ beg 2. ins @ end 3.del any node 4.display";
cin>>n;
switch(n)
{
 case 1:cout<<"enter the data to be inserted:";
cin>>val;
insbeg(val);break;
 case 2:cout<<"enter the data to be inserted:";
cin>>val;insend(val);break;
 case 3:cout<<"enter the data to be deleted:";
cin>>val;del(val);break;
 case 4:display();
}
cout<<"1.continue 2.no";
cin>>z;
}while(z!=2);
  getch();
}

c++ program to sort a string using merge sort

#include <iostream.h>
#include<conio.h>
void MergeSortA(int low , int high);
void MergeA(int low ,int mid ,int high);

char currentArray[5];
void main()
{
     clrscr();
            for(int i = 0; i < 5; i++)
 cin >> currentArray[i];

MergeSortA(0,4);

for(int i = 0; i < 5; i++)
 cout << currentArray[i] <<endl;

 getch();
}

void MergeSortA(int low , int high)
{
int mid = 0;
if(low < high)
{
 mid = ((low+high)/2);
 MergeSortA(low , mid);
 MergeSortA(mid+1,high);
 MergeA(low,mid,high);
}
}
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
 char Temp[5];

while(i <= mid && j <= high)
{
 if( currentArray[i] < currentArray[j] )
 {
Temp[k]=currentArray[i];
i++;
 }
 else
 {
Temp[k]=currentArray[j];
j++;
 }
 k++;
}
if(i > mid )
{
 for(int h = j ;h <= high ; h++ )
 {
Temp[k]=currentArray[h];
k++;
 }
}
else
 for(int h = i; h<= mid ; h++ )
 {
Temp[k]=currentArray[h];
k++;
 }
for(int i = 0; i <= high ; i++)
{
currentArray[i]=assign(Temp[i]);
}
}

Saturday, 12 October 2013

c program to check if given expression is correctly parenthesized

#include<stdio.h>
#include<conio.h>
int top=-1,st[20];
void push(char c)
{
st[++top]=c;
}
void pop()
{
top=top-1;
}
void caltop()
{
if(top==-1)
{
printf("\n\n expression is valid");
}
else
{
printf("expression is invalid");
}
}

void main()
{
int i,a[20];
clrscr();
printf("\n enter the expression");
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==')')
push(a[i]);
else if(a[i]==')')
pop();
}
caltop();
getch();
}

Tuesday, 1 October 2013

c++ program to search an element in an array using binary search

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[100],first=0,last=0,middle=0,n,search;
clrscr();
cout<<"enter the size of array";
cin>>n;
cout<<"enter the elements in ascending order";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"\n"<<"enter the element to be searched";
cin>>search;
first=0;last=n-1;middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<search)
first=middle+1;
else if(arr[middle]==search)
{
cout<<"element found at position:"<<middle+1;
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
getch();
}

c++ program to find LCM of given two numbers

#include<iostream.h>
#include<conio.h>
void main()
{
int u,v,t,l;
clrscr();
cout<<"enter any two integers";
cin>>u>>v;
l=(u*v);
while(v!=0)
{
t=u%v;
u=v;
v=t;
}
l=l/u;
cout<<"\n"<<"lcm of two numbers is:"<<l;
getch();
}

Sunday, 8 September 2013

c++ program for implementation of stack operations using arrays

#include<iostream.h>
STACK OPERATIONS
#include<conio.h>
class stack
{
int top,st[20];
public:
stack()
{
top=-1;
}
void push(int a)
{
st[++top]=a;
}
int pop()
{
return st[top--];
}
void display();
};
void stack::display()           //inline function
{
 for(int i=top;i>=0;i--)
 {
 cout<<"\n"<<st[i];
 }
}
void main()
{
 stack s;
 int a,z,data,x;
 clrscr();
 do
 {
 cout<<"1.push 2.pop 3.display:";
 cin>>a;
 switch(a)
 {
 case 1:cout<<"enter the data to push.";
cin>>data;
s.push(data);break;
 case 2:x=s.pop();
 cout<<"popped element is:"<<x;break;
 case 3:s.display();
 }
 cout<<"continue 1.yes 2.no";
 cin>>z;
 }while(z!=2);
 getch();
}

c++ program to convert given sentence in uppercase to lowercase alphabets

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
       char caps[50],c;
      int z;
       clrscr();
      cout<<"enter a sentence in uppercase..";
      cin.get(caps,50);
      l=strlen(caps);
      cout<<"your string length is.."<<l<<"\n";
      for(int i=0;i<l;i++)
      {
        z=caps[i];
        z=z+32;
        c=z;
        cout<<c;
      }
      getch();
}


sample input and output:enter a sentence in uppercase..
input:HELLO.
output:your string length is 5.
hello.

Saturday, 7 September 2013

c++ program to find second largest number in a given array

#include<iostream.h>
#include<conio.h>
void main()
{
int n,j,firstlarge,secondlarge,num[100];
clrscr();
cout<<"enter the size of array..";
cin>>n;
cout<<"enter the elements of array.";
for(int i=0;i<n;i++)
{
cin>>num[i];
}
firstlarge=num[0];
for(i=0;i<n;i++)
{
if(firstlarge<num[i])
firstlarge=num[i];
j=i;
}
secondlarge=num[n-j-1];
for(i=0;i<n;i++)
{
if(secondlarge<num[i] && j!=i)
secondlarge=num[i];
}
cout<<"second largest no. is .."<<secondlarge;
getch();
}

sample input and output:enter the size:10
enter the array elements:10,20,30,40,50,40,80,100,90,70.
second largest no. is..90


Friday, 6 September 2013

a simple c++ program to find whether or not a given no. is prime

#include<iostream.h>
#include<conio.h>
void main()
{
       int n,count=0;
       cin>>n;
       for(int i=1;i<=n;i++)
       {
         if(n%i==0)
          count++;
        }
        if(count==2)
        {
         cout<<"the no. you entered is prime..";
        }
         else
        {
         cout<<"not a prime....";
         }
          getch();