Thursday, February 16, 2012

Pointer in C - A Brief Explanation.



A Pointer in C is a special kind of variable which can store address of another variable. ‘C’ is not an object oriented programming so creating objects are not allowed but use of pointer and function pointer allow C to be used in a more object orientated way; C doesn't support objects, but a structure can contain a collection of data and function pointers so the structure can effectively contain data and the functions needed to process it. A Pointer declare with ‘*’ and the unary operator `&' is used to produce the address of an object.

Reference operator (&)
When we declare a variable the amount of memory needed is assigned for it at a specific location in memory. To know the exact location of the variable we use & operator.

Dereference operator (*)
A variable which stores a reference to another variable is called a pointer. Pointer variable should be declare using deference operator.

Pointer Arithmetic’s
We can perform addition and subtraction operations with pointers. By addition and subtraction pointers move according to the size of the data type size occupied in the memory for which the said pointer is pointing.

int x, *p;
p = &x;   //Address of integer variable a stored to a Pointer *p

String with pointer.

       char *ch;
       char ch1[50];  
       ch = &ch1[0];    //Address of ‘0’ location stored to a Pointer character *ch.

Pointer and Array
int a[10],*p;
p=&a[0];

Function Pointer

Function Pointers are pointers type variables, which point to the address of a function.  The executable compiled program code and the used variables, are put inside this memory.

Example

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

   void main()
      {
           int a,*a1,b;
           clrscr();
           a=20;
           b=a;
           a1=&a;
           printf("  a = %d   &a = %u   a1  = %u a1 = %d  b = %d \n",a,&a,a1,*a1,b);
           a=a+5;
           printf("  a = %d   &a = %u   a1  = %u  a1 = %d  b = %d ",a,&a,a1,*a1,b);
           getch();
      }
Here address of variable ‘a’ is assigned t pointer ‘p’ and value stored to a variable ‘b’.

output  –

  a = 20   &a = 65524   a1  = 65524 a1 = 20  b = 20
  a = 25   &a = 65524   a1  = 65524  a1 = 25  b = 20

Output are the values of variable ‘a’ and the address of ‘a’ but in second case value of a change and variable ‘b’ is unchaged  but the pointer ‘a1’ that pointing to the variable ‘a’ is changes.

Pointers pointing to another Pointer

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

      void main()
           {
              int a,*i,**j,***k;
              clrscr();
              a=10;
              i=&a;
              j=&i;
              k=&j;
              printf(" a = %d  i = %u  j = %u    k = %u ",a,i,j,k);
              printf(" a = %d  i = %d  j = %d    k = %d ",a,*i,**j,***k);
              getch();
           }
Output -
       a = 10  i = 65524  j = 65522    k = 65520
       a = 10  i = 10  j = 10    k = 10

Pointer and String. Printing each character and counting the string length.

#include <stdio.h>
#include <conio.h>
  void main()
      {
           char *s="bay of bengal";
           int n;
           clrscr();
           for(n=0; *s != '\0'; s++ )
              {
                   printf("   %c\n ",*s);
                   n++;
              }
           printf(" Nos of character  %d",n);
           getch();
      }

Output -

   b
   a
   y

   o
   f

   b
   e
   n
   g
   a
   l
 Nos of character  13


Pointer and Array

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

  void main()
      {
           int arr[]={5,3,9,12}, *ptr;
           clrscr();
           ptr=&arr[0];
           printf("\n\tOutput – ")
           while(*ptr)
              {
                   printf("%d ",*ptr);
                   ptr++;
              }
           getch();
      }


         Output -  5 3 9 12


Thursday, February 9, 2012

Array [XVII] - 2-D Array – Spiral Matrix



Spiral Matrix or Circular Matrix - Produce a spiral array. A spiral array is a square arrangement of the first N2 natural numbers, where the numbers increase sequentially as you go around the edges of the array spiralling inwards.



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

  void main()
   {
      int ar[10][10];
      int i,j,r=0,n,size,c=0;
      clrscr();
      printf("\n\tEnter Size  : ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
           {
              for(j=0;j<n;j++)
                   {

                        ar[i][j]=0;
                   }
           }
      size=n;
      i=1;

      while(i<=size*size)
           {
              for(j=0;j<n;j++)
                   {
                        ar[r][c++]=i++;
                   }
              r++;
              c--;
              if(i<=size*size)
                   {
                        for(j=0;j<n-1;j++)
                            {
                                ar[r++][c]=i++;
                            }
                        r--;
                        c--;
                   }
              if(i<=size*size)
                   {
                        for(j=0;j<n-1;j++)
                            {
                                ar[r][c--]=i++;
                            }
                        r--;
                        c++;
                   }
              if(i<=size*size)
                   {
                        for(j=0;j<n-2;j++)
                            {
                                ar[r--][c]=i++;
                            }
                        r++;
                        c++;
                   }
              n=n-2;
           }

      printf("\n\tThe array->\n");
      for(i=0;i<size;i++)
           {
              printf("\n\t");
              for(j=0;j<size;j++)
                   {
                        printf("%3d ",ar[i][j]);
                   }
           }

      getch();
  }

Spiral Matrix - Inbound

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

 void main()
   {
      int ar[10][10];
      int i,j,r=0,n,size,c=0;
      clrscr();
      printf("\n\tEnter Size  : ");
      scanf("%d",&n);
      for(i=0;i<n;i++)
           {
              for(j=0;j<n;j++)
                   {

                        ar[i][j]=0;
                   }
           }
      size=n;
      i=size*size;
      c=r=size-1;

      while(i>=1)
           {
              for(j=0;j<n;j++)
                   {
                        ar[r--][c]=i--;
                   }
              r++;
              c--;
              if(i>=1)
                   {
                        for(j=0;j<n-1;j++)
                            {
                                ar[r][c--]=i--;
                            }
                        r++;
                        c++;
                   }
              if(i>=1)
                   {
                        for(j=0;j<n-1;j++)
                            {
                                ar[r++][c]=i--;
                            }
                        r--;
                        c++;
                   }
              if(i>=1)
                   {
                        for(j=0;j<n-2;j++)
                            {
                                ar[r][c++]=i--;
                            }
                        r--;
                        c--;
                   }
              n=n-2;
           }

      printf("\n\tThe array->\n");
      for(i=0;i<size;i++)
           {
              printf("\n\t");
              for(j=0;j<size;j++)
                   {
                        printf("%3d ",ar[i][j]);
                   }
           }

      getch();
  }

Thursday, February 2, 2012

Array [XVI] 2-D Array – Determinant & Saddle Point




The determinant of a matrix can be defined, for example using the Leibniz formula, for matrices with entries in any commutative ring. Briefly, a ring is a structure where addition, subtraction, and multiplication are defined. The commutativity requirement means that the product does not depend on the order of the two factors. There are various ways to define the determinant of a square matrix A, i.e. one with the same number of rows and columns. Perhaps the most natural way is expressed in terms of the columns of the matrix. If we write an n-by-n  matrix in terms of its column vectors. The determinant of a square matrix A is denoted by "det A" or | A |.


The determinant of a 2×2 matrix is found much like a pivot  operation. It is the product of the elements on the main diagonal minus the product of the elements off the main diagonal.
a    b   = ad - bc
c    d


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

#define MAX 10
  float detm(float a[][MAX],int S)
      {
           int i,j,k;
           float mult;
           float det=1;
           for(i=0;i<S;i++)
              {
                   for(j=0;j<S;j++)
                        {
                            printf("\n\t%f ",a[i][i]);
                            if(a[i][i]!=0)
                                {
                                      mult=a[j][i]/(float)a[i][i];
                                }
                            else
                                {
                                      return(0);
                                }
                            for(k=0;k<S;k++)
                                {
                                      if(i==j)
                                                {
                                                          break;
                                                }
                                      a[j][k]=a[j][k]-a[i][k]*mult;

                                }
                            }
                   }

           for(i=0;i<S;i++)
              {
                   det=det*a[i][i];
              }
           return det;
  }



  int chck(float a[][MAX],int S)
      {
           int i,j,k;
           float nz;
           for(i=0;i<S;i++)
              {
                   if(a[i][i]==0)
                        {
                            for(j=0;j<S;j++)
                                {
                                      if(a[i][j]!=0)
                                                {
                                                          k=j;
                                                          break;
                                                }
                                      if(j==(S))
                                                {
                                                          return 0;
                                                }
                                }
                            for(j=0;j<S;j++)
                                {
                                      a[j][i]=a[j][i]-a[j][k];
                                }
                        }
              }
           return 1;
      }
  void main()
      {
           float a[MAX][MAX],det;
           int i,j,size;
           clrscr();
           printf("\n\tEnter Size of Array [Below 10] : ");
           scanf("%d",&size);
           for(i=0;i<size;i++)
              {
                   for(j=0;j<size;j++)
                        {
                            printf("\tEnter Element at [%d,%d] :",i,j);
                            scanf("%f",&a[i][j]);
                        }
              }
           printf("\n\tArray : \n");
           for(i=0;i<size;i++)
              {
                   printf("\t");
                   for(j=0;j<size;j++)
                        {
                            printf("%5.2f :",a[i][j]);
                        }
                   printf("\n");
              }

           if(chck(a,size)==0)
              {
                   det=0;
              }
           else
              {
                   det=detm(a,size);
                   printf("\n\tDeterminent of Above Matrix is :  %f",det);
              }
      getch();
  }


Saddle Point - Given a RxC Matrix, where R rows and C columns, a Saddle-Point  is a number in the matrix that is smallest in its row and largest in its column.



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

  void main()
   {
      int a[10][10];
      int i,j,r=0,c=0,r1=0,max=0,c1=0,f=0,min;
      clrscr();
      printf("\n\tEnter No. of Row : ");
      scanf("%d",&r);
      printf("\n\tEnter No. of Column : ");
      scanf("%d",&c);
      for(i=0;i<r;i++)
           {
              printf("\n\tEnter %d elements for row %d   :  ",c,i+1);
              for(j=0;j<c;j++)
                   {

                        scanf("%d",&a[i][j]);
                        printf("\t\t\t\t\t");
                   }
           }
      printf("\n\tThe array->\n");
      for(i=0;i<r;i++)
           {
              printf("\n\t");
              for(j=0;j<c;j++)
                   {
                        printf("%3d ",a[i][j]);
                   }
           }
      for(i=0;i<r;i++)
           {
              max=a[i][0];
              for(j=0;j<c;j++)
                   {
                        if(a[i][j]<max)
                            {
                                max=a[i][j];
                                c1=j;
                            }
                   }
              for(j=0;j<r;j++)
                   {
                        if(a[j][c1]>max)
                            {
                                max=a[j][c1];
                                r1=j;
                            }
                   }
              printf("\n\t c1 %d r1 %d \n",c1,r1);
                        if(r1==i)
                            {
                                f=1;
                                printf("\n\t%d at %d   %d is saddle point",a[r1][c1],r1+1,c1+1);
                                break;
                            }
           }
      if(f==0)
           {
              printf("\n\tNo Saddle Point ");
           }
      getch();
  }