Friday, 30 March 2012

Programs - Pointer


Program -1 : Write a program to demostrate the use of Address (&) and Pointer (*)    operators

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

void main()
{
          int a,*p;
          clrscr();

          printf("Enter Any Value : ");
          scanf("%d",&a);

          p=&a;

          printf("\n\nValue Of a (1st way): %d",a);
          printf("\n\nAddress Of a: %u",&a);
          printf("\n\nAddress Of a: %u",p);
          printf("\n\nValue Of a (2nd way): %d",*p);
          printf("\n\nValue Of a (3rd way): %d",*(&a));
          printf("\n\nAddress Of p: %u",&p);
          getch();
}

Program -2  : Write a program to display the contents of the variable their address using pointer variable of different data type


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

{
int num,*intptr;
float x, *floptr;
char ch, *cptr;
clrscr();
num=1234;
x=12.34;
ch='a';
intptr=&num;
cptr=&ch;
floptr=&x;
printf("\n Num %d stored at address %u",*intptr,intptr);
printf("\n Value %f stored at address %u",*floptr,floptr);
printf("\n Character %c stored at address %u",*cptr,cptr);

getch();
}


Program -3  : Write a program to demostrate the use of increment and decrement  operation using pointer

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

void main()
{
int a,*p;

clrscr();

p=&a;

printf("\n\nEnter Any Value :");
scanf ("%d",p);

clrscr();

printf("\n\nValue Of a : %d",*p);

++(*p);
printf("\n\nValue After Increment : %d",*p);

--(*p);
printf("\n\nValue After Decrement : %d",*p);

getch();
}

Program -4  : Write a program to swap values using pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p,*q,*t;
clrscr();

printf("\n\nEnter The Value Of a :");
scanf ("%d",&a);
printf("\n\nEnter The Value Of b :");
scanf ("%d",&b);

p=&a;
q=&b;
   printf("\n Before swap");
   printf("\n a = value %d Address = %u",a,p);
   printf("\n b = value =%d Address =%u",b,q);

t=p;
p=q;
q=t;

printf("\n After swap");
printf("\n\n Value Of a : %d",*p);
printf("\n\n Value Of b : %d",*q);

getch();
}



Program -5  : Array and pointer

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

void main()
   {
      int a[] = {1,2,3,4,5};
      int *p,i;
      clrscr();

      p=&a[0];  // assign base address
      for(i=0;i<5;i++)
       {
            printf("\n Number  = %d Address = %u",*(p),p);  //*(p+i),p+i
            p++;
       }
       getch();
   }

Program -6  : Write a program to read and print value stored in one-dimensional array  using pointer.  

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

void main()
{

          int a[50],i,n,*p;
         
clrscr();

          printf("\n\n Enter how many number : ");
          scanf("%d",&n);
         
p=a;

          for(i=0;i<n;i++)
           {
                   printf("\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",p);   //a[i]
                   p++;
           }
          p=a;
          printf("\n ----------------------------\n");
          printf("\n  Address\t  Value");
          printf("\n ----------------------------");

         
         for(i=0;i<n;i++)
           {
                   printf("\n\n  %u \t a[%d] : %d",p, i, *p ); //a+i  *(a+i)
                                      p++;
           }
           
          getch();
}

Program -7  : Write a program using pointer to compute the sum and average of all  elements stored in an array.    

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

void main()
{
          int a[50],*p,i,n,sum=0;
          float avg;
          clrscr();

          printf("\n\n Enter How many number : ");
          scanf("%d",&n);

          for(i=0;i<n;i++)
            {
                   printf("\n\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);//p+i
            }
          p=a;
          printf("\n ----------------------------\n");
          printf("\n  Address\t  Value");
          printf("\n ----------------------------");

          for(i=0;i<n;i++)
           {
                   printf("\n\n  %u \t a[%d] : %d",p, i, *p);
                   sum = sum + *(p);
                   p++;
           }

          avg=(float)sum/n;

          printf("\n\n ----------------------------\n");
          printf("\n\t\t  Sum : %d \n",sum);
          printf("\n\t      Average : %f",avg);
          printf("\n ----------------------------");

          getch();
}

Program -8 : Write a program to count total  odd and even number and calculate sum and average of odd and even numbers using pointer and array.

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

void main()
{
          int a[50],*p,n,i,even=0,odd=0,oddsum=0,evensum=0;
          float oddavg,evenavg;
          clrscr();


          printf("\n\n Enter How many number : ");
          scanf("%d",&n);

          for(i=0;i<n;i++)
            {
                   printf("\n\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);
            }
          p=a;

          printf("\n ----------------------------\n");
          printf("\n  Address\t  Value");
          printf("\n ----------------------------");

          for(i=0;i<n;i++)
           {
                   printf("\n\n  %u \t a[%d] : %d",p, i, *p);
                   if(*(p)%2==0)
                   {
                   even++;
                   evensum=evensum+*p;
                   }
                   else
                   {
                   odd++;
                   oddsum=oddsum+*p;
                   }
                   p++;
           }
          oddavg=(float)oddsum/odd;
          evenavg=(float)evensum/even;

          printf("\n -----------------------\n");
          printf("\n Total No. Of Odd  : %d",odd);
          printf("\n Sum Of Odd        : %d",oddsum);
          printf("\n Average           : %f",oddavg);
          printf("\n -----------------------\n");
          printf("\n Total No. Of Even : %d",even);
          printf("\n Sum Of Even       : %d",evensum);
          printf("\n Average           : %f",evenavg);
          printf("\n -----------------------");

          getch();
}

Program -9  : Write a program to print positive and negative number using pointer   and array.

//#include<stdio.h>
//#include<conio.h>
void main()
{
          int a[50],*p,n,i,pos=0,neg=0;
          clrscr();

          printf("\n\n Enter How many number : ");
          scanf("%d",&n);

          for(i=0;i<n;i++)
          {
                   printf("\n\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);
          }
          p=a;

       for(i=0;i<n;i++)
           {
                   printf("\n\n  %u \t a[%d] : %d",p, i, *p);
                   if(*(p)>0)
                   {
                   pos++;
                   }
                   else
                   {
                   neg++;

                   }
           p++;
           }



printf("\n --------------------------\n");
printf("\n Total No. Of Positive : %d",pos);
printf("\n --------------------------\n");
printf("\n Total No. Of Negative : %d",neg);
printf("\n --------------------------");
getch();
}


Program -10 : Write a program to print sum of square of numbers using pointer and array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],*p,i,n,sum=0;
clrscr();
p=a;
printf("\n\n Enter The Value Of n : ");
scanf("%d",&n);
clrscr();
for(i=0;i<n;i++)
          {
          printf("\n\n Enter The Value Of a[%d] : ",i);
          scanf("%d",p+i);
          sum=sum+ (*(p+i) * *(p+i));
          }
clrscr();
printf("\n -----------------------------\n");
printf("\n Sum Of Square Of Number : %d",sum);
printf("\n -----------------------------\n");
getch();
}

Program -11  : Write a program to sort the numbers using array and pointer

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

void main()
{
          int a[50],*p,n,i,j,temp;
          clrscr();

          printf("\n\n Enter The Value Of n : ");
          scanf("%d",&n);

          p=a;
          for(i=0;i<n;i++)
           {
                   printf("\n\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",p+i);
           }

          p=a;
          printf("\n Unsorted list");
          for(i=0;i<n;i++)
           {
                   printf("\n %d",*(p+i));
           }
          // sorting

          for(i=0;i<n;i++)
           {
             for(j=i;j<n;j++)
              {
                if(*(p+i)>*(p+j))      // if(a[i]>a[j])
                 {
                     temp=*(p+i);      // temp=a[i]
                     *(p+i)= *(p+j);   // a[i]=a[j]
                     *(p+j)=temp;      // a[j]=temp
                 }
              }
          }

          p=a;
          printf("The Sorted Array is : \n");
          for(i=0;i<n;i++)
           {
                   printf("\n %d",*(p+i));
          }
          getch();
}

Program -12  : Write a program to find minimum and maximum number using array
   and pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
          int a[50],*p,i,n,min,max;
          clrscr();

          printf("\n\n Enter how many number : ");
          scanf("%d",&n);


          for(i=0;i<n;i++)
          {
                   printf("\n\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);
          }
          min=a[0];
          max=a[0];
          p=a;

          for(i=0;i<n;i++)
          {
           printf("\n %d",*p);
           if(*(p+i) < min)
              {
                   min = *(p);
              }
           if(*(p+i) > max)
              {
                   max = *(p);
              }
              p++;
          }

          printf("\n ----------------------\n");
          printf("\n Maximum Number is : %d",max);
          printf("\n ----------------------\n");
          printf("\n Minimum Number is : %d",min);
          printf("\n ----------------------\n");

          getch();

}

Program -13 : Write a program to create matrix using pointer

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

void main()
          {
                   int i,j;
                   int mat[3][3];
                   clrscr();

                   printf("\n Input \n" );
                   for(i=0;i<3;i++)
                   {
                       for(j=0;j<3;j++)
                        {
                             printf("\n enter element [%d][%d] : ",i+1,j+1);
                             scanf("%d",&mat[i][j]);
                        }
                   }


                   printf("\n output \n" );

                   for(i=0;i<3;i++)
                   {
                      for(j=0;j<3;j++)
                       {
                             printf("%4d",*(*(mat+i)+j));
                       }
                       printf("\n");
                   }
             getch();
          }


Program -14 : Write a program to find length of string  using  pointer

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

void main()
{

 char *s;
 int l=0;
 clrscr();
 printf("\n Enter any name : ");
 gets(s);


 printf("\n entered name : %s",s);

 while(*s!='\0')
 {
  printf("\n %c at address %u", *s,s);
  s++;
  l++;
 }

 printf("\n len = %d",l);
 s--;
 printf("\n Reverse");
 while(l>0)
  {
     printf("\n %c at address %u",*s,s);
     s--;
     l--;
  }
 getch();
}

Program – 15 : Write a program to print string  in reverse order using  pointer

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

void main()
{

 char *s;
 int l=0;
 clrscr();
 printf("\n Enter any name : ");
 gets(s);


 printf("\n entered name : %s",s);

 while(*s!='\0')
 {
  printf("\n %c at address %u", *s,s);
  s++;
  l++;
 }

 printf("\n len = %d",l);
 s--;
 printf("\n Reverse");
 while(l>0)
  {
     printf("\n %c at address %u",*s,s);
     s--;
     l--;
  }
 getch();
}

Program -16 : Write a program to check whether the string is palindrome or not

#include<stdio.h>
#include<conio.h>
void main()
{
          int l=0,i;
          char *name,*s1,*s2,*s3;

          clrscr();

          printf("\n Enter Name : ");
          gets(name);
          printf("\n Printed string : %s",name);
          s1=name;
          printf("\n----------------------------\n");
          while(*name!='\0')
          {
          printf("\n\n %c and address = %u",*name,name);
          name++;
          l++;
          }


          printf("\n\n length of string : %d",l);
          name--;
          s3='\0';
          s3=s2;
          printf("\n----------------------------\n");
          while(l>0)
          {
          *s2=*name;
          printf("\n\n%c and address = %u",*name,name);
          name--;
          l--;
          s2++;
          }

          s2=s3;
          printf("\n\n original string =%s ",s1);
          printf("\n\nreverce string  %s ",s2);
          printf("\n----------------------------\n");


           if(s1==s2)
                   printf("\n same string");
           else
                   printf("\n Palindrome  string");

          getch();

}
Program -17 : Array of pointers OR array of string

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

void main()
{
          char *name[5];
          int i;
          clrscr();
          for(i=0;i<5;i++)
         {
            printf("\n Enter name %d ",i+1);
            gets(name[i]);

         }
        for(i=0;i<5;i++)
        {
           printf("\n %s",name[i]);
        }
 getch();
 }


Program -18  : Write a program to read and print value stored in one-dimensional dynamic  array.(malloc) 

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

void main()
{
          int *a,i,n;
          clrscr();

          printf("\n\n Enter how many number : ");
          scanf("%d",&n);

          a=(int *)malloc(n*sizeof(int));

          for(i=0;i<n;i++)
           {
                   printf("\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);

           }

          printf("\n ----------------------------\n");
          printf("\n  Address\t  Value");
          printf("\n ----------------------------");

          for(i=0;i<n;i++)
           {
                   printf("\n %u a[%d] : %d",&a[i],i,a[i] );

          }
          getch();
}

Program -19  : structure to pointer

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

struct emp
{
          int no;
          char name[15];
          long int salary;
};

void main()
{
          struct emp *e;
          clrscr();

          printf("\n enter no     : ");
          scanf("%d",&e->no);
          fflush(stdin);
          printf("\n Enter name   : ");
          gets(e->name);
          printf("\n Enter salary : ");
          scanf("%ld",&e->salary);

          printf("\n No         Name           Salary");
          printf("\n %d   %s              %ld",e->no,e->name,e->salary);
          getch();
}



Program -20 : structure to pointer for one or more data

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

struct stud
{
int rno;
char name[20];
};

void main()
{
           struct stud *s;
           int i,j,n;
          clrscr();
          printf("Enter No Of Student : ");
          scanf("%d",&n);
          s=(struct stud *)malloc(n*sizeof(struct stud));
          for (i=0;i<n;i++)
          {
                   printf("\n\nEnter Roll No : ");
                   scanf("%d",&s->rno);
                   fflush(stdin);
                   printf("\nEnter Name : ");
                   gets(s->name);
                   s++;
          }
          s=s-n;
          for (i=0;i<n;i++)
          {
                   printf("\n\nRoll No : %d\tName :%s",s->rno,s->name);
                   s++;
          }
          getch();
}


Program -21 : Write a program for array of structure using dynamic memory allocation

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

struct result
{
int rno;
char m[3];
int tot;
};

void main()
{
Struct result *r,temp;
int i,j,n;

clrscr();


printf("Enter No Of Student : ");
scanf("%d",&n);

r=(result *)malloc(n*sizeof(result));

for (i=0;i<n;i++)
{
printf("\n\nEnter Roll No : ");
scanf("%d",&r->rno);
printf("\nEnter Mark1 : ");
scanf("%d",&(r->m[0]));
printf("\nEnter Mark1 : ");
scanf("%d",&(r->m[1]));
printf("\nEnter Mark1 : ");
scanf("%d",&(r->m[2]));
r++;
}

r=r-n;

clrscr();

printf("\n================================");
printf("\n||Roll No||Mark1||Mark2||Mark3||");
printf("\n================================");

for (i=0;i<n;i++)
{
printf("\n||  %d    ||  %d ||  %d ||  %d ||",r->rno,r->m[0],r->m[1],r->m[2]);
r++;
}

printf("\n================================");
getch();
}

Posted By : Ruchita Pandya

No comments:

Post a Comment