Sunday, 16 August 2015

C language Pointers Tutorials


Pointers

   Pointer is a variable which stores address of another variable.

--> The syntax for declaring a pointer is as follows.

datatype  *<var_name>

   Eg:-   int  *a;

--> Function calls are of two types

1) call-by-value:

   Here the values of actual parameters get copied to formal parameters.

2) call-by-reference:

   Here the address of actual parameters get copied to formal parameters.

--> Any pointer variable required 2 bytes of memory irrespective of their datatype.

--> A pointer variable of some datatype can store address of a variable of same datatype i.e int * can store address of a variable, float * can store address of a float variable etc.

--> A pointer which is capable of storing address of a variable of any datatype is void *.

--> The address stored in a pointer in always being an unsigned integer.

--> When we have a pointer containing address of a variable, then we can access the value contained is that variable into the pointer using an operator called De-referencing operator, represented with the symbol *.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int x, *a;
      clrscr( );
      printf("\nEnter X Value: ");
      scanf("%d", &x);
      a = &x;
      printf("\n\tAddress: %u", a);
      printf("\n\tValue : %d", x);
      x = x + 7;
      printf("\n\tValue : %d", *a);
}

Advantages:

--> Instead of maintaining multiple copies of data, we can maintain a single copy and send its address to all the other peoples.

--> When large volumes of data is to be copied from one function to the other function instead of sending the entire data we can just send only the address, there by saving lot of time.

--> We can also allocate memory as much as we requires as and when required.

Dynamic memory allocation:

Allocating memory as much as we require as and when required is refered as dynamic memory allocation.

--> C language provide dynamic memory allocation using the following functions.

1) malloc( )

2) calloc( )

3) realloc( )

4) free( )

--> These functions are available in the header file alloc.h

1) malloc( ):

   Syntax:

            void * malloc(no. of bytes);

--> This function expect number of bytes for which the memory is to be allocated as argument.

--> It allocates the specified amount of memory and return its address.

--> It doesn't talk about what type of values to store in the allocated memory. So, its return type is void *

--> So, we need to type casting to the required pointer type when a pointer contain address of an array, then the pointer itself behavior like an array.

Program:

#include< stdio.h >
#include< conio.h >
#include< alloc.h >
void readarray(int *, int);
void printarray(int *, int);
void main( )
{

      int *a, n;
      clrscr( );
      printf("\nEnter N Value: ");
      scanf("%d",&n);
      a = (int *)malloc(n*sizeof(int));
      readarray(a, n);
      printarray(a, n);

}
void readarray(int *x, int n)
{

      int i;
      printf("\nEnter array elements: ");
      for(i=0;i<n;i++)
      scanf("%d",&x[i]);
}
void printarray(int *x, int n)
{

      int i;
      printf("\nThe elements in the array are:");
      for(i=0;i<n;i++)
      printf("%d ", x[i]);

}

2) calloc( ):

It is same as malloc( ) except that the values are initiated to zero of allocating the memory.

--> It is used to allocate multiple block of memory.

   Syntax:

            void * calloc(no. of elements, elements size);

3) realloc( ):

It is useful when we are required to adjust already allocated memory.

   Syntax:

            void * realloc(void *, size)

4) free( )

It is used to clear the allocated memory.

   Syntax:

            void  free(void *)




C language Operators and Expressions Tutorials

Operators:

   The symbol is used to perform an operation is called an operator.

--> An operator which expect only one operand is referred as Unary operator.

--> An operator which expect two operands is referred as Binary operator.

--> An operator which expect three operands is referred as Terinary operator.

--> The operators provided by C language are classified as follows.

1. Size operator:

   This operator is useful to retrieve the size of a datatype or a variable.

operator:    sizeof

   Syntax:

      sizeof( datatype/variable )

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x;

      clrscr( );

      x = sizeof(char);

      printf("\n\tCHAR: %d",x);

      x = sizeof(int);

      printf("\n\tINT: %d",x);

      x = sizeof(float);

      printf("\n\tFLOAT: %d",x);

      x = sizeof(double);

      printf("\n\tDOUBLE: %d",x);

      x = sizeof(long int);

      printf("\n\tLONG INT: %d",x);

      x = sizeof(long double);

      printf("\n\tLONG DOUBLE: %d",x);

}

2. Arithmetic operators:

   These operators are useful to perform arithmetic operators on the data.

--> The arithmetic operators are provided by C language as follows.

      + ------> Addition

      - ------> Subtraction

      * ------> Multiplication

      / ------> Division

--> When both the numerator and denomenator are integers, the division operator give only the quotient and will not considered the remainder

      % ------> Modulo or Remainder

--> It gives the remainder when two numbers are divided.

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x, y, a, b, c, d, e;

      clrscr( );

      printf("\nEnter two numbers: ");

      scanf("%d %d", &x, &y);

      a = x + y;

      b = x - y;

      c = x * y;

      d = x / y;

      e = x % y;

      printf("\n\t %d + %d : %d", x, y, a);

      printf("\n\t %d - %d : %d", x, y, b);

      printf("\n\t %d * %d : %d", x, y, c);

      printf("\n\t %d / %d : %d", x, y, d);

      printf("\n\t %d %% %d : %d", x, y, e);

}

Note:

--> When % is to be displayed in the output, we are required to specify %% in the format.

      + = ---> Add and Assign

      - = ---> Subtract and Assign

      * = ---> Multiply and Assign

      / = ---> Divide and Assign

      % = ---> Modulo and Assign

3) Increment/Decrement operators:

      ++ ----> Increment operator

      --   ----> Decrement operator

      x = x + 1 or x += 1 or x++

      x = x - 1 or x -= 1 or x--

--> Both the Increment and Decrement operators can be used in two ways as follows.

      x++ ---> post-increment

      ++x ---> pre-increment

      x--   ---> post-decrement

      --x   ---> pre-decrement

--> When we used as individual statement, both the pre and post operators appear to be same.

--> They vary when we used in other expressions or statements

--> When we used in expressions and other statements.

1) The pre-increment/pre-decrement assume latest updated value in that expression/statements

2) The post-increment/post-decrement assume pre-updated value or old value is that expression.

--> But in both the cases the value of that variable will be increment.

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x;

      clrscr( );

      printf("\nEnter a number: ");

      scanf("%d", &x);

      printf("\n\t%d %d %d %d %d", x++, ++x, x++, ++x, x++);

}

4) Type casting operator:

This is useful when a value of one type is to be consider as of another type temporarily for that expressions.

   Syntax:

      (type) value

Program:

#include< stdio.h >

#include< conio.h >

void main( )

{

      int x, y;

      float res;

      clrscr( );

      printf("\n\tEnter two numbers: ");

      scanf("%d %d", &x, &y);

      res = (float)x/y;

      printf("\n\tRESULT: %.2f", res);

}

5) Bit-wise operators:

These operators are useful to perform operations on the individual bits of the given data.

--> C language has a distinction of supporting special operators known as "Bit-wise operators" for manipulating of data at "bit level"

--> These operators are used for "testing the bits" or "shifting them" left or right--> Bit-wise operators may not be applied in "float or "double". They operate only "integers". Such as int, char, short, long int etc.

      operator      meaning

           >>           Right shift

           <<           Left shift

            ^            Bit-wise XOR

            ~            1's complement

            &            Bit-wise AND

            |             Bit-wise OR

--> Bit-wise operators are used for operators on individual bits.

1) Bit-wise AND:

This is useful to perform AND operator on the individual bits of the given data.

--> The truth table of Bit-wise AND operator is

    a      b      a&b

    0      0        0

    0      1        0

    1      0        0

    1      1        1

a = 0 0 0 0 0 1 1 1

b = 0 0 0 1 0 0 0 1

-------------------------

a&b = 0 0 0 0 0 0 0 1

-------------------------

2) Bit-wise OR operator:

This is useful to perform OR operation on the individual bits of the given data.

--> The truth table of Bit-wise OR operator is

    a      b      a|b

    0      0        0

    0      1        1

    1      0        1

    1      1        1

a = 0 0 0 0 0 1 1 1

b = 0 0 0 1 0 0 0 1

-------------------------

a|b = 0 0 0 1 0 1 1 1

-------------------------

3) Bit-wise XOR:

This is useful to perform XOR operation on the individual bits of the given data.

--> The truth table of Bit-wise XOR operator is

    a      b      a^b

    0      0        0

    0      1        1

    1      0        1

    1      1        0

a = 0 0 0 0 0 1 1 1

b = 0 0 0 1 0 0 0 1

-------------------------

a^b = 0 0 0 1 0 1 1 0

-------------------------

4) one's complement:

This is useful to perform 1's complement on the internal representation of the given data.

--> It is used to perform a unary operator.

a = 0 0 0 0 0 1 1 1

-------------------------

~a = 1 1 1 1 1 0 0 0

-------------------------

5) Left shift operator(<<):

This is useful to shift the bits in the internal representation of the given data towards left by the specified number of positions.

--> If the value is shifted by n positions towards left, then the value is raised by 2n

a = 0 0 0 0 1 1 0 1

a<<2

a = 0 0 1 1 0 1 0 0

6) Right shift operator(>>)

This is useful to shift the binary representation of the given value towards right by the specified number of positions.

--> If the value shifted by n-positions towards right, then the value will be decreased by 2n

a = 0 0 0 0 1 1 0 1

a>>2

a = 0 0 0 0 0 0 1 1

6) Relational operators:

These operators are useful to find the relation between the two given numbers.

      <     ---> Lessthan

      <=   ---> Lessthan or equal to

      >     ---> Greaterthan

      >=   ---> Greaterthan or equal to

      ==   ---> Equal to

      !=    ---> Not equal to

--> The relation operators are all Binary operators

--> They result in an integer value. That integer value will be either 0 or 1

--> '0' represents false i.e the condition doesn't hold

--> '1' represents true i.e the condition is satisfied

Note:

--> = is called Assignment operator.

--> It is useful to assign the RHS value to LHS variable

--> == is called comparison operator

--> It compares both the RHS and LHS value and return TRUE(1) if they are same, FALSE(0) otherwise.

7) Conditional operator:

--> The conditional operator is represented as "?" and ":"

   Syntax:

      (condition)?s1:s2

--> The condition is evaluated first

--> If it is TRUE, then the statements between ? and : will be executed.

--> If it is FALSE, then the statement after : will be executed.

Note:

--> "0" is considered as FALSE and any nonzero value is considered as TRUE.

--> We can specify as many number of statements as possible and required between ? and : they should be seperated by ",".

--> We can also specify as many number of statements as possible and required after column.

--> Among the statements specify after column the first statement will be executed only when the condition is false and the remaining statements will be executed all the times i.e when the condition is true or false.

--> Conditional operators can be nested i.e we can use conditional operator in another conditional operator.



Wednesday, 12 August 2015

C Program to read array of elements and print only odd numbers


Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
         int i, a[10];
         for( i = 0; i < 5; i++ )
         {
                    printf("Enter the value for a[ %d ]: ", i);
                    scanf("%d", &a[ i ]);
          }
          printf("\nThe array of Even elements are:\n");
          for( i = 0; i < 5; i++ )
          {
                    if( a[ i ] % 2 != 0 )
                    {
                               printf(" %d ", a[ i ] );
                     }
           }
}


Output:

Enter the value for a[ 0 ]: 1

Enter the value for a[ 1 ]: 2

Enter the value for a[ 2 ]: 3

Enter the value for a[ 3 ]: 4

Enter the value for a[ 4 ]: 5

The array of Even elements are:

1 3 5

C Program to read array of elements and print only even numbers


Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
         int i, a[10];
         for( i = 0; i < 5; i++ )
         {
                    printf("Enter the value for a[ %d ]: ", i);
                    scanf("%d", &a[ i ]);
          }
          printf("\nThe array of Even elements are:\n");
          for( i = 0; i < 5; i++ )
          {
                    if( a[ i ] % 2 == 0 )
                    {
                               printf(" %d ", a[ i ] );
                     }
           }
}


Output:

Enter the value for a[ 0 ]: 1

Enter the value for a[ 1 ]: 2

Enter the value for a[ 2 ]: 3

Enter the value for a[ 3 ]: 4

Enter the value for a[ 4 ]: 5

The array of Even elements are:

2 4








C program to read array of elements separated by comma and print array of elements



Input   : 2, 6, 7, 8, 1, 0
Output: 2 6 7 8 1 0

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
          int i = 0;
          char a[ ] = "2, 6, 7, 8, 1, 0";
          char *p, *b[ 10 ];
          p = strtok(a, ",");
          while( p != NULL )
          {
                 b[ i++ ] = p;
                 p = strtok( NULL, ",");
          }
          for( i = 0; i < 6; i++ )
          {
                 printf("%s", b[ i ]);
          }
}

Output:

2 6 7 8 1 0



C program to read and print array elements in reverse order



Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
         int a[ 5 ], i;
         for( i = 1; i < 5; i++)
        {
                printf("\nEnter the value for [ %d ]: ",i);
                scanf("%d", &a[ i ]);
         }
         printf("\nThe array of elements in reverse order:\n");
         for(i=5;i>0;i--)
         {
               printf("%d\t", a[ i ]);
         }
}


Output:

Enter the value for [ 0 ]: 0

Enter the value for [ 1 ]: 2

Enter the value for [ 2 ]: 1

Enter the value for [ 3 ]: 9

Enter the value for [ 4 ]: 7

The array of elements in reverse order:

7 9 1 2 0


C Program to read and print array of elements


Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
          int a[5], i;
          for( i = 0; i < 5; i++ )
         {
                  printf("\nEnter the value for a[%d]: ", i);
                  scanf("%d", &a[i]);
         }
         printf("\nThe array elements are: \n");
         for( i = 0; i < 5; i++ )
         {
                 printf("%d\t", a[i]);
         }
}

Output:

Enter the value for a[ 1 ]: 3

Enter the value for a[ 2 ]: 2

Enter the value for a[ 3 ]: 1

Enter the value for a[ 4 ]: 9

Enter the value for a[ 5 ]: 7

The array elements are:

3 2 1 9 7



C Program to print Data Type Size and Ranges


Program:

#include< stdio.h >
#include< conio.h >
#include< limits.h >
#include< float.h >
void main( )
{
            clrscr( ); // Clear the screen Contents
            printf("Integer data type:\n\tSize: %d\n\tRange: %d to %d", sizeof(int), INT_MIN, INT_MAX);
            printf("\nCharacter data type:\n\tSize: %d\n\tRange: %d to %d", sizeof(char), CHAR_MIN, CHAR_MAX);
            printf("\nFloat data type:\n\tSize: %d\n\tRange: %E to %E", sizeof(float), FLT_MIN, FLT_MAX);
            printf("\nDouble data type:\n\tSize: %d\n\tRange: %E to %E", sizeof(double), DBL_MIN, DBL_MAX);
}

Output:

Integer data type:
           Size: 2
           Range: -32768 to 32767
Character data type:
           Size: 1
           Range: -128 to 127
Float data type:
           Size: 4
           Range: 1.17549E - 38 to 3.402823E + 38
Double data type:
           Size: 8
           Range: 2.225074E - 308 to 1.797693E + 308



Thursday, 6 August 2015

Java program for multiplication of original matrix and transpose matrix


Input: s = 1
Matrix elements increased by one.
Original Matrix:

1  2  3 
4  5  6
7  8  9

Transpose Matrix:

1  4  7
2  5  8
3  6  9

Multiplication of two matrices:

14  32  50
32  77  122
50  122  194

Program:

import java.io.*;
import java.util.*;
public class matmul
{
          public static void main( String[ ] args ) {
          Scanner n = new Scanner( System.in );
          int s, i, j, k;
          int a[ ][ ] = new int[ 10 ] [ 10 ];
          int b[ ][ ] = new int[ 10 ][ 10 ];
          int mul[ ][ ] = new int[ 10 ][ 10 ];
          System.out.println( "Enter S value: " );
          s = n.nextInt( );
          for( i = 0 ; i < 3 ; i++)
         {
                 for( j = 0 ; j < 3 ; j++ )
                {
                        a[ i ][ j ] = s;
                        s = s + 1;
                 }
          }
          System.out.println( "Original Matrix: " );
          for( i = 0 ; i < 3 ; i++ )
          {
                  for( j = 0 ; j < 3 ; j++ )
                 {
                        System.out.print( a[ i ][ j ] + "  " );
                  }
                  System.out.println("");
           }
           for( i = 0 ; i < 3 ; i++ )
          {
                   for( j = 0 ; j < 3 ; j++ )
                  {
                         b[ i ][ j ] = a[ j ][ i ];
                   }
           }
           System.out.println( "Traspose Matrix:" );
           for( i = 0 ; i < 3 ; i++)
           {
                   for( j = 0 ; j < 3 ; j++)
                  {
                          System.out.print( b[ i ][ j ] + "  " );
                   }
                   System.out.println( "" );
            }
            for(i=0 ; i < 3 ; i++ )
           {
                    for( j = 0 ; j < 3 ; j++)
                   {
                           for( k = 0 ; k < 3 ; k++ )
                          {
                                  mul[ i ] [ j ] = mul[ i ] [ j ] + ( a[ i ] [ k ] * b[ k ] [ j ] );
                           }
                    }
             }
             System.out.println( "Multiplication of two matrices: " );
             for( i = 0 ; i < 3 ; i++ )
            {
                    for( j = 0 ; j < 3 ; j++ )
                   {
                            System.out.print( mul[i][j] + "  " );
                    }
                    System.out.println( " " );
             }
       }
}



C Program to print prime numbers upto the given Range

C Program to print PRIME Numbers upto the given Range.
Input: Enter N Value: 11
Output: 2, 3, 5, 7, 11


C Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
       int i, j, n;
       printf("\nEnter N Value: ");
       scanf("%d", &n);
       for( i = 1 ; i <= n ; i++ )
      {
              int c = 0 ;
              for( j = 1 ; j < = i ; j++ )
             {
                     if( i%j == 0)
                     {
                             c = c + 1;
                     }
              }
              if( c == 2 )
             {
                      printf(" %d ", i);
                      if( i < n )
                     {
                              printf(",");
                      }
              }
        }
}

Output:

Enter N Value: 11
2,3,5,7,11