Sunday 24 August 2014

C Program to Draw Pascal's triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1


#include
int main()
{
    int rows,coef=1,space,i,j;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=0;i    {
        for(space=1;space<=rows-i;space++)
        printf("  ");
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                coef=1;
            else
               coef=coef*(i-j+1)/j;
            printf("%4d",coef);
        }
        printf("\n");
    }
}

Write a C program to display reverse pyramid.

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *


 #include
void  main()
{
    int rows,i,j,space;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=rows;i>=1;--i)
    {
        for(space=0;space           printf("  ");
        for(j=i;j<=2*i-1;++j)
          printf("* ");
        for(j=0;j            printf("* ");
        printf("\n");
    }
    return 0;
}

Friday 22 August 2014

C Program to print half pyramid as using number

#include
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Thursday 21 August 2014

Pyramid And Structure Using C

#include 
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}
 

o/p
*
* *
* * *
* * * *
* * * * *

Program Of Template Partial Specialization

template 
class sortedVector
{
    public:
    void insert (T val)
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && val > vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }
    // other functions...
    private:
    T *vec_data;
    int length;
    int size;
};

To declare a partially specialized template that handles any pointer types, we'd add this class declaration


template
class sortedVector
{
    public:
    // same functions as before.  Now the insert function looks like this:
    insert( T *val )
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && *val > *vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }

    private:
    T** vec_data;
    int length;
    int size;
};

typeid, polymorphic class Using C++ Programing


#include
#include
#include
using namespace std;
class CBase 
virtual void f()
{} 
};
class CDerived : public CBase {};
int main () 
{
try 
{
CBase* a = new CBase;
CBase* b = new CDerived;
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
cout << "*a is: " << typeid(*a).name() << '\n';
cout << "*b is: " << typeid(*b).name() << '\n';
catch (exception& e) 
{ cout << "Exception: " << e.what() << endl; 
}
return 0;
}