Showing posts with label DBMS. Show all posts
Showing posts with label DBMS. Show all posts

Saturday, April 6, 2013

sin function in c

Sin function returns sine of an angle(in radian).
Declaration: double sin(double);

C programming code

#include <stdio.h>
#include <math.h>
 
int main()
{
  double result, x = M_PI/6;
 
  result = sin(x);
 
  printf("The sin(%lf) = %.2lf\n", x, result);
 
  return 0;
}
Compiler used: 
GCC
Output of program:

textcolor in c

textcolor function is used to change the color of drawing text in c programs.
Declaration :- void textcolor(int color);
where color is an integer variable. For example 0 means BLACK color, 1 means BLUE, 2 means GREEN and soon. You can also use write appropriate color instead of integer. For example you can write textcolor(YELLOW); to change text color to YELLOW. But use colors in capital letters only.

C programming code to change text color

#include<stdio.h>
#include<conio.h>
 
main()
{
   textcolor(RED);
   cprintf("C programming");
 
   getch();
   return 0;
}

C programming code for blinking text

#include<stdio.h>
#include<conio.h>
 
main()
{
   textcolor(MAGENTA+BLINK);
   cprintf("C programming");
 
   getch();
   return 0;
}
Note that we have used cprintf function instead of printf. This is because cprintf send formatted output to text window on screen and printf sends it to stdin.

getche in c

getche function prompts the user to press a character and that character is printed on screen.

C code for getche

#include<stdio.h>
#include<conio.h>
 
main()
{
   printf("Waiting for a character to be pressed from the keyboard to exit.");
 
   getche();
   return 0;
}
Run this program and press a character. Then view the user screen (Alt+F5) if using turbo c. You will find the character printed on the screen if you pressed a printable character. Try pressing enter or tab key (non printable) characters also.

gettime c

gettime in c: gettime function is used to find current system time. We pass address of a structure varibale of type ( struct time ).

C programming code for gettime

#include<stdio.h>
#include<dos.h>
 
main()
{
   struct time t;
 
   gettime(&t);
 
   printf("Current system time is %d : %d : %d\n",t.ti_hour,t.ti_min,t.ti_sec);
 
   return 0;
}
Output: 
system time

delay function in c

Delay in c: delay function is used to suspend execution of a program for a particular time.
Declaration :- void delay(unsigned int);
Here unsigned int is the number of milliseconds ( remember 1 second = 1000 milliseconds ). To use delay function in your program you should include the dos.h header file.

C programming code for delay

#include<stdio.h>
#include<stdlib.h>
 
main()
{
    printf("This c program will exit in 10 seconds.\n");        
 
    delay(10000);                         
 
    return 0;
}
Above c program exits in ten seconds, after the printf function is executed the program waits for 10000 milliseconds or 10 seconds and then program termination occurs.

Delay in c program

: If you don't wish to use delay function then you can use loops to produce delay in c program.
#include<stdio.h>
 
main()
{
   int c = 1, d = 1;
 
   for ( c = 1 ; c <= 32767 ; c++ )
       for ( d = 1 ; d <= 32767 ; d++ )
       {}
 
   return 0;
}
We don't write any statement in the loop body.

c program countdown

This c graphics program performs countdown for 30 seconds.

C program countdown code

#include<graphics.h>
#include<dos.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, i;
   char a[5];
 
   initgraph( &gd, &gm, "C:\\TC\\BGI");
 
   settextjustify( CENTER_TEXT, CENTER_TEXT );
   settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
   setcolor(RED);
 
   for(i = 30 ; i >=0 ; i--)
   {
      sprintf(a,"%d",i);
      outtextxy(getmaxx()/2, getmaxy()/2, a);
      delay(1000);
 
      if ( i == 0 )
         break;
      cleardevice();
   }
 
   getch();
   closegraph();
   return 0;
}

c program to draw circles in circles

This program draws circles in circles in two different colors.

C programming code

#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
main()
{
   int gd = DETECT, gm, x, y, color, angle = 0;
   struct arccoordstype a, b;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   delay(2000);
 
   while(angle<=360)
   {
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
      setcolor(RED);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
      getarccoords(&a);
      setcolor(GREEN);
      circle(a.xstart,a.ystart,25);
      angle = angle+5;
      delay(50);
   }
 
   getch();
   closegraph();
   return 0;
}
Output of program:
circles in circles
AttachmentSize
circles in circles.png18.11 KB

setviewport function in c

setviewport function sets the current viewport for graphics output.
Declaration :- void setviewport(int left, int top, int right, int bottom, int clip);
setviewport function is used to restrict drawing to a particular portion on the screen. For example setviewport(100 , 100, 200, 200, 1);
will restrict our drawing activity inside the rectangle(100,100, 200, 200).
left, top, right, bottom are the coordinates of main diagonal of rectangle in which we wish to restrict our drawing. Also note that the point (left, top) becomes the new origin.

C programming source code for setviewport

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, midx, midy;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   midx = getmaxx()/2;
   midy = getmaxy()/2;
 
   setviewport(midx - 50, midy - 50, midx + 50, midy + 50, 1);
   circle(50, 50, 55);
 
   getch();
   closegraph();
   return 0;
}
Output of program:
changed viewport

settextstyle function in c

Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text.
Declaration :- void settextstyle( int font, int direction, int charsize);
font argument specifies the font of text, Direction can be HORIZ_DIR (Left to right) or VERT_DIR (Bottom to top).

Different fonts

enum font_names 
{ 
   DEFAULT_FONT, 
   TRIPLEX_FONT, 
   SMALL_FONT, 
   SANS_SERIF_FONT,
   GOTHIC_FONT, 
   SCRIPT_FONT, 
   SIMPLEX_FONT, 
   TRIPLEX_SCR_FONT,
   COMPLEX_FONT, 
   EUROPEAN_FONT, 
   BOLD_FONT 
};

C programming source code for settextstyle

#include <graphics.h>
#include <conio.h>
 
main()
{
   int gd = DETECT, gm, x = 25, y = 25, font = 0;
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   for ( font = 0 ; font <= 10 ; font++)
   {
      settextstyle(font, HORIZ_DIR, 1);
      outtextxy(x, y, "Text with different fonts");
      y = y + 25;
   }
 
   getch();
   closegraph();
   return 0;
}
Output of program:
c fonts

setfillstyle function in c

setfillstyle function sets the current fill pattern and fill color.
Declaration :- void setfillstyle( int pattern, int color);
Different fill styles:
enum fill_styles 
{ 
   EMPTY_FILL, 
   SOLID_FILL, 
   LINE_FILL, 
   LTSLASH_FILL, 
   SLASH_FILL,
   BKSLASH_FILL, 
   LTBKSLASH_FILL, 
   HATCH_FILL, 
   XHATCH_FILL, 
   INTERLEAVE_FILL,
   WIDE_DOT_FILL, 
   CLOSE_DOT_FILL, 
   USER_FILL 
};

C programming source code for setfillstyle

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   setfillstyle(XHATCH_FILL, RED);
   circle(100, 100, 50);
   floodfill(100, 100, WHITE);
 
   getch();
   closegraph();
   return 0;
}
Output of program:
setfillstyle

setfillstyle function in c

setfillstyle function sets the current fill pattern and fill color.
Declaration :- void setfillstyle( int pattern, int color);
Different fill styles:
enum fill_styles 
{ 
   EMPTY_FILL, 
   SOLID_FILL, 
   LINE_FILL, 
   LTSLASH_FILL, 
   SLASH_FILL,
   BKSLASH_FILL, 
   LTBKSLASH_FILL, 
   HATCH_FILL, 
   XHATCH_FILL, 
   INTERLEAVE_FILL,
   WIDE_DOT_FILL, 
   CLOSE_DOT_FILL, 
   USER_FILL 
};

C programming source code for setfillstyle

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   setfillstyle(XHATCH_FILL, RED);
   circle(100, 100, 50);
   floodfill(100, 100, WHITE);
 
   getch();
   closegraph();
   return 0;
}
Output of program:
setfillstyle

setbkcolor function in c

Declaration :- void setbkcolor(int color);
setbkcolor function changes current background color e.g. setbkcolor(YELLLOW) changes the current background color to YELLOW.
Remember that default drawing color is WHITE and background color is BLACK.

C programming code for setbkcolor

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   outtext("Press any key to change the background color to GREEN.");
   getch();
 
   setbkcolor(GREEN);
 
   getch();
   closegraph();
   return 0;
}
setbkcolor program.

outtext function

outtext function displays text at current position.
Declaration :- void outtext(char *string);

C programming code for outtext

#include<graphics.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   outtext("To display text at a particular position on the screen use outtextxy");
 
   getch();
   closegraph();
   return 0;
}
Do not use text mode functions like printf, gotoxy etc while working in graphics mode. Also note '\n' or other escape sequences do not work in graphics mode. You have to ensure that the text doesn't go beyond the screen while using outtext.

C program to insert substring into a string

C program to insert substring into a string: This code inserts the target string into the source string. For example if the source string is "c programming" and target string is " is amazing" (please note there is space at beginning) and if we add target string to source string at position 14 then we obtain the string "c programming is amazing". In our c code we will make a function which perform the desired task and we pass three arguments to it the source string, target string and position. You can insert the string at any valid position.

C programming code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
void insert_substring(char*, char*, int);
char* substring(char*, int, int);
 
int main()
{
   char text[100], substring[100];
   int position;
 
   printf("Enter some text\n");
   gets(text);
 
   printf("Enter the string to insert\n");
   gets(substring);
 
   printf("Enter the position to insert\n");
   scanf("%d", &position);
 
   insert_substring(text, substring, position);
 
   printf("%s\n",text);
 
   return 0;
}
 
void insert_substring(char *a, char *b, int position)
{
   char *f, *e;
   int length;
 
   length = strlen(a);
 
   f = substring(a, 1, position - 1 );      
   e = substring(a, position, length-position+1);
 
   strcpy(a, "");
   strcat(a, f);
   free(f);
   strcat(a, b);
   strcat(a, e);
   free(e);
}
 
char *substring(char *string, int position, int length) 
{
   char *pointer;
   int c;
 
   pointer = malloc(length+1);
 
   if( pointer == NULL )
       exit(EXIT_FAILURE);
 
   for( c = 0 ; c < length ; c++ ) 
      *(pointer+c) = *((string+position-1)+c);       
 
   *(pointer+c) = '\0';
 
   return pointer;
}
Download Insert substring program.
Output of program:
Insert substring c program

C program for pattern matching, pattern matching in c

Pattern matching in c: C programming code to check if a given string is present in another string, For example the string "programming" is present in "c programming". If the string is present then it's location (i.e. at which position it is present) is printed. We create a function match which receives two character pointers and return the position if matching occurs otherwise returns -1. We are implementing naive string search algorithm in our c program

C programming code

#include <stdio.h>
#include <string.h>
 
int match(char [], char []);
 
int main() {
  char a[100], b[100];
  int position;
 
  printf("Enter some text\n");
  gets(a);
 
  printf("Enter a string to find\n");
  gets(b);
 
  position = match(a, b);
 
  if(position != -1) {
    printf("Found at location %d\n", position + 1);
  }
  else {
    printf("Not found.\n");
  }
 
  return 0;
}
 
int match(char text[], char pattern[]) {
  int c, d, e, text_length, pattern_length, position = -1;
 
  text_length    = strlen(text);
  pattern_length = strlen(pattern);
 
  if (pattern_length > text_length) {
    return -1;
  }
 
  for (c = 0; c <= text_length - pattern_length; c++) {
    position = e = c;
 
    for (d = 0; d < pattern_length; d++) {
      if (pattern[d] == text[e]) {
        e++;
      }
      else {
        break;
      }
    }
    if (d == pattern_length) {
      return position;
    }
  }
 
  return -1;
}
Download Pattern matching program.
Output of program:
Pattern matching c program

C program for pattern matching using pointers

#include<stdio.h>
 
int match(char*, char*);
 
main()
{
   char a[100], b[100];
   int position;
 
   printf("Enter some text\n");
   gets(a);
 
   printf("Enter a string to find\n");
   gets(b);
 
   position = match(a, b);
 
   if(position!=-1)
      printf("Found at location %d\n", position+1);
   else
      printf("Not found.\n");
 
   return 0;   
}
 
int match(char *a, char *b)
{
   int c;
   int position = 0;
   char *x, *y;
 
   x = a;
   y = b;
 
   while(*a)
   {
      while(*x==*y)
      {
         x++;
         y++;
         if(*x=='\0'||*y=='\0')
            break;         
      }   
      if(*y=='\0')
         break;
 
      a++;
      position++;
      x = a;
      y = b;
   }
   if(*a)
      return position;
   else   
      return -1;   
}

captcha program in c

This program generates captcha, a captcha is a random code generated using some algorithm. We will use random function in our code. These are used in typing tutors and in website to check whether a human is operating on a website.

C programming code

#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
 
main()
{
   int i = 0, key, num, midx, gd = DETECT, gm;
   char a[10];
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   midx = getmaxx()/2;
 
   settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
   settextjustify(CENTER_TEXT,CENTER_TEXT);
   setcolor(GREEN);
   outtextxy(midx,20,"CAPTCHA");
   settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
   outtextxy(midx,125,"Press any key to change the generated random code \"captcha\"");
   outtextxy(midx,150,"Press escape key to exit...");
 
   setcolor(WHITE);
   setviewport(100,200,600,400,1);
   setcolor(RED);
   randomize();
 
   while(1)
   {
      while(i<6)
      {
         num = random(3);
 
         if ( num == 0 )
            a[i] = 65 + random(26);     /* 65 is the ASCII value of A */
         else if ( num == 1)
            a[i] = 97 + random(26);     /* 97 is the ASCII value of a */
         else
            a[i] = 48 + random(10);     /* 48 is the ASCII value of 0 */
         i++;
      }
      a[i] = '\0';
      outtextxy(210,100,a);
      key = getch();
 
      if( key == 27 )                     /* escape key*/
         exit(0);
      clearviewport();
      i = 0;
   }
}
Output of program:


AttachmentSize
captcha.png4.68 KB

C program to add, subtract, multiply and divide Complex Numbers, complex arithmetic

C Program to add, subtract, multiply and divide complex numbers :- This program performs basic operations on complex numbers i.e addition, subtraction, multiplication and division. This is a menu driven program in which user will have to enter his/her choice to perform an operation. User can perform operations until desired. To easily handle a complex number a structure named complex has been used, which consists of two integers first integer is for real part of a complex number and second for imaginary part.

C programming code

#include <stdio.h>
#include <stdlib.h>
 
struct complex
{
  int real, img;
};
 
int main()
{
  int choice, temp1, temp2, temp3;
  struct complex a, b, c;
 
  while(1)
  {
    printf("Press 1 to add two complex numbers.\n");
    printf("Press 2 to subtract two complex numbers.\n");
    printf("Press 3 to multiply two complex numbers.\n");
    printf("Press 4 to divide two complex numbers.\n");
    printf("Press 5 to exit.\n");
    printf("Enter your choice\n");
    scanf("%d",&choice);
 
    if( choice == 5)
      exit(0);
 
    if(choice >= 1 && choice <= 4)
    {
      printf("Enter a and b where a + ib is the first complex number.");
      printf("\na = ");
      scanf("%d", &a.real);
      printf("b = ");
      scanf("%d", &a.img);
      printf("Enter c and d where c + id is the second complex number.");
      printf("\nc = ");
      scanf("%d", &b.real);
      printf("d = ");
      scanf("%d", &b.img);
    }
    if ( choice == 1 )
    {
      c.real = a.real + b.real;
      c.img = a.img + b.img;
 
      if ( c.img >= 0 )
        printf("Sum of two complex numbers = %d + %di",c.real,c.img);
      else
        printf("Sum of two complex numbers = %d %di",c.real,c.img);
    }
    else if ( choice == 2 )
    {
      c.real = a.real - b.real;
      c.img = a.img - b.img;
 
      if ( c.img >= 0 )
        printf("Difference of two complex numbers = %d + %di",c.real,c.img);
      else
        printf("Difference of two complex numbers = %d %di",c.real,c.img);
    }
    else if ( choice == 3 )
    {
      c.real = a.real*b.real - a.img*b.img;
      c.img = a.img*b.real + a.real*b.img;
 
      if ( c.img >= 0 )
        printf("Multiplication of two complex numbers = %d + %di",c.real,c.img);
      else
        printf("Multiplication of two complex numbers = %d %di",c.real,c.img);
    }
    else if ( choice == 4 )
    {
      if ( b.real == 0 && b.img == 0 )
        printf("Division by 0 + 0i is not allowed.");
      else
      {
        temp1 = a.real*b.real + a.img*b.img;
        temp2 = a.img*b.real - a.real*b.img;
        temp3 = b.real*b.real + b.img*b.img;
 
        if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d + %di",temp1/temp3,temp2/temp3);
          else
            printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);
        }
        else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d + %d/%di",temp1/temp3,temp2,temp3);
          else
            printf("Division of two complex numbers = %d %d/%di",temp1/temp3,temp2,temp3);
        }
        else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d/%d + %di",temp1,temp3,temp2/temp3);
          else
            printf("Division of two complex numbers = %d %d/%di",temp1,temp3,temp2/temp3);
        }
        else
        {
          if ( temp2/temp3 >= 0)
            printf("Division of two complex numbers = %d/%d + %d/%di",temp1,temp3,temp2,temp3);
          else
            printf("Division of two complex numbers = %d/%d %d/%di",temp1,temp3,temp2,temp3);
        }
 
      }
 
    }
    else
      printf("Invalid choice.");
 
    printf("\nPress any key to enter choice again...\n");
  }
}
Complex numbers program executable.
AttachmentSize
Complex Numbers.EXE16.62 KB
Compiler used: 
GCC
Output of program: 
Complex numbers program output