Sunday, 27 January 2013

Write a program to insert, traverse, sum of all integer numbers, sum of all even numbers and sum of all odd numbers in array


#include <iostream.h>
#include<conio.h>
int main()
{
            int i,n, sum=0,even=0 ,odd=0;
           int a[20] ;
            cout<<"How many elemnt do you want:--"<<endl;
            cin>>n;

              for (i=0; i<n; i++)
            {
             cout<<"Enter the value in array:-"<<endl;
             cin>>a[i];
            }
          cout<<"Element is:--"<<endl;
            for (i=0; i<n ;i++)
            {
                        cout<<a[i]<<endl;
            }
            cout<<"sum of all integer numbers:-"<<endl ;

            for (i=0; i<n ;i++)
            {
            sum=sum+a[i];
            }
            cout<<sum<<endl;
            for (i=0; i<n ;i++)
            {
            if(a[i]%2==0)
            {
even=even+a[i];
            }
            else
            {
            odd=odd+a[i];
            }
        }
                        cout<<"sum of all even numbers :-"<<endl ;
                        cout<<even<<endl;
                        cout<<"sum of all odd numbers :-"<<endl ;
                        cout<<odd<<endl;
getch();  
}

Write a program to insert and deletion of an element at/from Kth position in an array


#include<iostream.h>
 #include<conio.h>
 void main()
 {
 int n,a[20],i,item,j,loc;
 clrscr();
 cout<<"enter the size of array:-"<<endl;
 cin>>n;
 cout<<"\nEnter the elements of the array:-"<<endl;
 for(i=1;i<=n;i++)
            cin>>a[i];
 cout<<"\nEnter the element :-"<<endl;
 cin>>item;
 cout<<"Enter the location where u want to insert:-"<<endl;
 cin>>loc;
 for(j=n;j>=loc;j--)
             {
              a[j+1]=a[j];
             }
  n=n+1;
  a[loc]=item;
  cout<<"After insertion of a element:-"<<endl;
  for(i=1;i<=n;i++)
            {
  cout<<"\n"<<a[i];
              }
            cout<<"Enter the location where from where u want to delete:-"<<endl;
 cin>>loc;
 item=a[loc];
 for(j=loc;j<n;j++)
    {
     a[j]=a[j+1];
    }
  n=n-1;
  cout<<"After Deletion of a element:-"<<endl;
 for(i=1;i<=n;i++)
  cout<<"\n"<<a[i];

getch();
}

Write a program merge two arrays



#include<conio.h>
#include<iostream.h>
#define MAX1 10
#define MAX2 5
void main()
{
int n1=0,n2=0,a[MAX1],b[MAX2],num,i,j;
char ch;
clrscr();
cout<<"Enter elements in the first array: "<<endl;
do
{
cout<<"Enter element: ";
cin>>num;
a[n1]=num;
n1++;
if(n1>=MAX1)
{
cout<<"!!Limit exceeded!!";
break;
}
cout<<"Do you want to continue(y/n) ";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<endl<<"After insertion elements of first array are: "<<endl;
for(i=0;i<n1;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl<<"Inserting elements in the second array:"<<endl;
do
{
cout<<"Enter the element: ";
cin>>num;
b[n2]=num;
n2++;
if(n2>=MAX2)
{
cout<<"!!Limit exceeded!!";
break;
}
cout<<"Do you want to continue(y/n):- ";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"Elements of second array are:"<<endl;
for(i=0;i<n2;i++)
{
cout<<b[i]<<"\t";
}
if(n1+n2>MAX1)
cout<<endl<<"Size of first array is not sufficient for merging"<<endl;
else
{
j=0;
for(i=n1;i<n1+n2;i++)
{
if(j<n2)
{
a[i]=b[j];
j++;
}
}
cout<<endl<<"After merging elements of first array are:"<<endl;
for(i=0;i<n1+n2;i++)
{
cout<<a[i]<<"\t";
}
}
getch();
}

Write a program to Split an array into 2 arrays


#include<iostream.h>
#include<conio.h>
void main()
{
            int a[10],i,n,b[10],c[10],mid;
            clrscr();
            cout<<"how many element you want in array A=";
            cin>>n;
            for(i=0;i<n;i++)
            {
                        cout<<"enter the value of a["<<i<<"]=";
                        cin>>a[i];
            }//end of for loop
            cout<<"\n the element are"<<endl;
            for(i=0;i<n;i++)
            {
                        cout<<"element of a["<<i<<"]="<<a[i]<<endl;
            }//end of for loop
            cout<<endl<<"WE HAVE SPILT ARRAY A INTO ARRAY B AND ARRAY C"<<endl<<endl;
            mid=n/2;
            cout<<"ELEMENT OF ARRAY B ARE\n";
            for(i=0;i<n;i++)
            {
                        if(i<=mid-1)
                        {
                                    b[i]=a[i];
                                    cout<<"element of b["<<i<<"]=";
                                    cout<<b[i]<<endl;
                        }
            }//end of for loop
            int j;
            cout<<"\n ELEMENT OF ARRAY C ARE"<<endl;
            for(i=mid,j=0;i<n;i++,j++)
            {
                        c[j]=a[i];
                        cout<<"element of c["<<j<<"]=";
                        cout<<c[j]<<endl;
            }
            j--;

getch();
}