Sunday 25 December 2011

How to use function ?

#include<stdio.h>
#include<conio.h>
int header();
int sum;               /* This is a global variable */
int main( )
{
int index;
header();      /* This calls the function named header */
for (index = 1;index <= 7;index++)
square(index); /* This calls the square function */
ending();
getch();       /* This calls the ending function */
}


header()               /* This is the function named header */
{
sum = 0;       /* initialize the variable "sum" */
printf("This is the header for the square program\n\n");
}


square(number)         /* This is the square function */
int number;
{
int numsq;
numsq = number * number; /* This produces the square */
sum += numsq;
printf("The square of %d is %d\n",number,numsq);
}
ending()               /* This is the ending function */
{
printf("\nThe sum of the squares is %d\n",sum);


}

Data Types in C



A programming language is proposed to help programmer to process certain kinds of data and to
provide useful output. The task of data processing is accomplished by executing series of commands
called program. A program usually contains different types of data types (integer, float, character
etc.) and need to store the values being used in the program. C language is rich of data types. A C
programmer has to employ proper data type as per his requirements.
C has different data types for different types of data and can be broadly classified as:

1. Primary Data Types
2. Secondary Data Types


Primary Data Types:

Integer Data Types:

Integers are whole numbers with a range of values, range of values are machine dependent.
Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32767
(that is, -215 to +215-1). A signed integer use one bit for storing sign and rest 15 bits for number.
To control the range of numbers and storage space, C has three classes of integer storage namely
short int, int and long int. All three data types have signed and unsigned forms. A short
int requires half the amount of storage than normal integer. Unlike signed integer, unsigned
integers are always positive and use all the bits for the magnitude of the number. Therefore, the
range of an unsigned integer will be from 0 to 65535. The long integers are used to declare a longer
range of values and it occupies 4 bytes of storage space.

Syntax:

int <variable name>;
int num1;
short int num2;
long int num3;

Example: 5, 6, 100, 2500.

Integer Data Type Memory Allocation:


Floating Point Data Types:

The float data type is used to store fractional numbers (real numbers) with 6 digits of precision.
Floating point numbers are denoted by the keyword float. When the accuracy of the floating point
number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision
further we can use long double which occupies 10 bytes of memory space.

Syntax :

float <variable name>;
float num1;
double num2;
long double num3;

Example: 9.125, 3.1254.

Floating Point Data Type Memory Allocation :


Character Data Type:

Character type variable can hold a single character and are declared by using the keyword char. As
there are singed and unsigned int (either short or long), in the same way there are signed and
unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters have
values between 0 and 255, signed characters have values from –128 to 127.

Syntax:

char <variable name>;
char ch = ‘a’;

Example: a, b, g, S, j.

Void Type:

The void type has no values therefore we cannot declare it as variable as we did in case of integer
and float. The void data type is usually used with function to specify its type.


Saturday 24 December 2011

Program to finding TRANSPOSE matrix of a matrix


//program to transpose a matrix//


#include<stdio.h>
#include<conio.h>
#define max 5
void main()
{
  int i,j,m,n,a[max][max],b[max][max];
  clrscr();
  printf("\t\t\t**** TRANSPOSE OF A MATRIX ****");
  printf("\nEnter the no. of rows & coloumns:\n");
  scanf("%d%d",&m,&n);
  printf("Enter the elements of matrix:\n");
  for(i=0;i<m;i++)
  {
     for(j=0;j<n;j++)
     {
      scanf("%d",&a[i][j]);
      b[j][i]=a[i][j];
     }
  }
  printf("\nInputed Matrixes is:\n\n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
      printf("%-4d\t",a[i][j]);  //4 digit right alignment
    }
    printf("\n");
  }
  printf("\nTransposed matrix is:\n\n");
  for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
    {
      printf("%-4d\t",b[i][j]); //4 digit right alignment
    }
    printf("\n");
  }
  getch();
}


//Snap shot of output...




MULTIPLICATION of Two Matrices


   // *****MULTIPLICATION of Two Matrices*****//
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,sum,a[10][10],b[10][10],c[10][10],m,n,k=0;
clrscr();
printf("       *** MULTIPLICATION OF TWO MATRIXES ***");
printf("\nEnter the no. of rows & coloumns :\n");
scanf("%d%d",&m,&n);
if(m!=n)
printf("\nEnter The same no. of rows & coloumns !");
else
{
   printf("Enter the elements of 1st matrix :\n");
   for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
     scanf("%d",&a[i][j]);
   }
   printf("Enter the elements of 2nd matrix :\n");
   for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
     scanf("%d",&b[i][j]);
   }
   {
      while(k<n)
      {
       sum=0;
       for(i=0;i<m;i++)
       {
     for (j=0;j<n;j++)
     {
       for(k=0;k<n;k++)
       sum=sum+a[i][k]*b[k][j];
       c[i][j]=sum;
       sum=0;
     }
       }
     }
     printf("Result- Multipliccation of two Matrixes :\n");
     for(i=0;i<m;i++)
     {
       for(j=0;j<n;j++)
       printf("%d\t",c[i][j]);
       {
     printf("\n");
       }
     }
   }
     printf("\nThanks ! For using my Program.");
     getch();
  }
}


//Snap shot of output...




A Program for ADDITION of two matrix


//*****A program for ADDITION of two matrixes*****//


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],m,n;
clrscr();
printf("\t\t *** ADDITION OF MATRIXES ***");
printf("\nEnter the no. of row & coloumn :\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of 1st matrix:\n");
for(i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 scanf("%d",&a[i][j]);
}
printf("Enter the element of 2nd matrix :\n");
for(i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 c[i][j]=a[i][j]+b[i][j];
}
printf("Addition of both matirxes is :\n");
for(i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 printf("%d\t",c[i][j]);
 {
   printf("\n");
 }
}
printf("\nThanks! for using the programe.......@Devesh");
getch();


}


//output look like this....



Tuesday 13 December 2011

BST Creation, Insertion and Display


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


struct searchtree
{
   int element;
   struct searchtree *left,*right;
}*root;
typedef struct searchtree *node;
typedef int ElementType;


node insert(ElementType, node);
void makeempty();
void display(node, int);
void makeempty()
{
root = NULL;
}


void main()
{
int ch;
ElementType a;
makeempty();
while(1)
{


printf("\n1. Insert\n2. Display\n3. Exit\n");
                printf("\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{


case 1:
printf("Enter an element : ");
scanf("%d", &a);
root = insert(a, root);
break;
  case 2: if(root==NULL)
                 printf("\nEmpty tree");
                   else display(root, 1);
                   break;
       case 3: exit(0);
           default:printf("Invalid Choice");
      }
   }
}
node insert(ElementType x, node t)
{
if( t == NULL)
   {
t = (node)malloc(sizeof(node));
t->element = x;
t->left = t->right = NULL;


}else
{
    if(x < t->element) t->left = insert(x, t->left);
        else if(x > t->element) t->right = insert(x, t->right);
   }
   return t;
}


void display(node t,int level)
{
int i;
if(t)
{


display(t->right, level+1);
printf("\n");
for(i=0;i<level;i++)


printf(" ");
printf("%d", t->element);
display(t->left, level+1);


   }


}

Output look like this :


Saturday 10 December 2011

Human face or Smiley program in java using Applet


import java.awt.*;
import java.applet.*;


public class HumanFace extends Applet{
private int mouseX, mouseY;
private boolean mouseclicked = false;
public void init(){
setBackground(Color.GREEN);
}


public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillOval(40,40,120,150);
g.setColor(Color.BLACK);
g.drawOval(57,75,30,30);
g.drawOval(110,75,30,30);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.setColor(Color.RED);
g.drawLine(100,100,100,130);
g.fillRect(75,140,50,7);
if (mouseclicked) {
g.clearRect(75,140,50,7);
g.setColor(Color.BLUE);
g.fillRect(70,139,60,9);
//g.drawArc(40,100,100,50,0,-120);
mouseclicked = true;
}


}


public boolean mouseDown(Event e, int x, int y )
{
mouseX=x; mouseY=y;
mouseclicked = true;
repaint();
return true;
}
}
/* <applet code="HumanFace.class" height= "300" width="200" >
</applet> */


//Output look like this...