Loading

Archive for 08/01/2012 - 09/01/2012

C Program to Delete a File

C - Program to Delete a File


#include<stdio.h>
 
main()
{
   int status;
   char file_name[25];
 
   printf("Enter the name of file you wish to Delete :: ");
   gets(file_name);
 
   status = remove(file_name);
 
   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }
 
   return 0;
}

Posted in | 1 Comment

C - Program to Find Substring

C - Program to Find Substring


#include <stdio.h>
#include <malloc.h>
 
char* substring(char*, int, int);
 
main() 
{
   char string[100], *pointer;
   int position, length;
 
   printf("Enter a string\n");
   gets(string);
 
   printf("Enter the position and length of substring\n");
   scanf("%d%d",&position, &length);
 
   pointer = substring( string, position, length);
 
   printf("Required substring is \"%s\"\n", pointer);
 
   free(pointer);
 
   return 0;
}
 
/*C substring function: It returns a pointer to the substring */
 
char *substring(char *string, int position, int length) 
{
   char *pointer;
   int c;
 
   pointer = malloc(length+1);
 
   if (pointer == NULL)
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
 
   for (c = 0 ; c < position -1 ; c++) 
      string++; 
 
   for (c = 0 ; c < length ; c++)
   {
      *(pointer+c) = *string;      
      string++;   
   }
 
   *(pointer+c) = '\0';
 
   return pointer;
}

Posted in | 1 Comment

C Program for Palindrome

C - Program for Palindrome


#include <stdio.h>
#include <string.h>
 
main()
{
   char text[100];
   int begin, middle, end, length = 0;
 
   gets(text);
 
   while ( text[length] != '\0' )
      length++;
 
   end = length - 1;
   middle = length/2;
 
   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("Not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
      printf("Palindrome.\n");
 
   return 0;
}

Posted in | 1 Comment

C Program to Add two Complex Numbers

C - Program to Add two Complex Numbers


#include <stdio.h>
 
struct complex
{
   int real, img;
};
 
main()
{
   struct complex a, b, c;
 
   printf("Enter a and b where a + ib is the first complex number.\n");
   printf("a = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.\n");
   printf("c = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di\n",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di\n",c.real,c.img);
 
   return 0;
}

Posted in | Leave a comment

C Program to Concatenate Strings

C - Program to Concatenate Strings


#include<stdio.h>
 
void concatenate_string(char*, char*);
 
main()
{
    char original[100], add[100];
 
    printf("Enter source string\n");
    gets(original);
 
    printf("Enter string to concatenate\n");
    gets(add);
 
    concatenate_string(original, add);
 
    printf("String after concatenation is \"%s\"\n", original);
 
    return 0;
}
 
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
 
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}

Posted in | 1 Comment

C Program to Compare Two Strings

C - Program to Compare Two Strings


#include<stdio.h>
 
int compare_string(char[], char[]);
 
main()
{
    char first[100], second[100], result;
 
    printf("Enter first string\n");
    gets(first);
 
    printf("Enter second string\n");
    gets(second);
 
    result = compare_string(first, second);
 
    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");
 
    return 0;
}
 
int compare(char a[], char b[])
{
   int c = 0;
 
   while( a[c] == b[c] )
   {
      if( a[c] == '\0' || b[c] == '\0' )
         break;
      c++;
   }
   if( a[c] == '\0' && b[c] == '\0' )
      return 0;
   else
      return -1;
} 

Posted in | 1 Comment

C Program to Find String Length

C - Program to Find String Length


#include<stdio.h>
 
main()
{
   char array[100], *pointer;
   int length = 0;
 
   printf("Enter a string\n");
   gets(array);
 
   pointer = array;
 
   while(*(pointer+length))
      length++;
 
   printf("Length of entered string = %d\n",length);
 
   return 0;
}

Posted in | 1 Comment

C Program for Matrix multiplication

C - Program for Matrix multiplication


#include <stdio.h>
 
int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
 
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
 
  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);
 
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);
 
  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
 
    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
 
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }
 
        multiply[c][d] = sum;
        sum = 0;
      }
    }
 
    printf("Product of entered matrices:-\n");
 
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);
 
      printf("\n");
    }
  }
 
  return 0;
}

Posted in | 1 Comment

C Program to Transpose a Matrix

C - Program to Transpose a Matrix


#include<stdio.h>
 
main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
 
   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
 
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
 
   printf("Transpose of entered matrix :-\n");
 
   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }  
      printf("\n");
   }
 
   return 0;
}

Posted in | 1 Comment

C program to perform addition, subtraction, multiplication and division

C Program to Perform Addition, Subtraction, Multiplication and Division 



#include <stdio.h>
 
int main()
{
   int first, second, add, subtract, multiply;
   float divide;
 
   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);
 
   add      = first + second;
   subtract = first - second;
   multiply = first * second;
   divide   = first / (float)second;   //typecasting
 
   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);
 
   return 0;
}

Posted in , , , | 1 Comment

C/C++ Program for Water Jug Problem in Artificial Intelligence

C/C++ Program for Water Jug Problem in Artificial Intelligence



#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class Queue
{
            public:
                        Queue()
                        : head(NULL), tail(NULL)
                        {
                        }
                        void enqueue(int i)
                        {
                            if (head == NULL)
                                head = tail = new Node(i, NULL);
                            else
                                tail = tail->next = new Node(i, NULL);
                        }
                        int dequeue()
                        {
                                    Node* old = head;
                                    head = head->next;
                                    int i = old->i;
                                    delete old;
                                    return i;
                        }
                        int isEmpty()
                        {
                                    return (head == NULL);
                        }
                        ~Queue()
                        {
                                    while (!isEmpty())
                                    dequeue();
                        }
            private:
                        struct Node
                        {
                                    int i;
                                    Node* next;

                                    Node(int iP, Node* nextP)
                                    : i(iP), next(nextP)
                                    {
                                    }
                        } *head, *tail;
} iQueue;

const int MAX = 100;
const int MAX_I = (MAX + 1) * (MAX + 1);

int N, M, k, n, m;

int distance[MAX_I];
int prev[MAX_I];

int nmToI(int n, int m)
{
            return n * (M + 1) + m;
}

int iToN(int i)
{
            return i / (M + 1);
}

int iToM(int i)
{
            return i % (M + 1);
}

void trace(int i)
{
            if (i > 0)
                        trace(prev[i]);
            cout <<"    "<<iToN(i) << "   |   " <<iToM(i) << "\n";
}

void test(int n, int m, int n1, int m1)
{
            if (n1 < 0 || n1 > N || m1 < 0 || m1 > M)
                        return;

            int i1 = nmToI(n1, m1);

            if (distance[i1] != 0)
                        return;

            int i = nmToI(n, m);
            distance[i1] = distance[i] + 1;
            prev[i1] = i;
            iQueue.enqueue(i1);
}

int solve()
{
            n = m = 0;
            distance[0] = 1;
            iQueue.enqueue(0);

            while (!iQueue.isEmpty())
            {
                        int i = iQueue.dequeue();
                        int n = iToN(i);
                        int m = iToM(i);

                        if (n == k || m == k || n + m == k)
                        return i;

            // empty out a jug
                        test(n, m, 0, m);
                        test(n, m, n, 0);

            // fill a jug
                        test(n, m, N, m);
                        test(n, m, n, M);

            // pour one to another until source is empty
                        test(n, m, 0, n + m);
                        test(n, m, n + m, 0);

            // pour one to another until destination is full
                        test(n, m, n - M + m, M);
                        test(n, m, N, m - N + n);
            }
            return -1;
}

void main()
{
            clrscr();
            cout<<"Please enter the number of gallons in first jug:  ";
            cin>>N;
            cout<<"Please enter the number of gallons in second jug:  ";
            cin>>M;
            cout<<"Please enter the vol. of water to be left finally: ";
            cin>>k;

            int i = solve();

            cout<<"  JUG 1  "<<"  JUG 2 \n";
            cout<<"----------------\n";
            if (i == -1)
                        cout << 0 << "\n";
            else
            {
                        cout << distance[i] << "\n";
                        trace(i);
            }
            cout << -1 << "\n";
            getch();
}

Posted in | 5 Comments

C Program to Print Date

C - Program to Print Date


#include<stdio.h>
#include<conio.h>
#include<dos.h>
 
main()
{
   struct date d;
 
   getdate(&d);
 
   printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
   getch();
   return 0;
}

Posted in | 1 Comment

C Program to Get IP Address of Your Computer

C Program to Get IP Address of Your Computer


#include<stdlib.h>
 
main()
{
   system("C:\\Windows\\System32\\ipconfig");
   system("pause");
 
   return 0;
}

Posted in | 2 Comments

c program to list files in directory

C - Program to List Files in Directory


#include<stdio.h>
#include<conio.h>
#include<dir.h>
 
main()
{
   int done;
   struct ffblk a;
 
   printf("Press any key to view the files in the current directory\n");
 
   getch();
 
   done = findfirst("*.*",&a,0);
 
   while(!done)
   {
      printf("%s\n",a.ff_name);
      done = findnext(&a);
   }
 
   getch();
   return 0;
}

Posted in | 1 Comment

C Program to Delete Element from Array

C - Program to Delete Element from  Array


#include <stdio.h>
 
main()
{
   int array[100], position, c, n;
 
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d elements\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &position);
 
   if ( position >= n+1 )
      printf("Deletion not possible.\n");
   else
   {
      for ( c = position - 1 ; c < n - 1 ; c++ )
         array[c] = array[c+1];
 
      printf("Resultant array is\n");
 
      for( c = 0 ; c < n - 1 ; c++ )
         printf("%d\n", array[c]);
   }
 
   return 0;
}

Posted in | 1 Comment

C Program to Insert Element in Array

C - Program to Insert an Element in an Array


#include <stdio.h>
 
int main()
{
   int array[100], position, c, n, value;
 
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d elements\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &position);
 
   printf("Enter the value to insert\n");
   scanf("%d", &value);
 
   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];
 
   array[position-1] = value;
 
   printf("Resultant array is\n");
 
   for (c = 0; c <= n; c++)
      printf("%d\n", array[c]);
 
   return 0;
}

Posted in | Leave a comment

C Program to Reverse an Array

C - Program to Reverse an Array


#include <stdio.h>
 
int main()
{
   int n, c, d, a[100], b[100];
 
   printf("Enter the number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter the array elements\n");
 
   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);
 
   /*
    * Copying elements into array b starting from end of array a
    */
 
   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];
 
   /*
    * Copying reversed array into original.
    * Here we are modifying original array, this is optional.
    */
 
   for (c = 0; c < n; c++)
      a[c] = b[c];
 
   printf("Reverse array is\n");
 
   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);
 
   return 0;
}

Posted in | 1 Comment

C Program for Linear Search

C - Program for Linear Search


#include<stdio.h>
 
main()
{
   int array[100], search, c, number;
 
   printf("Enter the number of elements in array\n");
   scanf("%d",&number);
 
   printf("Enter %d numbers\n", number);
 
   for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d",&search);
 
   for ( c = 0 ; c < number ; c++ )
   {
      if ( array[c] == search )     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
  break;
      }
   }
   if ( c == number )
      printf("%d is not present in array.\n", search);     
 
   return 0;
}

Posted in | Leave a comment

C - Program to Print Floyd's Traingle

C - Program to Print Floyd's  Traingle


#include <stdio.h>
 
int main()
{
  int n, i,  c, a = 1;
 
  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);
 
  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }
 
  return 0;
}

Posted in | Leave a comment

C- Program for Binary Search

C - Program for Binary Search

 

#include<stdio.h>
#include<conio.h>

void insertionsort(int a[],int n)

{
   int i,j,temp;
    for(i=1;i<n;i++)
        temp=a[i];
        for(j=n-1;j>=0 && a[j]<temp;j--)
        a[j+1]=a[j];
        a[j+1]=temp;

}


void main()
{
  int i,j,key,n,a[100],flag,c;
  clrscr();
  printf("\nENTER THE NO. OF ELEMENTS:");
  scanf("%d",&n);
  printf("\nENTER THE ELEMENTS:");
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  insertionsort(a,n);
  printf("\nENTER THE ELEMENT TO BE SEARCHED:");
  scanf("%d",&key);
  flag=0;
  for(i=0,j=n-1;i<=j;)
  {
    c=(i+j)/2;
    if(a[c]<key)
        i=c+1;
    if(a[c]>key)
        j=c-1;
    if(a[c]==key)
           {
        printf("ELEMENT FOUND AT POSITION:%d",c+1);
        flag=1;
        break;
        }
   }
    if(flag==0)
        printf("ELEMENT NOT FOUND!!!!");
   getch();
}

Posted in | Leave a comment

C Program for Bubble Sort

C - Program for Bubble Sort


#include <stdio.h>
 
int main()
{
  int array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);
 
  return 0;
}

Posted in | Leave a comment

C Program for Dijkstras Algorithm

C - Program to Implement Dijkstra's Algorithm


#include<stdio.h>
#include<conio.h>
#define MAX 30
#define infinity 9999

void dijkstra(int g[MAX][MAX],int n,int startnode);

void main()
{
  int g[MAX][MAX],i,j,n;
  int edges,u,v,w;
  clrscr();

  printf("\nEnter the no. of Vertices::");
  scanf("%d",&n);
  printf("\nEnter the no. of Edges::");
  scanf("%d",&edges);
  printf("\nEnter the Edges(u,v,weight)::\n");
  for(i=0;i<edges;i++)
  {
    scanf("%d%d%d",&u,&v,&w);
    g[u][v]=g[v][u]=w;
  }

  printf("\nEnter the Start node::");
  scanf("%d",&u);
  dijkstra(g,n,u);
  getch();
}

void dijkstra(int g[MAX][MAX],int n,int startnode)
{
  int cost[MAX][MAX],dist[MAX],pred[MAX];
  int visited[MAX],count,mindist,nextnode,i,j;

  for(i=0;i<n;i++)                                 //create cost matrix
    for(j=0;j<n;j++)
        if(g[i][j]==0)
            cost[i][j]=infinity;
        else
            cost[i][j]=g[i][j];

  for(i=0;i<n;i++)
  {
    dist[i]=cost[startnode][i];
    pred[i]=startnode;
    visited[i]=0;
   }

   visited[startnode]=1;
   dist[startnode]=0;
   count=1;
   while(count<n-1)
   {
     mindist=infinity;
     //next node is node at min. distance
     for(i=0;i<n;i++)
    if(dist[i]<mindist && !visited[i])
    {
      mindist=dist[i];
      nextnode=i;
    }
     visited[nextnode]=1;
     //check if better path exists
     for(i=0;i<n;i++)
    if(!visited[i])
        if(mindist+cost[nextnode][i]<dist[i])
        {
            dist[i]=mindist+cost[nextnode][i];
            pred[i]=nextnode;
         }
     count++;
  }

  //print the path and distance of each node
  for(i=0;i<n;i++)
    if(i!=startnode)
    {
      printf("\nDistance of %d=%d",i,dist[i]);
      printf("\nPath=%d",i);
      j=i;
      do
      {
       j=pred[j];
       printf("<-%d",j);
      }while(j!=startnode);
    }
}

Posted in | Leave a comment

C Program for Huffmans Algorithm

 C - Program for Huffman's Algorithm



#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

// stucture of tree node
typedef struct treenode
{
    float freq;
    char data;
    struct treenode *left,*right;
}treenode;

/* structure of node of linked list */
typedef struct node
{
    treenode *data; //address of tree
    struct node *next;
}node;

node *insert(node *,treenode *);
treenode *create();
void encode();
void decode(treenode *);
int n;
char alphabets[30];
char codes[30][10];

void preorder(treenode *p,int i ,char word[])
 {
    if(p!=NULL)
       {
        if(p->left==NULL && p->right==NULL)
           {
            word[i]=0;
            printf("\n%c --- %s",p->data,word);
            alphabets[n]=p->data;
            strcpy(codes[n++],word);
           }
        word[i]='0';
        preorder(p->left,i+1,word);
        word[i]='1';
        preorder(p->right,i+1,word);
       }
 }

void main()
 {
    int op;char word[10];
    treenode *root=NULL;
    clrscr();
    do
       {
        printf("\n\n1)Create Huffman Tree ");
        printf("\n2)Encode a Message ");
        printf("\n3)Decode a message ");
        printf("\n4)Quit");
        printf("\nEnter Your Choice : ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: n=0;root=create();
                printf("\nPrefix codes : \n");
                preorder(root,0,word); // create the encoding sequence
                break;
            case 2: encode(); break;
            case 3: decode(root);break;
           }
       }while(op!=4);
}

treenode *create()
{
    treenode *p,*t1,*t2;
    node *head;
    int n,i;
    char x;
    float probability;
    head=NULL;       //empty linked list
    printf("\nEnter No. of alphabets :");
    scanf("%d",&n);
    for(i=0;i<n;i++)
       {
        flushall();
        printf("\nEnter alphabet :");
        scanf("%c",&x);
        printf("\nEnter frequency :");
        scanf("%f",&probability);
      /* create a new tree and insert it in
         the priority linked list */
        p=(treenode*)malloc(sizeof(treenode));
        p->left=p->right=NULL;
        p->data=x;
        p->freq=probability;
        head=insert(head,p);
      }
    /* create the final tree by merging of two trees
       of smaller weights (n-1) merges will be required*/
    for(i=1;i<n;i++)
       {
        t1=head->data;         //first tree
        t2=head->next->data;   //second tree
        head=head->next->next; /*remove first 2 trees
                 from linked list*/
      /*merge t1 and t2 with new tree in P */
        p=(treenode *)malloc(sizeof(treenode));
        p->left=t1;
        p->right=t2;
        p->freq=t1->freq+t2->freq;
        head=insert(head,p); /*insert the new tree in the linked list*/
       }

    return(head->data);
// preorder(head->data);
 //getch();
}

node *insert(node *head,treenode *t)
{
    node *p,*q;
    p=(node *)malloc(sizeof(node));
    p->data=t;
    p->next=NULL;
    if(head==NULL)  //empty linked list
        return(p);
    if(t->freq<head->data->freq)
       {
        p->next=head;
        return(p);
       }
    // locate the point of insertion
    q=head;
    while(q->next!=NULL && t->freq>q->next->data->freq)
        q=q->next;
    p->next=q->next;
    q->next=p;
    return(head);
}

void encode()
{
    char word[30];int i,j;
    flushall();
    printf("\n Enter a Message : ");
    gets(word);
    printf("\n Encoded Message \n");
    for(i=0;word[i]!='\0';i++)
      {
        for(j=0;alphabets[j]!=word[i] && j<n;j++);
            if(j<n)
                printf("%s",codes[j]);
      }
}

void decode(treenode *p)
{
    char word[90];int i;treenode *q;
    flushall();
    printf("\nEnter an Encoded message : ");
    gets(word);
    q=p;i=0;
    printf("\nDecoded Message = ");
    while(word[i]!='\0')
       {
        if(word[i]=='0')
            q=q->left;
        else
            q=q->right;
        if(q->left==NULL && q->right==NULL)
            {
            printf("%c",q->data);
            q=p;
            }
        i++;
       }

}

Posted in | Leave a comment

C Program for Paint

 C - Program for Paint


#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>

union REGS i, o;
int leftcolor[15];

int get_key()
{
   union REGS i,o;

   i.h.ah = 0;
   int86(22,&i,&o);

   return ( o.h.ah );
}

void draw_color_panel()
{
   int left, top, c, color;

   left = 100;
   top = 436;

   color = getcolor();
   setcolor(GREEN);
   rectangle(4,431,635,457);
   setcolor(RED);
   settextstyle(TRIPLEX_FONT,0,2);
   outtextxy(10,431,"Colors : ");

   for( c = 1 ; c <= 15 ; c++ )
   {
      setfillstyle(SOLID_FILL, c);
      bar(left, top, left+16, top+16);
      leftcolor[c-1] = left;
      left += 26;
   }

   setcolor(color);
}

void draw_shape_panel()
{
   int left, top, c, color;

   left = 529;
   top = 45;

   color = getcolor();
   setcolor(GREEN);
   rectangle(525,40,633,255);

   for( c = 1 ; c <= 7 ; c++ )
   {
      rectangle(left, top, left+100, top+25);
      top += 30;
   }
   setcolor(RED);
   outtextxy(530,45,"Bar");
   outtextxy(530,75,"Line");
   outtextxy(530,105,"Pixel");
   outtextxy(530,135,"Ellipse");
   outtextxy(530,165,"Freehand");
   outtextxy(530,195,"Rectangle");
   outtextxy(530,225,"Clear");
   setcolor(color);
}

void change_color(int x, int y)
{
   int c;

   for( c = 0 ; c <= 13 ; c++ )
   {
      if( x > leftcolor[c] && x < leftcolor[c+1] && y > 437 && y < 453 )
         setcolor(c+1);
      if( x > leftcolor[14] && x < 505 && y > 437 && y < 453 )
         setcolor(WHITE);
   }
}

char change_shape(int x, int y)
{
   if ( x > 529 && x < 625 && y > 45 && y < 70 )
      return 'b';
   else if ( x > 529 && x < 625 && y > 75 && y < 100 )
      return 'l';
   else if ( x > 529 && x < 625 && y > 105 && y < 130 )
      return 'p';
   else if ( x > 529 && x < 625 && y > 135 && y < 160 )
      return 'e';
   else if ( x > 529 && x < 625 && y > 165 && y < 190 )
      return 'f';
   else if ( x > 529 && x < 625 && y > 195 && y < 220 )
      return 'r';
   else if ( x > 529 && x < 625 && y > 225 && y < 250 )
      return 'c';
      return 0;
}

void showmouseptr()
{
   i.x.ax = 1;
   int86(0x33,&i,&o);
}

void hidemouseptr()
{
   i.x.ax = 2;
   int86(0x33,&i,&o);
}

void restrictmouseptr( int x1, int y1, int x2, int y2)
{
   i.x.ax = 7;
   i.x.cx = x1;
   i.x.dx = x2;
   int86(0x33,&i,&o);

   i.x.ax = 8;
   i.x.cx = y1;
   i.x.dx = y2;
   int86(0x33,&i,&o);
}

void getmousepos(int *button,int *x,int *y)
{
   i.x.ax = 3;
   int86(0x33,&i,&o);

   *button = o.x.bx;
   *x = o.x.cx;
   *y = o.x.dx;
}

main()
{
   int gd = DETECT,gm;

   int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;
   char ch = 'f' ;            // default free-hand drawing

   initgraph(&gd,&gm,"C:\\TC\\BGI");

   maxx = getmaxx();
   maxy = getmaxy();

   setcolor(BLUE);
   rectangle(0,0,maxx,maxy);

   setcolor(WHITE);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);

   draw_color_panel();
   draw_shape_panel();

   setviewport(1,1,maxx-1,maxy-1,1);

   restrictmouseptr(1,1,maxx-1,maxy-1);
   showmouseptr();
   rectangle(2,2,518,427);
   setviewport(1,1,519,428,1);

   while(1)
   {
      if(kbhit())
      {
         key = get_key();

         if( key == 1 )
         {
            closegraph();
            exit(0);
         }
      }

      getmousepos(&button,&x,&y);

      if( button == 1 )
      {
         if( x > 4 && x < 635 && y > 431 && y < 457 )
            change_color( x, y );
         else if ( x > 529 && x < 625 && y > 40 && y < 250 )
             ch = change_shape( x, y );

         temp1 = x ;
         temp2 = y ;

         if ( ch == 'f' )
         {
            hidemouseptr();
            while( button == 1 )
            {
               line(temp1,temp2,x,y);
               temp1 = x;
               temp2 = y;
               getmousepos(&button,&x,&y);
            }
            showmouseptr();
         }

         while( button == 1)
            getmousepos(&button,&x,&y);

         /* to avoid interference of mouse while drawing */
         hidemouseptr();

         if( ch == 'p')
            putpixel(x,y,getcolor());

         else if ( ch == 'b' )
         {
            setfillstyle(SOLID_FILL,getcolor());
            bar(temp1,temp2,x,y);
         }
         else if ( ch == 'l')
            line(temp1,temp2,x,y);
         else if ( ch == 'e')
            ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));
         else if ( ch == 'r' )
            rectangle(temp1,temp2,x,y);
         else if ( ch == 'c' )
         {
            ch = 'f';          // setting to freehand drawing
            clearviewport();
            color = getcolor();
            setcolor(WHITE);
            rectangle(2,2,518,427);
            setcolor(color);
         }

         showmouseptr();
      }
   }
}

Posted in | Leave a comment

C Program to Find Length of String Using Command line Argument

C - Program to Find the Length of  String Using Command line Argument


#include < stdio.h >
#include < conio.h >

void main(int argc,char *argv[])
{
    int len;
    char *ptr;   
    clrscr();
    if ( argc != 2)
    {
        printf("\n Invalid arguments ");
        exit(0);
    }
    else
    {
        ptr=argv[1];
        len=0;
        while ( *(ptr+len) != '\0')
        {
            len++;
        }
        printf("\n Length is %d",len);
    }
}

Posted in | Leave a comment

C Program to Sort Names in Alphabetical Order

 C Program to Sort Names in Alphabetical Order


#include < stdio.h >
#include < conio.h >
#include < alloc.h >

void main()
{
    char *list[100],temp[10];
    int n,i,j;
    clrscr();
    printf("How many names?\n");
    scanf("%d",&n);
    printf("Enter %d names \n",n);
    for(i=0;i < n;i++)
    {
        list[i]=(char *)malloc(100);
        flushall();
        gets(list[i]);
    }
    for(i=0;i < n-1;i++)
    {
        for(j=i+1;j < n;j++)
        {
            strcmp(list[i],list[j]);
            {
                strcpy(temp,list[i]);
                strcpy(list[i],list[j]);
                strcpy(list[j],temp);
            }
        }
    }
    printf("The %d names in sorted order are\n",n);
    for(i=0;i < n;i++)
    {
        puts(list[i]);
    }
}//main

Posted in | Leave a comment

C Program for Heap Sort

C - Program for Heap Sort


#include < stdio.h >
#include < conio.h >

void main()
{
    int a[50];
    int n;
    clrscr();
    printf("\nEnter n: ");
    scanf("%d",&n);
    read_data(a,n);
    printf("\nBefore sort : \n");
    print_data(a,n);
    heap_sort(a,n);
    printf("\nAfter sort : \n");
    print_data(a,n);
}

int read_data(int a[],int max)
{
    int i;
    printf("\nEnter %d values \n",max);
    for(i=1;i<=max;i++)
    {
        scanf("%d",&a[i]);
    }
    return;
}

int print_data(int a[],int max)
{
    int i;
    for(i=1;i<=max;i++)
    {
        printf("%10d",a[i]);
    }
    return;
}

int heap_sort(int a[],int n)
{
    int i,j,t;
    for(i=n/2;i>=1;i--)
    {
        adjust(a,i,n);
    }
    for(i=n-1;i>=1;i--)
    {
        printf("\n");
        print_data(a,n);
        t=a[i+1];
        a[i+1]=a[1];
        a[1]=t;
        adjust(a,1,i);
    }
    return;
}
int adjust(int a[],int cur_pos,int max)
{
    int cur_rec,j;
    cur_rec=a[cur_pos];
    j=cur_pos * 2;
    while(j<=max)
    {
        if(j < max)
        {
        if(a[j] < a[j+1])
        {
            j=j+1;
        }
     }
    if(a[j] > cur_rec)
    {
        a[j/2]=a[j];
        j=j*2;
    }
 else
  break;
 }
 a[j/2]=cur_rec;
 return;
}

Posted in | Leave a comment

C Program to Convert decimal number to hexadecimal

C - Program to Convert decimal number to Hexadecimal

 


#include < stdio.h >
#include < conio.h > 
void main()
{
 int dec,rem[20],i,max,r;
 
 clrscr();
 printf("\n Enter a decimal number : ");
 scanf("%d",&dec);
 i = 0; 
 while ( dec != 0 )
 {
  r = dec % 16;
  dec = dec / 16;
  rem[i] = r;
  i++;
 } 
 i = i-1; 
 while ( i >=0)
 {
  if ( rem[i] < 10 )
  printf("%d",rem[i]);
  else
  printf("%c",55 + rem[i]);
  i--;
 } 
}



Posted in | Leave a comment

C Program for Selection Sort


C - Program to Implement Selection Sort


#include<stdio.h>

main()
{
   int array[100], n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

   return 0;
}

Posted in | Leave a comment

C Program to Shudown Computer

C Program to Shudown Computer 

( works in Windows Xp)


#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");

   return 0;
}

Posted in | Leave a comment

C Program to Check Anagram

C - Program to Check Anagram


#include <stdio.h>

int check_anagram(char [], char []);

int main()
{
   char a[100], b[100];
   int flag;

   printf("Enter first string\n");
   gets(a);

   printf("Enter second string\n");
   gets(b);

   flag = check_anagram(a, b);

   if (flag == 1)
      printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
   else
      printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);

   return 0;
}

int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;

   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }

   c = 0;

   while (b[c] != '\0')
   {
      second[b[c]-'a']++;
      c++;
   }

   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return 0;
   }

   return 1;
}

Posted in | Leave a comment

C Program to Generate Random Numbers

C - Program to Generate Random Numbers


#include <stdio.h>
#include <stdlib.h>

int main() {
  int c, n;

  printf("Ten random numbers in [1,100]\n");

  for (c = 1; c <= 10; c++) {
    n = rand()%100 + 1;
    printf("%d\n", n);
  }

  return 0;
}

Posted in | Leave a comment

C Program for Pascals Triangle

 C - Program for Pascals Triangle


#include<stdio.h>

long factorial(int);

main()
{
   int i, n, c;

   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");

      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}

Posted in | Leave a comment

C Program for Odd/Even Number

 C Program to Check whether a Number is Odd/Even


#include<stdio.h>

main()
{
   int n;

   printf("\n Enter an integer ::");
   scanf("%d",&n);

   if ( n%2 == 0 )
      printf("\n Even");
   else
      printf("\n Odd");

   return 0;
}

Posted in | Leave a comment

C Program for Fibonacci Series



 C Program for Fibonacci Series


#include<stdio.h>

main()
{
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
   scanf("%d",&n);
   printf("First %d terms of Fibonacci series are :-\n",n);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
   return 0;
}

Posted in | Leave a comment

C Program for Armstrong Number

 C - Program for Check if a Number is Armstrong Number



#include <stdio.h>

main()
{
   int number, sum = 0, temp, remainder;

   printf("\n Enter a number :: ");     
   scanf("%d",&number);

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }

   if ( number == sum )
      printf("\n Entered number is an Armstrong number.");
   else
      printf("\n Entered number is not an Armstrong number.");        

   return 0;
}

Posted in | Leave a comment

C Program to Reverse a Number


 C Program to Reverse a Number



#include <stdio.h>

main()
{
   int n, reverse = 0;

   printf("Enter a number to reverse\n");
   scanf("%d",&n);

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }

   printf("Reverse of entered number is = %d\n", reverse);

   return 0;
}

Posted in | Leave a comment

C Program to Display prime numbers

C - Program to Display Prime Numbers


#include<stdio.h>
#include<conio.h>

void main()
{
   int n, i = 3, count, c;

   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);

   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }

   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }   
  getch();     
  }

Posted in | Leave a comment

C Program for Counting Sort

C - Program for Counting Sort


#include<stdio.h>
#include<conio.h>

void main()
{
   int a[20],b[20],c[1000],n,i,j,k=1000;
   clrscr();
   printf("\nHow many Numbers Do u want to sort :: ");
   scanf("%d",&n);

   printf("\nEnter the Numbers :: ");
   for(i=1;i<=n;i++)
   {
    scanf("%d",&a[i]);
   }
  
 /* counting sort starts here */
   for(i=0;i<k;i++)
    c[i]=0;
   for(j=1;j<=n;j++)
    c[a[j]]=c[a[j]]+1;
   for(i=1;i<k;i++)
    c[i]=c[i]+c[i-1];
   for(j=n;j>=1;j--)
   {
    b[c[a[j]]]=a[j];
    c[a[j]]=c[a[j]]-1;
   }

   printf("\nThe Sorted Numbers :: ");
   for(i=1;i<=n;i++)
    printf(" %d ",b[i]);
   getch();
}

Posted in | Leave a comment

C Program to Swap Two Numbers without Temporary Variable

C -Program to Swap Two Numbers without Temporary Variable



#include<stdio.h>
#include<conio.h>

void main()
{
   int x,y;
   clrscr();
   printf("\n Enter Value of x :: ");
   scanf("%d",&x);
   printf("\n Enter Value of y :: ");
   scanf("%d",&y);
   printf("\n Before Swapping\n");
   printf("\n x :: %d\t\ty :: %d",x,y);
   x=x+y;
   y=x-y;
   x=x-y;
   printf("\n\n After Swapping\n");
   printf("\n x :: %d\t\ty :: %d",x,y);
   getch();
}

Posted in | Leave a comment

C Program to Implement Insertion Sort

 

 C - Program to Implement Insertion Sort

  
 
 
#include<stdio.h>
void main()
{
 int A[20], N, Temp, i, j;
 clrscr();
 printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
 scanf("%d", &N);
 printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
 for(i=0; i<N; i++)
 {
  gotoxy(25,11+i);
  scanf("\n\t\t%d", &A[i]);
 }
 for(i=1; i<N; i++)
 {
  Temp = A[i];
  j = i-1;
  while(Temp<A[j] && j>=0)
  {
   A[j+1] = A[j];
   j = j-1;
  }
  A[j+1] = Temp;
 }
 printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
 for(i=0; i<N; i++)
  printf("\n\t\t\t%d", A[i]);
 getch();
}

Posted in | Leave a comment

C/C++ Program for Euclids Algorithm for GCD

C/C++ Program for Euclids Algorithm for  Finding GCD of Two Numbers


#include<stdio.h>
#include<iostream.h>
#include<conio.h>

void main()
{
int a,b,c,i,j,k;
clrscr();
cout<<"\n\t\t   *** Euclids Algorithm to Find GCD ***\n\n";
cout<<"\nEnter First Number :: ";
cin>>a;
cout<<"\nEnter Second Number :: ";
cin>>b;
if(a<b);
{
     k=a;
     a=b;
     b=k;
}
c=b;
while(b>0)
{
 c=a%b;
 a=b;
 b=c;
}
cout<<"\nGCD of Two Nos. :: "<<a;
getch();
}

Posted in , | Leave a comment

c/c++ Program to implement RSA Algorithm

C/C++ Program to implement RSA Algorithm




#include<iostream.h>
#include<conio.h>
#include<math.h>

int main()
{
   int i,j,k,p,q,flag,e,x,d;
   clrscr();
  cout<<"\n Enter Value of p :: ";
  cin>>p;
  cout<<"\n Enter Value of q :: ";
  cin>>q;
  int n=p*q;
  int fn=(p-1)*(q-1);

  cout<<"\n Value of (p,q) :: "<<p<<" "<<q;
  cout<<"\n Value of n,fn  :: "<<n<<" "<<fn;
  cout<<"\n\n Select Public Key e (1 < e < "<<fn<<") ::";
  cin>>e;

  int sk;
  for(i=1;i>0;i++)
  {
    x=i*e;
    d=x%fn;
    if(d==1)
    {
    cout<<"\n\n Private Key ::"<<i;
    sk=i;
    break;
    }

  }

  int pt;
  cout<<"\n\n  Enter Plain Text ::";
  cin>>pt;

  int m=1;
  for(i=0;i<e;i++)
    m=(m*pt)%n;
  m=m%n;
  cout<<"\n\n  Cipher Text ::"<<m;
  getch();

  k=1;
  for(i=0;i<sk;i++)
    k=(k*m)%n;
  k=k%n;
  cout<<"\n\n  Decrypted Text ::"<<k;
  getch();
 return 0;
}

Posted in | Leave a comment

c program to calc in/out degree of vertices of graph

C Program to Calculate In/Out - Degree of Vertices of a Graph


#include<stdio.h>
#include<conio.h>

typedef struct node
{
  struct node *next;
  int vertex;
}node;

node *g[20];
int n,visited[20];
int indegree(int i);
int outdegree(int i);
void dfs(int i);

void insert(int vi,int vj)
{
 node *p,*q;
 q=(node*)malloc(sizeof(node));
 q->vertex=vj;
 q->next=NULL;

 if(g[vi]==NULL)
    g[vi]=q;
 else
 {
   p=g[vi];
   while(p->next!=NULL)
    p=p->next;
   p->next=q;
 }
}

void readgraph()
{
 int vi,vj,i,j,k,no_of_edges;
 for(i=0;i<n;i++)
    g[i]=NULL;
 printf("\nEnter the no. of Vertices::");
 scanf("%d",&n);
 printf("\nEnter the no of Edges::");
 scanf("%d",&no_of_edges);
 for(i=0;i<no_of_edges;i++)
 {
    printf("\nEnter the Edge(u,v)::");
    scanf("%d%d",&vi,&vj);
    insert(vi,vj);
  }
}


void main()
{
  int i,j,k;
  clrscr();
  readgraph();
  for(i=0;i<n;i++)
    visited[i]=0;

 /* printf("\n=====================================================");
  printf("\nNode\tIndegree\tOutdegree");
  printf("\n=====================================================");
  for(i=0;i<n;i++)
  {
   j=indegree(i);
   k=outdegree(i);
   printf("\n%2d\t%4d\t\t%5d",i,j,k);
   }
  printf("\n-----------------------------------------------------");
  */
  dfs(0);
  getch();
}

int outdegree(int i)
{
  int j=0;
  node *p;
  p=g[i];
  while(p!=NULL)
  {
   p=p->next;
   j++;
  }
  return(j);
}

int indegree(int v)
{
  int i,j=0,k;
  node *p;
  for(i=0;i<n;i++)
  {
    p=g[i];
    while(p!=NULL)
    {
      if(p->vertex==v)
        j++;
      p=p->next;
    }
  }
  return(j);
}

void dfs(int i)
{
 node *p;
 p=g[i];
 visited[i]=1;
 printf("\nVisit->%d",i);
 while(p!=NULL)
 {
   i=p->vertex;
   if(!visited[i])
    dfs(i);
   p=p->next;
 }
}

Posted in | Leave a comment

c program to find factorial of number

C Program to Find Factorial of Number


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a,i,j,k;
    clrscr();
    printf("\n\t\t***PROGRAMME TO CALCULATE FACTORIAL OF A NO.***");
    printf("\n\t\tENTER A NO.: ",a);
    scanf("%d",&a);
    j=1;
    for(i=a;i>0;i--)
    {
     j=j*(i);

     }
    printf("\n\t\tTHE FACTORIAL OF NO IS: %d",j);
    getch();
   }

Posted in , | Leave a comment

c program to copy a text file

C Program to Copy a Text File


#include<stdio.h>
#include<conio.h>

void main()
{
  FILE *fp1,*fp2;
  char file1[500],file2[500];
  char x;
  printf("\nENTER THE SOURCE FILE:");
  gets(file1);
  fp1=fopen(file1,"rb");
  printf("\nENTER THE DESTINATION FILE:");
  gets(file2);
  fp2=fopen(file2,"wb");
  if(fp1!=NULL && fp2!=NULL)
  {
    while((x=getc(fp1))!=EOF)
    putc(x,fp2);

  }

  else
    printf("\nERROR CREATING FILE...");

  fclose(fp1);
  fclose(fp2);
  getch();
 }

Posted in , , , | Leave a comment

c program to display blinking text/character

C Program to Display Blinking Text/Character


#include <conio.h>

int main(void)
{
   int i;

   clrscr();
   for (i=0; i<9; i++)
   {
       textattr(i + ((i+1) << 4));
       cprintf("This is a test\r\n");
   }

   return 0;
}

Posted in , , , | Leave a comment

c program for graphics editor

C - Program for Graphics Editor

-------------------------------------------------------------------------------------------------------------
Title: assignment to understand functions available in graphics liabrary such as,
1. Text  & graphics mode,initialization of graphics mode, graphics drivers, switching between text & graphics mode,error handling.
2. Colour,colour palette,aspect ratio,text:fonts,alignments,size,orientation &justification.
3. Graphics primitives: pixel,line,circle,ellipse,polygons,line styles,bar graphs,pie charts,histogram,filling a polygon, windowing.
4. Writing a graphics editor.
------------------------------------------------------------------------------------------------------------

 
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>        //include header file
#include <conio.h>
#include<iostream.h>
class graph_edit           //class declraration
{               
   int x1,y1,x,y;

   public:
   void drawcircal();
   void drawline();
   void drawellips();
   void draw_poly();
   void fill_poly();
   void barchar();
   void piechart();
   void pixel();
   void text();
   graph_edit();
   };


graph_edit::graph_edit()    //constructor declaration
{
x1=y1=0;
}

void graph_edit::piechart() //function for piechart
{
      x=getmaxx()/2;
      y=getmaxy()/2;

      setfillstyle(SOLID_FILL, 5);
      pieslice(x,y,0,60,150);
      setfillstyle(SOLID_FILL, 10);
      pieslice(x,y,61,180,150);
      setfillstyle(SOLID_FILL, 12);
      pieslice(x,y,181,360,150);
   }

void graph_edit::barchar() //function for barchart
{
      x=getmaxx();
      y=getmaxy();

      setfillstyle(BKSLASH_FILL, getmaxcolor());
      line(50,20,50,y-20);
      line(20,y-50,x-20,y-50);
      bar(100,y-50,x-500,200);
      bar(200,y-50,x-400,150);
      bar(300,y-50,x-300,250);
      bar(400,y-50,x-200,350);
      outtextxy(40,22,"Y");
      outtextxy(x-30,y-40,"x");
      outtextxy(10,y-30,"(0,0)");
}

void graph_edit::drawcircal() //function for circal
{
      int rad;
      x=getmaxx()/2;
      y=getmaxy()/2;

      cout<<"\nEnter the radius";
      cin>>rad;

      setcolor(getmaxcolor());
      clrscr();
      circle(x,y,rad);
}

void graph_edit::draw_poly()  //function for polygon
{
      int poly[15];
      x=getmaxx();
      y=getmaxy();

      poly[0] = 20;
      poly[1] = x/2;

      poly[2] = x-20;
      poly[3] = 20;

      poly[4] = x-50;
      poly[5] = y-20;

      poly[6] = x/2;
      poly[7] = y/2;

      poly[8] = poly[0];
      poly[9] = poly[1];

      clrscr();
      drawpoly(5, poly);

}

void graph_edit::text()  //function for textstyle
{
      char str[20];
      int si;
      y=getmaxy()/2;
      cout<<"\nEnter the size of Text:";
      cin>>si;
      settextstyle(SANS_SERIF_FONT,HORIZ_DIR, si);
      sprintf(str,"Graphics");
      clrscr();
      outtextxy(0,y,str);
}
void graph_edit::fill_poly() //function for fillpolygon
{
      int poly[15];
      x=getmaxx();
      y=getmaxy();

      poly[0] = 20;
      poly[1] = x/2;

      poly[2] = x-20;
      poly[3] = 20;

      poly[4] = x-50;
      poly[5] = y-20;

      poly[6] = x/2;
      poly[7] = y/2;

      poly[8] = poly[0];
      poly[9] = poly[1];

      clrscr();

      drawpoly(5, poly);
      setfillstyle(SLASH_FILL,12);
      fillpoly(5,poly);
      getch();
}



void graph_edit::drawellips() //function for ellips
{
      int angle_s,angle_e,x_rad,y_rad;
      angle_s=0;
      angle_e=360;
      x=getmaxx()/2;
      y=getmaxy()/2;

      cout<<"\n Enter x and y radius:";
      cin>>x_rad>>y_rad;

      clrscr();
      ellipse(x,y,angle_s,angle_e,x_rad,y_rad);
      cout<<x1;
}
void graph_edit::pixel()  //function for draw pixel
{
int x,y;
cout<<" Enter X and Y Position";
cin>>x>>y;
putpixel(x,y,10);
}

void graph_edit::drawline()   //function for draw line
{

      int x2,y2;
      cout<<"Enter starting point (x1,y1)";
      cin>>x1>>y1;
      cout<<"Enter ending point (x2,y2)";
      cin>>x2>>y2;
      clrscr();
      line(x1,y1,x2,y2);

}


int main(void)     //main function declaration
{
graph_edit g;
int ch;           //initialize graphic mode
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
errorcode = graphresult();
if(errorcode != grOk)
{
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
}
do              //display menu
{

      cout<<"\n\n\t\tMENU";
      cout<<"\n\n\t1:Circle\n\t2:Line\n\t3:ellips";
      cout<<"\n\t4:polygon\t\n\t5:FillPolygon\n\t6:Text";
      cout<<"\n\t7:Pixel\n\t8:Barchart\n\t9:Pie Chart\n\t10:Exit";
      cout<<"\n\nEnter your choice:";
      cin>>ch;

switch(ch)
{

case 1: g.drawcircal();
      getch();          //case for circal drawing
      cleardevice();
      break;
case 2: g.drawline();
      cleardevice();    //case for line drawing
      getch();
      break;
case 3: g.drawellips();
      getch();          //case for ellips drawing
      cleardevice();
      break;
case 4: g.draw_poly();
      getch();          //case for polygon drawing
      cleardevice();
      break;
case 5: g.fill_poly();
      cleardevice();    //case for polygon filling
      getch();
      break;
case 6: g.text();
      getch();          //case for text style
      cleardevice();
      break;
case 7: cleardevice();
      g.pixel();        //case for pixel drawing
      getch();
      break;
case 8:     cleardevice();
      g.barchar();      //case for barchart
      getch();
      break;
case 9:     cleardevice();
      g.piechart();     //case for barchart
      getch();
case 10:exit(0);
      getch();

}

      cleardevice();
}while(ch!=10);

   getch();               //closing graph
   closegraph();
   return 0;
}

Posted in | Leave a comment