Tuesday, 20 June 2023

C Programming Chapter 7 Pointers

Pointers in C

        

 

           i                       location name

 

                      3                      value at location

 

                    65524               location number

 

ð    Write A Program To Show A Variable, Pointer And Its Average.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

      int i = 3 ;

      printf ( "\nAddress of i = %u", &i ) ;

      printf ( "\nValue of i = %d", i ) ;

      getch();

}

 

The output of the above program would be:

 

Address of i = 65524

Value of i = 3

 

ð    Write A Program To Show A Variable, Pointer And Its Average.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

      int i = 3 ;

 

      printf ( "\nAddress of i = %u", &i ) ;

      printf ( "\nValue of i = %d", i ) ;

      printf ( "\nValue of i = %d", *( &i ) ) ;

      getch();

}

 

The output of the above program would be:

 

Address of i = 65524

Value of i = 3

Value of i = 3

 

 

                                   i                          j

 

                                  3                       65524

 

                              65524                  65522

 

ð    Write A Program To Show A Variable, Pointer And Its Average.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

       int i = 3 ;

       int *j ;

 

       j = &i ;

       printf ( "\nAddress of i = %u", &i ) ;

       printf ( "\nAddress of i = %u", j ) ;

       printf ( "\nAddress of j = %u", &j ) ;

       printf ( "\nValue of j = %u", j ) ;

       printf ( "\nValue of i = %d", i ) ;

       printf ( "\nValue of i = %d", *( &i ) ) ;

       printf ( "\nValue of i = %d", *j ) ;

       getch();

}

 

Output:

 

Address of i = 65524

Address of i = 65524

Address of j = 65522

Value of j = 65524

Value of i = 3

Value of i = 3

Value of i = 3

 

ð    Write A Program To Show A Variable, Pointer And Its Average.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

       int i = 3, *j, **k ;

 

       j = &i ;

       k = &j ;

       printf ( "\nAddress of i = %u", &i ) ;

       printf ( "\nAddress of i = %u", j ) ;

       printf ( "\nAddress of i = %u", *k ) ;

       printf ( "\nAddress of j = %u", &j ) ;

       printf ( "\nAddress of j = %u", k ) ;

       printf ( "\nAddress of k = %u", &k ) ;

       printf ( "\nValue of j   = %u", j ) ;

       printf ( "\nValue of k    = %u", k ) ;

       printf ( "\nValue of i   = %d", i ) ;

       printf ( "\nValue of i   = %d", * ( &i ) ) ;

       printf ( "\nValue of i   = %d", *j ) ;

       printf ( "\nValue of i   = %d", **k ) ;

       getch();

}

 

The output of the above program would be:

 

Address of i = 65524

 

Address of i = 65524

Address of i = 65524

Address of j = 65522

Address of j = 65522

Address of k = 65520

Value of j    = 65524

Value of k    = 65522

Value of i  = 3

Value of i  = 3

Value of i  = 3

Value of i  = 3

 

                      i                             j                          k

 

                     3                        65524                65522

 

                  65524                  65522                 65520

 

ð    Write A Program To Swap Normally.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

       int a = 10, b = 20 ;

 

       swapv ( a, b ) ;

       printf ( "\na = %d b = %d", a, b ) ;

       getch();

}

 

swapv ( int    x, int  y )

{

       int t ;

 

       t = x ;

       x = y ;

       y = t ;

 

       printf ( "\nx = %d y = %d", x, y ) ;

}

 

The output of the above program would be:

x = 20 y = 10

a = 10 b = 20

 

ð    Write A Program To Swap Using Variable.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

       int a = 10, b = 20 ;

 

       swapr ( &a, &b ) ;

       printf ( "\na = %d b = %d", a, b ) ;

       getch();

}

 

swapr( int    *x, int  *y )

{

       int t ;

 

       t = *x ;

       *x = *y ;

       *y = t ;

}

 

The output of the above program would be:

 

a = 20 b = 10

 

ð    Write A Program With Pointers And Function To Calculate Area And Perimeter.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

       int radius ;

       float area, perimeter ;

 

       printf ( "\nEnter radius of a circle " ) ;

       scanf ( "%d", &radius ) ;

       areaperi ( radius, &area, &perimeter ) ;

 

       printf ( "Area = %f", area ) ;

       printf ( "\nPerimeter = %f", perimeter ) ;

       getch();

}

 

areaperi ( int   r, float  *a, float  *p )

{

       *a = 3.14 * r * r ;

       *p = 2 * 3.14 * r ;

}

 

And here is the output...

 

Enter radius of a circle 5

Area = 78.500000

Perimeter = 31.400000

 

 

ð    Following Is The Recursive Version Of The Function To Calculate The Factorial Value.

 

#include<stdio.h>

#include<conio.h>

void main( )

{

       int a, fact ;

 

       printf ( "\nEnter any number " ) ;

       scanf ( "%d", &a ) ;

 

       fact = rec ( a ) ;

       printf ( "Factorial value = %d", fact ) ;

       getch();

}

 

rec ( int    x )

{

       int f ;

 

       if ( x == 1 )

            return ( 1 ) ;

       else

            f = x * rec ( x - 1 ) ;

 

       return ( f ) ;

}

 

And here is the output for four runs of the program

 

Enter any number 1

Factorial value = 1

Enter any number 2

Factorial value = 2

Enter any number 3

Factorial value = 6

Enter any number 5

Factorial value = 120

 

ð  Accessing An Array Using Pointers

 

#include <stdio.h>

void printarr(int a[]);

void printdetail(int a[]);

void main()

{

    int a[5];

    for(int i = 0;i<5;i++)

    {

        a[i]=i;

    }

    printdetail(a);

getch();

}

 

void printarr(int a[])

{

    for(int i = 0;i<5;i++)

    {

        printf("value in array %d\n",a[i]);

    }

}

void printdetail(int a[])

{

    for(int i = 0;i<5;i++)

    {

        printf("value in array %d and address is %8u\n",a[i],&a[i]);

    }

}

 

void print_usingptr(int a[]) \\ A

{

    int *b;    \\ B

    b=a;               \\ C

    for(int i = 0;i<5;i++)

    {

        printf("value in array %d and address is %16lu\n",*b,b); \\ D

        b=b+2; \\E

    }

}

 

ð  MANIPULATING ARRAYS USING POINTERS

 
#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
void print_usingptr(int a[]);
void main()
{
    int a[5];
    for(int i = 0;i<5;i++)
    {
        a[i]=i;
    }
    print_usingptr(a);
getch();
}
 
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
 
void printdetail(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %8u\n",a[i],&a[i]);
    }
}
 
void print_usingptr(int a[])
{
    int *b;
    b=a;
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",*b,b);
        b++;                  // A
    }
}

 

ð  ANOTHER CASE OF MANIPULATING AN ARRAY USING POINTERS

#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
void print_usingptr_a(int a[]);
void main()
{
    int a[5];
    int *b;
    int *c;
    for(int i = 0;i<5;i++)
    {
        a[i]=i;
    }
    printarr(a);
    *b=2;            \\ A
    b++;             \\ B
    *b=4;            \\ C
    b++;
    *b=6;            \\ D
    b++;
    *b=8;            \\ E
    b++;
    *b=10;
    b++;
    *b=12;
    b++;
    a=c; //error     \\F
    printarr(a);
 
 
}
void printarr(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d\n",a[i]);
    }
}
void printdetail(int a[])
{
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",a[i],&a[i]);
    }
}
 
void print_usingptr_a(int a[])
{
 
    for(int i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",*a,a); \\ F
        a++; // increase by 2 bytes         \\ G
    }
}

 

ð  TWO-DIMENSIONAL ARRAY USING POINTERS

#include <stdio.h>
void printarr(int a[][]);
void printdetail(int a[][]);
void print_usingptr(int a[][]);
void main()
{
    int a[3][2];      \\ A
    for(int i = 0;i<3;i++)
        for(int j=0;j<2 ;j++)
        {
               {
                     a[i]=i;
               }
        }
    printdetail(a);
getch();
}
 
 
 
void printarr(int a[][])
{
    for(int i = 0;i<3;i++)
        for(int j=0;j<2;j++)
        {
               {
                      printf("value in array %d\n",a[i][j]);
               }
        }
}
 
void printdetail(int a[][])
{
    for(int i = 0;i<3;i++)
        for(int j=0;j<2;j++)
        {
               {
                      printf(
                      "value in array %d and address is %8u\n",
                      a[i][j],&a[i][j]);
               }
        }
}
 
void print_usingptr(int a[][])
{
    int *b;   \\ B
    b=a;                \\ C
    for(int i = 0;i<6;i++)   \\ D
    {
        printf("value in array %d and address is %16lu\n",*b,b);
        b++; // increase by 2 bytes \\ E
    }
}

 

ð    A Complete C Program For Implementing A List With Operations For Reading Values Of The Elements Of The List And Displaying Them Is Given Here:

#include<stdio.h>

#include<conio.h>

void main()

{

   void read(int *,int);

   void dis(int *,int);

   int a[5],i,sum=0;

 

   clrscr();

   printf("Enter the elements of array \n");

   read(a,5);      /*read the array*/

   printf("The array elements are \n");

   dis(a,5);

   getch();

}

 

void read(int c[],int i)

{

   int j;

   for(j=0;j<i;j++)

   scanf("%d",&c[j]);

   fflush(stdin);

}

 

void dis(int d[],int i)

{

   int j;

   for(j=0;j<i;j++)

      printf("%d ",d[j]);

   printf("\n");

}

Example

Input

Enter the elements of the first array

15

30

45

60

75

Output

The elements of the first array are

15 30 45 60 75

 

Manipulations On The List Implemented Using An Array

ð    Shown next are C programs for carrying out manipulations such as finding the sum of elements of an array, adding two arrays, and reversing an array.

ð  ADDITION OF THE ELEMENTS OF THE LIST
 
#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   int a[5],i,sum=0;
 
   clrscr();
   printf("Enter the elements of list \n");
   read(a,5);       /*read the list*/
   printf("The list elements are \n");
   dis(a,5);
   for(i=0;i<5;i++)
   {
      sum+=a[i];
   }
   printf("The sum of the elements of the list is %d\n",sum);
   getch();
}
 
void read(int c[],int i)
{
   int j;
   for(j=0;j<i;j++)
   scanf("%d",&c[j]);
   fflush(stdin);
}
 
void dis(int d[],int i)
{
   int j;
   for(j=0;j<i;j++)
   printf("%d ",d[j]);
   printf("\n");
}

Example

Input

Enter the elements of the first array

15
30
45
60
75
Output

The elements of the first array are

15 30 45 60 75

The sum of the elements of an array is 225.

Addition of the two lists

Suppose the first list is

1
2
3
4
5

and the second list is

5
6
8
9
10

The first element of first list is added to the first element of the second list, and the result of the addition is the first element of the third list.

In this example, 5 is added to 1, and the first element of third list is 6.

This step is repeated for all the elements of the lists and the resultant list after the addition is

6
8
11
13
15
 
   #include<stdio.h>
   #include<conio.h>
   void main()
   {
    void read(int *,int);
    void dis(int *,int);
    void add(int *,int *,int * ,int);
    int a[5],b[5],c[5],i;
 
    clrscr();
    printf("Enter the elements of first list \n");
    read(a,5);       /*read the first list*/
    printf("The elements of first list are \n");
    dis(a,5);  /*Display the first list*/
    printf("Enter the elements of second list \n");
    read(b,5);      /*read the second list*/
    printf("The elements of second list are \n");
    dis(b,5);  /*Display the second list*/
    add(a,b,c,i);
    printf("The resultant list is \n");
    dis(c,5);
    getch();
   }
 
   void add(int a[],int b[],int c[],int i)
   {
    for(i=0;i<5;i++)
      {
       c[i]=a[i]+b[i];
      }
   }
   void read(int c[],int i)
   {
    int j;
    for(j=0;j<i;j++)
    scanf("%d",&c[j]);
    fflush(stdin);
   }
 
   void dis(int d[],int i)
   {
    int j;
    for(j=0;j<i;j++)
    printf("%d ",d[j]);
    printf("\n");
   }

 

 

Example

Input

Enter the elements of the first list

1
2
3
4
5
Output

The elements of the first list are

2 3 4 5
Input

Enter the elements of the second list

6
7
8
9
10
Output

The elements of the second list are

6 7 8 9 10

The resultant list is

7 9 11 13 15
Inverse of the list

The following program makes a reverse version of the list.

#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void   dis(int *,int);
   void  inverse(int *,int);
 
   int a[5],i;
   clrscr();
   read(a,5);
   dis(a,5);
   inverse(a,5);
   dis(a,5);
   getch();
}
 
void read(int c[],int i)
{
   int j;
   printf("Enter the list \n");
   for(j=0;j<i;j++)
      scanf("%d",&c[j]);
   fflush(stdin);
}
void dis(int d[],int i)
{
    int j;
   printf("The list is \n");
   for(j=0;j<i;j++)
   printf("%d ",d[j]);
   printf("\n");
}
void inverse(int inver_a[],int j)
{
   int i,temp;
   j-;
   for(i=0;i<(j/2);i++)
   {
      temp=inver_a[i];
      inver_a[i]=inver_a[j];
      inver_a[j]=temp;
      j-;
   }
}

Example

Input

Enter the list

10
20
30
40
50
Output

The list is

10 20 30 40 50

The inverse of the list is

50 40 30 20 10

ð  This Is Another Version Of An Inverse Program, In Which Another List Is Used To Hold The Reversed List.

#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   void inverse(int *,int *,int);
   int a[5],b[5];
   clrscr();
   read(a,5);
   dis(a,5);
   inverse(a,b,5);
   dis(b,5);
   getch();
}
 
void read(int c[],int i)
{
   int j;
   printf("Enter the list \n");
   for(j=0;j<i;j++)
      scanf("%d",&c[j]);
   fflush(stdin);
}
void dis(int d[],int i)
{
   int j;
   printf("The list is \n");
   for(j=0;j<i;j++)
      printf("%d ",d[j]);
   printf("\n");
}
void inverse(int a[],int inverse_b[],int j)
{
   int i,k;
   k=j-1;
   for(i=0;i<j;i++)
   {
      inverse_b[i]=a[k];
      k-;
   }
}

Example

Input

Enter the list

10
20
30
40
50
Output

The list is

10 20 30 40 50

The inverse of the list is

50 40 30 20 10

 

 

ð  MERGING OF TWO SORTED LISTS

 
#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   void sort(int *,int);
   void merge(int *,int *,int *,int);
   int a[5],b[5],c[10];
   clrscr();
   printf("Enter the elements of first list \n");
   read(a,5);      /*read the list*/
   printf("The elements of first list are \n");
   dis(a,5);  /*Display the first list*/
   printf("Enter the elements of second list \n");
   read(b,5);   /*read the list*/
   printf("The elements of second list are \n");
   dis(b,5);  /*Display the second list*/
   sort(a,5);
   printf("The sorted list a is:\n");
   dis(a,5);
   sort(b,5);
   printf("The sorted list b is:\n");
   dis(b,5);
 
   merge(a,b,c,5);
   printf("The elements of merged list are \n");
   dis(c,10);  /*Display the merged list*/
   getch();
}
void read(int c[],int i)
{
   int j;
   for(j=0;j<i;j++)
      scanf("%d",&c[j]);
   fflush(stdin);
}
 
void dis(int d[],int i)
{
   int j;
   for(j=0;j<i;j++)
      printf("%d ",d[j]);
   printf("\n");
}
void sort(int arr[] ,int k)
{
   int temp;
   int i,j;
   for(i=0;i<k;i++)
   {
      for(j=0;j<k-i-1;j++)
      {
         if(arr[j]<arr[j+1])
         {
            temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
         }
      }
   }
}
void merge(int a[],int b[],int c[],int k)
{
   int ptra=0,ptrb=0,ptrc=0;
   while(ptra<k && ptrb<k)
   {
      if(a[ptra] < b[ptrb])
      {
            c[ptrc]=a[ptra];
         ptra++;
      }
      else
      {
            c[ptrc]=b[ptrb];
         ptrb++;
      }
      ptrc++;
   }
   while(ptra<k)
   {
      c[ptrc]=a[ptra];
      ptra++;ptrc++;
   }
   while(ptrb<k)
   {
      c[ptrc]=b[ptrb];
      ptrb++;  ptrc++;
   }
}

Example

Input

Enter the elements of the first list

10 20 25 50 63
Output

The elements of first list are

20 25 50 63
Input

Enter the elements of the second list

16 62 68 80
Output

The elements of second list are

12 16 62 68 80

The sorted list a is

63 50 25 20 10

The sorted list b is

80 68 62 16 12

The elements of the merged list are

80 68 63 62 50 25 20 16 12 10

 

 

 

ð  USING POINTERS TO QUICK SORT

#include <stdio.h>
#define MAX 10
void swap(int *x,int *y)
{
   int temp;
   temp = *x;
   *x = *y;
   *y = temp;
}
int getkeyposition(int i,int j )
{
   return((i+j) /2);
}
void qsort(int list[],int m,int n)
{
   int key,i,j,k;
   if( m < n)
   {
      k = getkeyposition(m,n);
      swap(&list[m],&list[k]);
      key = list[m];
      i = m+1;
      j = n;
      while(i <= j)
      {
         while((i <= n) && (list[i] <= key))
                i++;
         while((j >= m) && (list[j] > key))
                j-;
         if( i < j)
                swap(&list[i],&list[j]);
      }
      swap(&list[m],&list[j]);
      qsort(list[],m,j-l);
      qsort(list[],j+1,n);
   }
}
void readlist(int list[],int n)
{
   int i;
   printf("Enter the elements\n");
   for(i=0;i<n;i++)
       scanf("%d",&list[i]);
}
void printlist(int list[],int n)
{
   int i;
   printf("The elements of the list are: \n");
   for(i=0;i<n;i++)
       printf("%d\t",list[i]);
}
 
void main()
{
   int list[MAX], n;
   printf("Enter the number of elements in the list max = 10\n");
   scanf("%d",&n);
   readlist(list,n);
   printf("The list before sorting is:\n");
   printlist(list,n);
   qsort(list,0,n-1);
   printf("\nThe list after sorting is:\n");
   printlist(list,n);
}

Example

Input

Enter the number of elements in the list, max = 10

10

Enter the elements

7
99
23
11
65
43
23
21
21
77
Output

The list before sorting is:

The elements of the list are:

7 99 23 11 65 43 23 21 21 77

The list after sorting is:

The elements of the list are:

7 11 21 21 23 23 43 65 77 99

 

 

No comments:

Post a Comment

JAVA PROGRAMMING - OOPS CHAPTER 1

Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...