Monday, 1 October 2012

Merging Two Arrays


#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values: "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void merge(Array a1, Array a2)
{
int i=0,j=0;
/*while (i< a1.n && j< a2.n )
{
if (a1.a[i] < a2.a[j])
{
a[n] = a1.a[i];
i++;
}
else
{
a[n] = a2.a[j];
j++;
}
n++;

}*/
while (i < a1.n)
{
a[n] = a1.a[i];
i++; n++;
}
while (j < a2.n)
{
a[n] = a2.a[j];
j++; n++;
}
}
};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
cout<<"for 2nd array ";
a2.read();
a2.display();
// itm.sort();
// itm.display();
// a2.sort();
// a2.display();
a3.merge(itm,a2);
a3.display();
// itm.del();
// itm.insert();
}

Average of Arrays


Array Reading by For Loop


#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;

}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}

};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
}

Deletion and Insertion at any index of Array


#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void del()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=index;i<n-1;i++)
a[i] = a[i+1];
n--;
display();
}
void insert()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=n;i>index;i--)
a[i] = a[i-1];
cout << "Enter Value";
cin >> a[i];
n++;
display();
}


};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
a2.read();
a2.display();

itm.del();
itm.insert();
}

Sunday, 23 September 2012

Sum of Array's Elements


#include<iostream>
using namespace std;
const int size = 5;
void main()
{
int a[size];
int sum = 0;

for( int i = 0; i<size; i++)
{
cout<<"enter"<<endl;
cin>>a[i];
sum = sum + a[i];
}
cout<<"sum is: "<<endl;
cout<<sum<<endl;
}

Friday, 13 July 2012

Calculate age


#include <iostream>
using namespace std;
class age
{
private:
int day;
int mon;
int yer;
int day1;
int mon1;
int yer1;
public:
age():day(0),mon(0),yer(0),day1(0),mon1(0),yer1(0)
{}
void get()
{
cout<<"enter current date:"<<endl;
cin>>day;
cout<<"enter current month:"<<endl;
cin>>mon;
cout<<"enter current year:"<<endl;
cin>>yer;
cout<<"Enter your date of birth:"<<endl;
cin>>day1;
cout<<"Enter your month of birth:"<<endl;
cin>>mon1;
cout<<"Enter your year of birth:"<<endl;
cin>>yer1;
}
void print()
{
if(day>day1 && mon>mon1)
{
cout<<"your age is DD-MM-YYYY"<<endl;
cout<<day1-day<<"-"<<mon1-mon1<<"-"<<yer1-yer<<endl;
}
else if(day>day1 && mon<mon1)
cout<<day1-day<<"-"<<mon1-mon1<<"-"<<yer1-yer<<endl;
else if(day<day1 && mon<mon1)
cout<<cout<<(day+30)-day1<<"-"<<(mon+11)-mon1<<"-"<<(yer-1)-yer1<<endl;
}
};
void main()
{
age a;
a.get();
a.print();
}

Thursday, 22 March 2012

Print Rational Number


#include <iostream>
using namespace std;
class rational
{
private:
int num;
int denum;
public:
void get()
{
cout<<"enter numerator: "<<endl;
cin>>num;
cout<<"enter denomirator: "<<endl;
cin>>denum;
}
void print()
{
cout<<"you Entered: "<<endl;
cout<<num<<"/"<<denum<<endl;
cout<<endl;
}
};

void main()

{
rational r1;
r1.get();
r1.print();
}

Print time in hours, minutes and seconds


#include<iostream>
using namespace std;
class time
{
private:
 int hour;
 int mint;
 int sec;
public:
 void get()
 {
cout<<"enter hours: "<<endl;
    cin>>hour;
cout<<"enter minutes: "<<endl;
    cin>>mint;
cout<<"enter seconds: "<<endl;
    cin>>sec;
 }
 void print()
 {
cout<<"you entered: "<<endl;
cout<<hour<<":"<<mint<<":"<<sec<<endl;
 }
};

void main()
{
 time t1;
 t1.get();
 t1.print();
}

Print distance in feets and inches


#include<iostream>
using namespace std;
class dist
{
private:
 int feet;
 float inch;
public:
 void get()
 {
  cout<<"enter feets: "<<endl;
  cin>>feet;
  cout<<"enter inches: "<<endl;
  cin>>inch;
 }
 void print()
 {
cout<<"you entered: "<<endl;
cout<<"feets = "<<feet<<endl;
cout<<"and"<<endl;
cout<<"inches = "<<inch;
     cout<<endl;
 }
};


void main()
{
 dist d1;
 d1.get();
 d1.print();
}

Monday, 19 March 2012

Addition of real and imaginary part


#include<iostream>
using namespace std;
class complex
{
private:
int real;
int img;
public:
void set(int a, int b)
{
real=a;
img=b;
}
void get()
{
cout<<"enter real number: "<<endl;
cin>>real;
cout<<"enter imaginary number: "<<endl;
cin>>img;
}
void print()
{
cout<<"you entered:"<<real<<"+"<<img<<"i"<<endl;
cout<<"addition of real and imaginary part is: "<<real+img;
cout<<endl;
}
};

void main()
{
complex C1,C2;
C1.get();
C2.set(10,20);
C1.print();
}

subtraction of two numbers (basic syntax)


#include<iostream>
using namespace std;
class subtract
{
private:
int number1;
int number2;
public:
void set(int a, int b)
{
number1=a;
number2=b;
}
void get()
{
cout<<"enter first number: "<<endl;
cin>>number1;
cout<<"enter second number: "<<endl;
cin>>number2;
}
void print()
{
cout<<"subtraction of two numbers is: "<<number1-number2;
cout<<endl;
}
};

void main()
{
subtract s1,s2;
s1.get();
s2.set(20,10);
s1.print();
}

Sunday, 18 March 2012

Factorial Of Number using Recursion


# include <iostream>
using namespace std;
int factorial(int n);
int main()
{
for(int i=1; i<=10; i++)
cout<<i<<"!= "<<factorial(i);
cout<<endl;


return 0;
}


int factorial(int n)
{
if (n==1)
return 1;
else
return n*factorial(n-1);
}

Christmas Song using Switch structure


#include <iostream>
using namespace std;

int main()
{
   for ( int day = 1; day < 13; day++ ) {
      cout << "On the ";
      switch ( day ) {       // switch for current day
         case 1:
            cout << "first";
            break;
         case 2:
            cout << "second";
            break;
         case 3:
            cout << "third";
            break;
         case 4:
            cout << "fourth";
            break;
         case 5:
            cout << "fifth";
            break;
         case 6:
            cout << "sixth";
            break;
         case 7:
            cout << "seventh";
            break;
         case 8:
            cout << "eighth";
            break;
         case 9:
            cout << "nineth";
            break;
         case 10:
            cout << "tenth";
            break;
         case 11:
            cout << "eleventh";
            break;
         case 12:
            cout << "twelfth";
                        break;
      }
      cout << " day of Christmas,\nMy true love sent to me:\n";
      switch ( day ) {     // switch for gifts
         case 12:
            cout << "\tTwelve drummers drumming,\n";
         case 11:
            cout << "\tEleven pipers piping,\n";
         case 10:
            cout << "\tTen lords a-leaping,\n";
         case 9:
            cout << "\tNine ladies dancing,\n";
         case 8:
            cout << "\tEight maids a-milking,\n";
         case 7:
            cout << "\tSeven swans a-swimming,\n";
         case 6:
            cout << "\tSix geese a-laying,\n";
         case 5:
            cout << "\tFive golden rings,\n";
         case 4:
            cout << "\tFour calling birds,\n";
         case 3:
            cout << "\tThree French hens,\n";
         case 2:
            cout << "\tTwo turtle doves, and\n";
         case 1:
            cout << "A partridge in a pear tree.\n\n\n";
      }
   }
   cout << endl;
   return 0;
}      

Sunday, 26 February 2012

Average of numbers using array


# include <iostream>
using namespace std;
void readArray(int arr[], const int size);
double average(int arr[], const int size);
int main(){
const int size = 10;
int arr[size];
readArray(arr,size);
cout<<"average of the Entered array : "<<average(arr,size)<<endl;
return 0;

}
void readArray(int arr[], const int size)
{
for(int i = 0; i<size; i++)
{
cout<<"enter element : "<<endl;
cin>>arr[i];
}

return;
}

double average(int arr[], const int size)
{
int sum = 0;
for(int i = 0; i<size; i++)
sum += arr[i];
return static_cast<double> (sum/size);

}

Factorial of number using Recursive Function


#include <iostream>
using namespace std;
int factorial(int n);
int main () {
for(int i=1; i<=10; i++)
cout<<i<<"! = "<<factorial(i)<<endl;

return 0;
}
int factorial(int n)
{
if(n==1)
return 1;
else
return n*factorial(n-1);
}