C Program to Delete duplicate elements from an array

C Program to Delete duplicate elements from an array

Program : To delete duplicate elements in an array
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,k,n;
clrscr();

printf("nEnter array size : ");
scanf("%d",&n);

printf("nAccept Numbers : ",n);
for(i=0;i<n;i++)
 scanf("%d",&a[i]);

clrscr();

printf("nOriginal array is : ");
for(i=0;i<n;i++)
 printf(" %d",a[i]);

printf("nUpdated array is  : ");
for(i=0;i<n;i++)
{
   for(j=i+1;j<n;)
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
             a[k]=a[k+1];
          n--;
      }
      else
         j++;
   }
}

for(i=0;i<n;i++)
    printf("%d ",a[i]);
getch();
}

Output :
Enter array size : 5
Accept Numbers : 1 2 2 3 4
Original array is : 1 2 2 3 4
Updated array is  : 1 2 3 4

C decrementing pointer

In the last chapter we have studied about the increment operation on pointer variable in this chapter we will study decrement operation on pointer variable.

Formula : ( After decrementing )

new_address = (current address) - i * size_of(data type)
Decrementation of Pointer Variable Depends Upon : data type of the Pointer variable

Example :

Data TypeOlder Address stored in pointerNext Address stored in pointer after incrementing (ptr–)
int10000998
float10000996
char10000999

Explanation :

  • Decrementing a pointer to an integer data will cause its value to be decremented by 2
  • This differs from compiler to compiler as memory required to store integer vary compiler to compiler

Pointer Program : Difference between two integer Pointers

#include<stdio.h>

int main(){

float *ptr1=(float *)1000;
float *ptr2=(float *)2000;

printf("\nDifference : %d",ptr2-ptr1);

return 0;
}
Output :
Difference : 250

Explanation :

  • Ptr1 and Ptr2 are two pointers which holds memory address of Float Variable.
  • Ptr2-Ptr1 will gives us number of floating point numbers that can be stored.
ptr2 - ptr1 = (2000 - 1000) / sizeof(float)
            = 1000 / 4
            = 250

Live Example 2:

#include<stdio.h>

struct var{
    char cvar;
    int ivar;
    float fvar;
};

int main(){

struct var *ptr1,*ptr2;

ptr1 = (struct var *)1000;
ptr2 = (struct var *)2000;

printf("Difference= %d",ptr2-ptr1);

return 0;
}

Output :

Difference = 142

Explanation :

ptr2-ptr1 = (2000 - 1000)/Sizeof(struct var)
          = 1000 / (1+2+4)
          = 1000 / 7
          = 142
C Program to Read integers into an array and Reversing them using Pointers

C Program to Read integers into an array and Reversing them using Pointers

Write a C program using pointers to read in an array of integers and print its elements in reverse order.

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();

ptr=&arr[0];

printf("Enter the size of array : ");
scanf("%d",&size);

printf("nEnter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }

ptr=&arr[size-1];

printf("nElements of array in reverse order are:n");

 for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

getch();
}
Output :
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

Program to read integers into an array and reversing them using pointers Explanation :

  1. We have declared one pointer variable and one array.
int size,i,arr[MAX];
int *ptr;
  1. Address of first element of array is stored inside pointer variable.
ptr=&arr[0];
  1. Accept Size of an Array.
printf("Enter the size of array : ");
scanf("%d",&size);
  1. Now we have accepted element one by one using for loop and scanf statement .
printf("nEnter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }
  1. Increment pointer variable so that it will then point to next element of array.
  2. After accepting all elements store address of last element inside pointer variable.
ptr=&arr[size-1];
  1. Again using reverse for loop and printf statement print an array.
for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

C incrementing pointer

Incrementing Pointer :

  1. Incrementing Pointer is generally used in array because we have contiguous memory in array and we know the contents of next memory location.
  2. Incrementing Pointer Variable Depends Upon data type of the Pointer variable

Formula : ( After incrementing )

new value =  current address +  i * size_of(data type)

Three Rules should be used to increment pointer -

Address + 1 = Address

Address++   = Address

++Address   = Address
Pictorial Representation :
Data TypeOlder Address stored in pointerNext Address stored in pointer after incrementing (ptr++)
int10001002
float10001004
char10001001

Explanation : Incremeting Pointer

  • Incrementing a pointer to an integer data will cause its value to be incremented by 2 .
  • This differs from compiler to compiler as memory required to store integervary compiler to compiler
Note to Remember : Increment and Decrement Operations on pointer should be used when we have Continues memory (in Array).

Live Example 1 : Increment Integer Pointer

#include<stdio.h>

int main(){

int *ptr=(int *)1000;

ptr=ptr+1;
printf("New Value of ptr : %u",ptr);

return 0;
}
Output :
New Value of ptr : 1002

Live Example 2 : Increment Double Pointer

#include<stdio.h>

int main(){

double *ptr=(double *)1000;

ptr=ptr+1;
printf("New Value of ptr : %u",ptr);

return 0;
}
Output :
New Value of ptr : 1004

Live Example 3 : Array of Pointer 

#include<stdio.h>

int main(){

float var[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];

ptr=&var;
printf("Value inside ptr : %u",ptr);

ptr=ptr+1;
printf("Value inside ptr : %u",ptr);

return 0;
}
Output :
Value inside ptr : 1000
Value inside ptr : 1020

Explanation :

Address of ptr[0] = 1000
We are storing Address of float array to ptr[0]. -
Address of ptr[1]
= Address of ptr[0] + (Size of Data Type)*(Size of Array)
= 1000 + (4 bytes) * (5)
= 1020
Address of Var[0]…Var[4] :
Address of var[0] = 1000
Address of var[1] = 1004
Address of var[2] = 1008
Address of var[3] = 1012
Address of var[4] = 1016

C Program to Implement Stack Operations Using Array

C Program to Implement Stack Operations Using Array

C Program to implement Stack Operations Using Stack

Program for implementing a stack using arrays.It involves
various operations such as push,pop,stack empty,stack full and
display.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
/* stack structure*/
struct stack  {
          int s[size];
          int top;
           }st;
//-------------------------------------
int stfull()
{
 if(st.top>=size-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
void push(int item)
{
 st.top++;
 st.s[st.top] =item;
}
//-------------------------------------
int stempty()
{
 if(st.top==-1)
    return 1;
 else
    return 0;
}
//-------------------------------------
int pop()
 {
 int item;
 item=st.s[st.top];
 st.top--;
 return(item);
 }
//-------------------------------------
void display()
{
 int i;
 if(stempty())
    printf("n Stack Is Empty!");
 else
  {
  for(i=st.top;i>=0;i--)
     printf("n%d",st.s[i]);
  }
}
//-------------------------------------
void main(void)
{
int item,choice;
char ans;
st.top=-1;
clrscr();
printf("ntt Implementation Of Stack");
do
{
 printf("n Main Menu");
 printf("n1.Pushn2.Popn3.Displayn4.exit");
 printf("n Enter Your Choice");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
        printf("n Enter The item to be pushed");
        scanf("%d",&item);
        if(stfull())
            printf("n Stack is Full!");
        else
            push(item);
        break;
  case 2:
        if(stempty())
            printf("n Empty stack!Underflow !!");
        else
          {
          item=pop();
          printf("n The popped element is %d",item);
          }
        break;
  case 3:
        display();
        break;
  case 4:
        exit(0);
 }
 printf("n Do You want To Continue?");
 ans=getche();
}while(ans =='Y'||ans =='y');
getch();
}

Explanation of the C Porgram : Stack Using Array

Step 1 : Declare One Stack Structure

#define size 5

struct stack
{
     int s[size];
     int top;
}st;
  1. We have created ‘stack’ structure.
  2. We have array of elements having size ‘size’
  3. To keep track of Topmost element we have declared top as structure member.

Step 2 : Push/Pop Operation

While pushing remember one thing in mind that we are incrementing the top and then adding element. and while removing or poping the element, we are firstly removing the element and then decrementing the top.
void push(int item) {
    st.top++;
    st.s[st.top] =item;
}