Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Tuesday, 23 February 2016

Bresenham's circle drawing


Q)Circle generation using bresenham’s circle drawing algorithm.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
void drawcir(float x,float y,float p,float q){
putpixel(x+p,y+q,3);
putpixel(x-p,y+q,3);
putpixel(x+p,y-q,3);
putpixel(x-p,y-q,3);
putpixel(x+q,y+p,3);
putpixel(x-q,y+p,3);
putpixel(x+q,y-p,3);
putpixel(x-q,y-p,3);
    }
int main()
{
   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");
   float gx,gy,grad;
   cout<<"enter the x coordinateof the centre";
   cin>>gx;
   cout<<"enter the y coor of the centre";
   cin>>gy;
  cout<<"enter the radious of the circle";
  cin>>grad;
  float p=0;
  float q=grad;
  float d=3-2*grad;
  while(p<q){
  drawcir(gx,gy,p,q);
p++;
if(d<0)
{d=d+4*p+6;
}
  else{
q--;
  d=d+4*(p-q)+10;
  }
drawcir(gx,gy,p,q);
  }
   getch();
   closegraph();
   return 0;
}

Sunday, 21 February 2016

Filehandeling :Reading a text file

Q) Program to  read a text file and calculate number of tabs,new lines,spaces,characters in it.

#include <iostream>
#include<stdio.h>
using namespace std;

int main()

{
int tab=0,lines=0,pa=0,charec=0;
FILE *fp;
char ch;
fp=fopen("mim.txt","r");
ch=getc(fp);
    while(ch!=EOF)
    {   if(ch=='\t')
       tab++;
       if(ch=='\n')
       lines++;
       if(ch==' ')
       pa++;
       charec++;
       ch=getc(fp);
    }

cout<<"number of tabs ="<<tab<<endl;
cout<<"number of new lines ="<<lines<<endl;
cout<<"number of spaces ="<<pa<<endl;
cout<<"number of characters ="<<charec<<endl;
fclose(fp);
return 0;
}

Monday, 21 September 2015

Stack implementation using linked list

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}
void del()
{
     if(head==NULL)
        cout<<"Stack is empty"<<endl; 
    else
    head=head->next;
   
}
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The the Stack is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}

Linked list insertion and deletion at begining

#include <iostream>
using namespace std;
struct node
   {
     int data ;
     node* next;
   };
node* head;
void insert(int x)
    {   node * temp=new node();
        temp->data=x;
          if(head==NULL)
             {
                 head =temp;
                 temp->next=NULL;
             }
  else
     {   temp->next=head;
        head=temp;
     }
}
void del()
{
     if(head==NULL)
        cout<<"Linked is          empty"<<endl; 
    else
    head=head->next;
   
}
void show()
{ node*temp1=head;
while(temp1!=NULL)
{cout<<temp1->data<<"->";
       
       
    temp1=temp1->next;
    }
    cout<<"NULL"<<endl;
    }
int main()
{head=NULL;
char ch='y';int n;
    int x;
while(ch=='y'||ch=='Y')
    {
        cout<<"Enter 1 for insertion"<<endl;
        cout<<"Enter 2 for deletion"<<endl;
        cin>>n;
         switch (n)
        { case 1 : cout<<"Enter data:";
                   cin>>x;
                
           insert (x);
            break;
case 2 : del();
        break;
}
        cout<<"The linked list is:"<<endl;
        show();
        cout<<"Do you want to continue operation  y/n"<<endl;
    cin>>ch;
    }
}

Wednesday, 16 September 2015

Doubly linked list

#include <iostream>

using namespace std;
struct node
{
    int data;
    node* next;
    node* previous;
};

node* head;
void insert(int x)
{
    node* temp=new node();
    temp->data=x;
    if(head==NULL)
    {
        head=temp;
        head->next=NULL;
        head->previous=NULL;
    }
    else

    temp->next=head;
    head->previous=temp;
    head=temp;
}
void showstart()
{
   node* temp1=head;
   while(temp1!=NULL)
   {
       cout<<temp1->data<<"->";
       temp1=temp1->next;
   }
   cout<<"NULL"<<endl;
}
void showend()
{
    node* temp2=head;
    while(temp2->next!=NULL)
    {
        temp2=temp2->next;
    }

    while(temp2!=head)
    {
        cout<<temp2->data<<"->";
        temp2=temp2->previous;
    }
    cout<<head->data;

}
int main()
{
    head=NULL;
    int n,x;
    cout<<"How many elements do you want to enter?"<<endl;
    cin>>n;
    while(n--)
      {
          cout<<"Enter data :";
          cin>>x;
          insert(x);
      }
cout<<"Doubly linked list from the start is :"<<endl;
showstart();

cout<<"Doubly linked list from the end is:"<<endl;
showend();
}