Tuesday, 11 March 2014

C Program to Delete Duplicate Elements In The Array

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

int main()
{

    int a[20],b[20];
    int n=0;
    int i=0,j=0,k=0;
    int flag=0;

    printf("enter the length of the array:");
    scanf("%d",&n);

    printf("\n\narray elements:");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    /*for(i=0;i<n-1;i++)
    {
        for(k=0;k<n-1;k++)
        {
            if(a[i]==a[k])
            {
                for(j=k;j<n-1;j++)
                {
                    a[j]=a[j+1];
                    n=n-1; //redusing length
                }
            }
        }
    }*/

    b[0]=a[0];
    k=1;

    for(i=1;i<n;i++)
    {
        for(j=0;j<k;j++)
        {
            if(a[i]==b[j])
            {
                flag=1;
                break;
        }
    }

     if(flag==0)
        {
            b[k]=a[i];
            k=k+1;
        }

     flag=0;
    }

    for(i=0;i<k;i++)
        printf("b[%d]=%d\n",i,b[i]);

    getch();
    return 0;
}

Monday, 6 January 2014

C PROGRAM TO DEVELOP STRING FUNCTION -- strcpy

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

void  my_strcpy(char *dest , char *source);


int main()
{
    char str[20];
    char dest[20];
    printf("enter any string:");
    scanf("%s",str);
   
    my_strcpy(dest,str);
   
    printf("\ndest=%s",dest);
   
    getch();
    return 0;
}



void my_strcpy(char *dest,char *source)
{
     while(*source!='\0')
     {
                         *dest++=*source++;
                         }
                         *dest='\0';
                        
                         }

C PROGRAM TO DEVELOP SORTING THE STRINGS IN ALPHABETICAL ORDER

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


int main()
{
    char str[10][20];
    char temp[20];
    char ch;
    char *dest;
    int i=0,j=0,k=0;
  
  
    while(strlen(gets(str[i++]))!=0)
    ;
    i--;
  
    while(k<=i)
    {
    for(j=k;j<=i-1;j++)
    {
                    if((strcmp(str[k],str[j]))>0) //swap
                    {
                                                    strcpy(temp,str[j]);
                                                    strcpy(str[j],str[k]);
                                                    strcpy(str[k],temp);
                                                    }
                                                    }
                                                    k++;
                                                    }
                                                  
                                                  
for(j=0;j<=i;j++)
puts(str[j]);

getch();
return 0;
}                                                  

C PROGRAM TO DEVELOP STRING FUNCTION -- strrchr

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

char *my_strrchr(char *string,char charecter);

int main()
{
    char str[20];
    char ch;
    char *dest;
   
    printf("enter any string:");
    gets(str);
   
    printf("enter an charecter to search in the string:");
    scanf("%c",&ch);
   
    dest=my_strrchr(str,ch);
   
    printf("\n dest=%s",dest);
   
    getch();
    return 0;
}


char *my_strrchr(char *string,char charecter)
{
     char *temp;
     int flag=0;
     while(*string!='\0')
     {
                         if(*string==charecter)
                         temp=string;flag=1;
                        
                         string++;
                         }
                        
                         if(flag==1)
                         return(temp);
                         else
                         return(NULL);
                         }

C PROGRAM TO DEVELOP STRING FUNCTION- strchr

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

char *my_strchr(char *string,char charecter);

int main()
{
    char str[20];
    char ch;
    char *dest;
   
    printf("enter any string:");
    gets(str);
   
    printf("enter an charecter to search in the string:");
    scanf("%c",&ch);
   
    dest=my_strchr(str,ch);
   
    if(dest==NULL)  
    printf("\nvirus");
    else
    printf("\nno virus");
   
    getch();
    return 0;
}



char *my_strchr(char *string,char charecter)
{
     while(*string!='\0' && *string!=charecter)
     {
                         string++;
                         }
                        
                         if(string==NULL)
                         return(NULL);
                         else
                         return(string);
                         }

Tuesday, 24 December 2013

OPENGL PROGRAM TO DRAW GINGER BEARD MAN

#include<GL/glut.h>
#include<stdlib.h>
#include<math.h>

void myInit()
{
     glClearColor(1.0,1.0,1.0,0.0);
     glColor3f(1.0,0.0,0.0);
     glPointSize(4.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluOrtho2D(0.0,640.0,0.0,480.0);
     }
    


      void myDisplay()
      {
           glClear(GL_COLOR_BUFFER_BIT);
           glColor3f(0.0,0.0,0.0);
           int m=40,l=3;
           unsigned long int i=0;
           int px=115,py=121;
           int qx=0,qy=0;
           glBegin(GL_POINTS);
           for(i=0;i<100000;i++)
           {
                         qx=m*(1+2*l)-py+abs(px-l*m);
                         qy=px;
                         glVertex2i(qx,qy);
                         px=qx;
                         py=qy;
                         }
                         glEnd();
                         glFlush();
                        
                         }   
         
          int main(int argc ,char **argv)
          {
               glutInit(&argc,argv);
               glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB );
               glutInitWindowSize(640,480);
               //glutInitWindowPosition(200,200);
               glutCreateWindow("gingerbeard man");
               glutDisplayFunc(myDisplay);
               myInit();
               glutMainLoop();
               }


CONCENTRIC CIRCLE ANIMATION

#include <allegro.h>
#define R makecol(255,0,0)
#define G makecol(0,255,0)
#define B makecol(0,0,255)
#define K makecol(0,0,0)
#define W makecol(255,255,255)


float inner_circ_angle=0;
float cx=0,cy=0;
float sx=0,sy=0;
float r=30,angle=0;//rotating circle

BITMAP *buffer;

int draw_main_circles();
int draw_rotating_circles();


int main()
{   

   
    //int cirnum=0;
    //int rx=0,ry=0;//to detect the sign of directions
    //int dir=0;
   
   
   
    allegro_init();
    install_keyboard();
   
    set_color_depth(32);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
    buffer=create_bitmap(SCREEN_W,SCREEN_H);
   
    cx=SCREEN_W/2;
    cy=SCREEN_H/2;
    //layernum=2;//one layer
   
   
    while(!key[KEY_ESC])
    {
                        draw_main_circles();
                       
                        //circle(buffer,cx,cy,r,K);
                        //draw_rotating_circles();
                         blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
                        clear_to_color(buffer,K);
                      
                        rest(100);
                        }
                       
                        destroy_bitmap(buffer);
                       
                        allegro_exit();
                       
                        }END_OF_MAIN();
                       
                        int  draw_main_circles()
                        {
                             int i=0,j=0;
                             int tx=0,ty=0;
                             //for main circle
                             circle(buffer,cx,cy,r,G);
                             //tx=cx+r*cos(inner_circ_angle);
                             //ty=cy+r*sin(inner_circ_angle);
                            
                             //circlefill(buffer,tx,ty,4,K);
                            
                             for(j=0;j<10;j++)
                             {
                             for(i=0;i<=10;i++)
                             {
                                              tx=cx+(j*r)*cos(i*36);
                                              ty=cy+(j*r)*sin(i*36);
                                              circle(buffer,tx,ty,r,G);
                                             
                                              //calculating for sub circle
                                              sx=tx+r*cos(inner_circ_angle);
                                              sy=ty+r*sin(inner_circ_angle);
                                              circlefill(buffer,sx,sy,3,makecol(rand()%255,rand()%255,rand()%255));
                                              inner_circ_angle+=0.1;
                                             
                                              }
                                              if(j>=10) j=0;
                                              if(inner_circ_angle>=360) inner_circ_angle=0;
                                             
                                              }
                                              return 1;
                                             
                                              }//end for main-circles drawing
                                             

/*int  draw_rotating_circles()
{
     sx=cx+r*cos(inner_circ_angle);
     sy=cy+r*sin(inner_circ_angle);
     inner_circ_angle++;
     circlefill(buffer,sx,sy,3,R);
     blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
    
     //inner_circ_angle++;
     if(inner_circ_angle>=360)
      inner_circ_angle=0;
     
      return 1;
     }*/