Search This Blog

Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

C Program for Shear Transformation - Shear Transformation in 2D Computer Graphics

Shear transformation or shearing is one of the 2d transformations in computer graphics. Here i present a c program for shear transformation of polygons. The shear transformation works as follows:

For shearing along X axis, shearfactor* (y coordinate) is added with x co-ordinates of all points. That is:
newx=oldx+shearfactor*oldy

Similarly, to shear along Y axis, we add shearfactor* (x coordinate) to y co-ordinates of all points.
newy=oldy+shearfactor*oldx

Program to Find Power of a Number

This is a c program to calculate power of a given number. The inputs are the number and its exponent (power). For example, if you give m and n, the result is m^n.

#include<stdio.h>
int main()
{
    int m, n, i,p=1;
    printf("Enter the number and its power (exponent)\n");
    scanf("%d%d",&m,&n);
    for( i = 0 ; i < n ; i++ )
        p*=m;
    printf("%d raised to %d is: %d",m,n,p);
}

Output:

Enter the number and its power (exponent) 2 3 2 raised to 3 is: 8

Algorithm and C Program to Find Closure From Functional Dependencies

In this post we will see how to find closure of an attribute or a set of attributes. Before learning how to get closure, we should first know what is a closure. Closure of a given set C is the set of attributes that are functionally determined by the set C under the set of functional dependencies F. There can be closure for any set. Every attribute in the set whose closure is to be found out, will be a member of its closure set C+ also. Consider an example:

C Program to Check Whether a Given Number is Even or Odd

C and C++ programs to check whether a given number is Odd or even

C Program

#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even");
else
printf("Odd");
}

C Program to Swap Numbers

The following are C and C++ programs to swap the values of two variables using a third variable.

C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}


Blooming Flower and Bee Animation C Program - Computer Graphics Lab Experiment

One of my graphics lab questions was to write a c program to animate a blooming flower and a bee (or a beetle) that comes to the flower, sit on it and fly away. The program uses graphics.h header file.

Output:



Program 

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int i,xmax,ymax;
int startx,starty;
int stopped=0;

void drawFlower(int i)
{
setcolor(GREEN);
line(xmax/2,ymax/2,xmax/2,ymax/2+100);
setcolor(BROWN);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(xmax/2,ymax/2,10+1.8*i,5+.6*i);
fillellipse(xmax/2,ymax/2,5+.6*i,10+1.8*i);
setcolor(BROWN);
setfillstyle(SOLID_FILL,RED);
fillellipse(xmax/2,ymax/2,5+.6*i,5+.6*i);
}

void drawBee(int i)
{
startx+=2;
starty+=7*sin(i);
setcolor(BROWN);
setfillstyle(SOLID_FILL,BLUE);
fillellipse(startx,starty,12,5);
setcolor(BLUE);
fillellipse(startx,starty,5,10*(i%2));
fillellipse(startx+8,starty-3,1,1);
fillellipse(startx+8,starty+3,1,1);
line(startx+9,starty-2,startx+11,starty-5);
line(startx+9,starty+2,startx+11,starty+5);
}

void main()
{
int max,may,i,t;
int gm=DETECT,gd;
initgraph(&gm,&gd," ");
xmax=getmaxx();
ymax=getmaxy();
startx=xmax/2-100;
starty=ymax/2;
setbkcolor(WHITE);
getch();
for(i=0;i<20;i++)
 {
 delay(120);
 cleardevice();
 drawFlower(i);
 }
t=i;

for(i=0;startx+i<xmax-60;i++)
       {
       delay(90);
       cleardevice();
       drawFlower(t);
       drawBee(i);
       if(stopped==0&&abs(startx-xmax/2)<10&&abs(starty-ymax/2)<15)
  {
  delay(2500);
  stopped=1;
  }
       }
getch();

}


How to use fork and exec System Call

Using fork and exec System calls in Linux (UNIX) operating systems. Fork system call is used to create a child process from a parent process.
#include<stdio.h>
main()
{

int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
exec("/bin/date\n",NULL,NULL);
exit(0); }
else
{
printf("Parent process %d\n",pid); }}
if(sp)
while(dd=readdir(sp))
printf("--> %s\n",dd->d_name);
}
}

How to make parent process wait till Completion of Child Process (Joining parent with child process)

How to make the parent process wait till the completion of execution of child process. The following C program makes the parent process to wait till the completion of its child process.

#include<stdio.h>
main()
{

int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
printf("From child process \n");
}
else
{
wait(0);
printf("From parent process\n");
}
}

How to make Child process an Orphan Process

C program in Linux or Unix to make a child process orphan:

Making child as orphan

#include<stdio.h>
main()
{

int pid,pid1;
pid=fork();
if(pid>0)
{
printf("From parent process\n");
printf("Parent process %d \n",getpid());
}
else
{
sleep(1);
printf("From child process\n");
printf("child process %d \n",getpid());
}}

C Program to Display and Set System Time and Date Using 8086 Interrupts

This is a C Program to Display and Set System Time and Date Using 8086 Interrupts. Interrupt number 21 (in hexadecimal) is used to:
C program to get system time and date and also to set it using 8086 interrupt INT 21 interrupt 0x21 21h int86() function call interrupts in C program
Output of c Program to get and set system time and date 

  • Get System time
  • Set System time
  • Get System Date
  • Set System Date
To Get system date:
- Call interrupt 0x21 (hexadecimal) 
- AH (higher byte of accumulator) should be made equal to 2A (hex) before calling interrupt
Output:



  • day in DL
  • Month in DH
  • Year in CX

To set System Date:

- Call interrupt 0x21 (hexadecimal) 
- AH (higher byte of accumulator) should be made equal to 2B (hex) before calling interrupt
- Before Calling interrupt, store:
  • Year in CX
  • Month in DH
  • Day in DL
To Get system time:
- Call interrupt 0x21 (hexadecimal) 
- AH (higher byte of accumulator) should be made equal to 2C (hex) before calling interrupt
Output:
  • Hours in CH
  • Minutes in CL
  • Seconds in DH
  • 1/100 th of a second in DL







To set System Time:







- Call interrupt 0x21 (hexadecimal) 


- AH (higher byte of accumulator) should be made equal to 2D (hex) before calling interrupt

- Before Calling interrupt, store:


  • Hour in CH 

  • Minutes in CL

  • Seconds in DH 

  • 1/100 th of second in DL




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

void main()
{
int day,month,year,tflag=0,dflag=0,hr,min,sec;
union REGS inreg,outreg;
DISDATE:printf("\nCurrent date:\n");
inreg.h.ah=0x2a;
int86(0x21,&inreg,&outreg);
printf("%d-%d-%d ",outreg.h.dl,outreg.h.dh,outreg.x.cx);
switch(outreg.h.al)
{
case 0:printf("Sunday");break;
case 1:printf("Monday");break;
case 2:printf("Tueday");break;
case 3:printf("Wednesday");break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Sunday");break;
}
if(dflag==1)
goto SETTIME;
DISTIME:printf("\nCurrent Time:\n");
inreg.h.ah=0x2c;
int86(0x21,&inreg,&outreg);
printf("%d:%d:%d.%d ",outreg.h.ch,outreg.h.cl,outreg.h.dh,outreg.h.dl);
if(tflag==1)
goto END;
SETDATE:printf("\nSet Date.\nDay,Month and Year\n");
scanf("%d%d%d",&day,&month,&year);
inreg.h.ah=0x2b;
inreg.x.cx=year;
inreg.h.dh=month;
inreg.h.dl=day;
int86(0x21,&inreg,&outreg);
if(outreg.h.al==0x00)
{
dflag=1;
goto DISDATE;
}
else
{
printf("\nInvalid entry");
getch();
exit(0);
}
SETTIME:printf("\nSet Time. Enter Time.\nHour,Minutes and Seconds\n");
scanf("%d%d%d",&hr,&min,&sec);
inreg.h.ah=0x2d;
inreg.h.ch=hr;
inreg.h.dh=sec;
inreg.h.cl=min;
inreg.h.dl=0;
int86(0x21,&inreg,&outreg);
if(outreg.h.al==0x00)
{
tflag=1;
goto DISTIME;
}
else
{
printf("\nInvalid entry");
getch();
exit(0);
}
END: getch();
}

C Program to Display SCAN and ASCII Codes of Keys on Keyboard Using 8086 Interrupt

output of C program source code to find scan code and ASCII code of any key pressed on keyboard asci ascii A 65 97 32 30 keyboard code using 8086 interrupt 16 0x16 16h INT16 int86 int86x
Output (ASCII and SCAN code) when a is pressed
This program displays SCAN code and ASCII code of any key pressed on the keyboard. We use inregs is made zero in the program. Then interrupt 16h is called using int86() function in dos.h. The arguments are interrupt number (in hexadecimal ; in C 0x16) pointer to input register and pointer to output register.
The interrupt stores scan code in higher byte of accumulator and ASCII code in lower byte of accumulator.

8086 interrupt number 16h (hexadecimal) to read a keystroke from keyboard. The condition for this interrupt is that the higher byte of accumulator (AH) should be 00h.

#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main()
{
union REGS inregs,outregs; //declaring 2 register files
clrscr();
inregs.h.ah=0x0; //pre-requisite for desired operation of interrupt 16h
int86(0x16,&inregs,&outregs); //calling interrupt 16h
printf("\nScan Code :%d\nASCII code:%d\n",outregs.h.ah,outregs.h.al);
getch();
}

Storage Classes for Variables in C Programming Language

Every variable in C programming language has two properties; type and storage class. 'Type' refers to the data type of the variable such as integer, character, floating point values etc. It also deals with the size of the variable ( in bytes). 'Storage class' determines the part of memory where storage is allocated for the variable and how long the storage allocation continues to exist. It also determines the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is accessible by name. The scope restriction can be overrode by making use of pointers. Whatever, in short, storage class is the property that determines the part of memory where storage is allocated, the lifetime of variable and the scope of the variable.

There are for storage classes in C programming:

  • Automatic
  • Register
  • External
  • Static



Automatic variables:

'Aromatic' or 'auto' is the storage class which is default for all local variables, i.e. variables declared inside a function or a block is automatic by default. They are normally declared s at the start of the block. Memory is allocated automatically upon entry to a block and freed automatically upon exit from the block. The scope of automatic variables is local to the block in which they are declared, including any blocks nested within the block. For these reasons, automatic variables are often referred to as 'local variables'. Since any variable declared inside a block or a function is 'local' by default, the keyword 'auto' which is used to declare automatic variables, are rarely used.

int fact(int n)
{
auto int result; /* automatic variable declaration*/
if(n==0)
result =1;
else
result =n*fact (n-1);
return (result);
}



In above example, an automatic integer variable 'result' is declared within the function 'fact'. The variable is local to the function as it's declared as automatic. But, it is not necessary to use the keyword 'auto' because even if auto is avoided, the variable will be auto by default.

Register Variables:

A Register variable is a special case of automatic variable. Register variables are identical to automatic variables in the case of scope, i.e, they are local to the function or block in which they are declared. The only difference is in the part of memory in which the storage is allocated. An automatic variable is always stored in RAM whereas, as the name indicates, register variables are stored in registers. When we consider a program in which a certain variable is frequently used, the performance of the program can be improved if it is stored in a register instead of RAM since registers are very much faster memory components than RAM. Variables used in enormously repeated loops of large programs, variables used as parameters for frequently called functions etc. can be declared as register variables in order to increase the performance of the program considerably.

To declare a register variable, the keyword 'register' should be prefixed to the data type as shown:

register int a;

When the compiler encounters a register variable declaration, it tries to store the variable in microprocessor's register rather than RAM. Value stored in registers are much faster than those in RAM. Since the number of registers are limited, if it couldn't be stored in register it will store it in RAM. Even the availability of resisters doesn't guarantee faster execution of program. For example, if too many register variables are declared and there are not enough registers to store all of them, values in some of the registers would have to be moved to temporary storage in memory in order to clear those registers for other variables. Thus much time may be wasted in shipping the data in and out of the registers. In addition, careless use of register variables may interfere with other uses of registers by the compiler, such as storage of temporary values in expression evaluation. In short, the keyword 'register' should be used carefully and lavish usage may result in slower execution of program.

External variables:

In some applications it may be helpful to have data which is accessible from within any block and/or which remains in existence for the entire execution of program. Such variables are called global variables, and the C language provides storage classes which can meet these requirements; namely, the external storage class.
External variables may be declared outside any function block in a source code file the same way any other variable is declared; by specifying its type and name. No storage class specifier is used -- the position of the declaration within the file indicates external storage class. Memory fit such variables is allocated when the program begins execution, and remains allocated until the program terminates. For most C implementations, every byte of memory allocated for an external variable is initialised to zero. The scope of the external variable is global,i.e. the entire source code in the file following the declarations. All functions following the declaration may access the external variable by using its name. However, if a local variable having the same name is declared within a function, references to the name access the local variable cell.

#include<stdio.h>
int m=0; /* External variable */
main ()
{
int m=1; /* auto variable */
printf("m=%d",m);
}

The output of this code is:
m=1

Static variable

The difference between static and automatic storage class is that the lifetime of a static variable is more than that of the automatic variable. The difference between them is that static variable do not disappear when the function is no longer active. There value persist. If control comes back to the same function again , the static variables have the same values they had last time around. The difference can be easily understood by comparing two programs with same variable with different storage classes. Let us compare two programs and their outputs.






#include<stdio.h>

void findsum(int a)

   {

   auto int sum=0; /*automatic*/

   sum+=a;

   printf("\nsum=%d",sum);

   }

void main()

   {

   int p;

   for(p=1;p<4;p++)

     {

     findsum(p);

     }

   }
#include<stdio.h>

void findsum(int a)

   {

   static int sum=0; /*static*/

   sum+=a;

   printf("\nsum=%d",sum);

   }

void main()

   {

   int p;

   for(p=1;p<4;p++)

     {

     findsum(p);

     }

   }

Output:
sum=1
sum=2
sum=3
Output:
sum=1
sum=3
sum=6

In the first example, the variable sum in findsum() is declared as an automatic variable while it is static in second example. In first example, at the first call of function findsum() (when p=1), the variable sum in the function is initialized as with zero. After the completion of execution of the function findsum(), the life of variable sum ends. In the next call, again sum is allocated and initialised. So, each time the number is going to be added, the initial value of sum will be 0.

In second example, once the function gets executed, the variable sum is allocated and initialised with zero. Since it is declared as a static variable, the line static int sum=0; will not be executed in the next executions of the function. i.e. the declaration statement (along with initialisation if any) will not be executed each time the function is called. Moreover, if one execution of the function is over, the static variable will not be deallocated. If control comes back to the same function again , the static variables have the same values they had last time around. There for the variable sum will give cumulative values in second example.


Program to Check Armstrong Number

An Armstrong number is a positive integer for which the sum of 'n'th power of its digits is equal to the number itself where n is the number of digits in the number. This is a c program to check whether a given number is an Armstrong number or not. For example, 153 is a 3 digit Armstrong number because 13+53+33=153.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main ()
{
int a,b=0,sum=0;
long int n;
printf("Enter the number to check\n ");
scanf("%i",&n);
if(n<1)
{
printf ("\nThe number should be greater than 0");
}
else
{
a=n;
//counting the digits
while (a>0)
  {
  a=a/10;
  b++;
  }
a=n;
//adding up bth power of digits
while(a>0)
  {
  sum=sum+pow(( a%10) ,b);
  a=a/10;
  }
if(sum==n)
  printf ("\nThe number is an ARMSTRONG number");
else
  printf ("\nThe number is NOT an ARMSTRONG number");
}
getch();
}

Determinant of Matrix Program - Program to Find Determinant of Matrix

In this post i am sharing a C program to calculate determinant of matrices. Determinant exists only for square matrix. Here i have wrote a c program to find determinant of matrices of order less than or equal to 3.

#include<stdio.h>
void main() { int a[10][10],i,m,j; int det=0; printf("\nEnter the order of matrix:\n "); scanf("%i",&m); printf("\nenter elements\n"); for(i=0;i<m;i++)  {  for(j=0;j<m;j++)   scanf("%d",&a[i][j]);  } printf("\nThe matrix is\n"); for(i=0;i<m;i++)  {  printf("\n");  for(j=0;j<m;j++)   printf("%3d",a[i][j]);  } printf("\nthe transpose is\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) printf("%3i",a[j][i]); printf("\n"); } if(m==1) { det=a[0][0]; printf("det=%i",det); } else if(m==2) { int k=1,l=1; for(i=0;i<2;i++) for(j=0;j<2;j++) { if(i==j) k*=a[i][j]; else l*=a[i][j]; } printf("\ndeterminant=%i",k-l); } else if(m==3) { for(i=0;i<3;i++) det=det+(a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3]-a[1][(i+2)%3]*a[2][(i+1)%3])); printf("\nDeterminant of matrix is: %d",det); } else printf("This program can't calculate determinant of matrix of order greater than 3"); }