CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS C ARRAYS IN C DETAILS
    Question :
    Write a program to delete an element from an array?

    Posted by: pratyaksh on 8/19/2008

    Contact pratyaksh Contact pratyaksh
    Category Arrays in C Interview Questions
    Rating (0.3) By 1411 users
    Added on 8/19/2008
    Views 2505
    Rate it!
    Answers:

    Programming's = loads of fun!!!!!!!
    #include <stdio.h>
    #include <conio.h>
    #define size 10
    void main()
    { int a[size],del,j,i;
    clrscr();
    for(i=0;i<size;i++)
    {j=i+1;
    printf("
    Enter the %d element of array",j);
    scanf("%d",&a[i]);}
    printf("
    Enter the element to be deleted");
    scanf("%d",&del);
    del--;
    for(i=del;i<size-1;i++)
    a[i]=a[i+1];
    a[size-1]=0;
    printf("
    Element has been deleted");
    printf("
    Array now is.....
    ");
    for(i=0;i<size;i++)
    printf("%d ",a[i]);
    getch();
    }

    Another way of writing the program is to just assign value zero to the specified element.

    #include <stdio.h>
    #include <conio.h>
    #define size 10
    void main()
    { int a[size],del,j,i;
    clrscr();
    for(i=0;i<size;i++)
    {j=i+1;
    printf("
    Enter the %d element of array",j);
    scanf("%d",&a[i]);}
    printf("
    Enter the element to be deleted");
    scanf("%d",&del);
    /*Corresponding element assigned value zero*/
    a[--del]=0;
    for(i=0;i<size;i++)
    printf("%d ",a[i]);
    getch();
    }

    Stay COOOooolllllllllllll!!!!!!!



    Posted by: Pranjal Gururani    

    Contact Pranjal Gururani Contact Pranjal Gururani

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


    /* Program to remove one element from array*/
    int main()
    {
    int * array1;

    int size1;
    printf("What is original Size of Array
    ");
    scanf("%d", &size1);

    array1 = (int*)malloc(size1 * sizeof(int ));

    if(array1 == NULL)
    {
    fprintf(stderr, "out of memory
    ");
    exit;
    }

    for (int i = 0; i< size1; i++)
    {
    array1[i] = i;
    printf("Element %d : %d
    ",i, array1[i]);
    }





    int number;
    printf("Which element do you wish to remove
    ");
    scanf("%d", &number);

    for (i = number; i< size1-1; i++)
    {
    array1[i] = array1[i+1];
    }

    printf("





    ");

    realloc(array1,(size1-1) * sizeof(int ));

    for (int i = 0; i< size1-1; i++)
    {

    printf("Rearranged Element %d : %d
    ",i, array1[i]);
    }

    free(array1);


    return 0;
    }



    Posted by: rn    

    Contact rn Contact rn

    #include <stdio.h>
    //#include <conio.h>
    #include"stdafx.h"
    #include"malloc.h"
    //#define size 10
    void main()
    {
    int *a,*b,size, del,j=0,i;
    printf("enter the size of the array
    ");
    scanf("%d",&size);
    a=(int*)malloc(size*sizeof(int));
    b=(int*)malloc(size*sizeof(int));
    for(i=0;i<=size-1;i++)
    {
    printf("enter the value in an array
    ");
    scanf("%d",&a[i]);
    }
    printf("enter the value to be deleted
    ");
    scanf("%d", &del);
    for(i=0;i<=size-1;i++)
    {
    if(a[i]!=del)
    {
    b[j]=a[i];
    j++;
    }
    }
    for(i=0;i<j;i++)
    printf("%d
    ", b[i]);

    }



    Posted by: bhanu    

    Contact bhanu Contact bhanu

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int ar[10],i,loc;
    cout<<"enter array";
    for(i=0;i<10;i++)
    cin>>a[i];
    cout<<"enter loc to be deleted";
    cin>>loc;
    for(i=loc;i<10;i++)
    a[i]=a[i+1];
    cout<<"
    array after deletion";
    for(i=0;i<9;i++)
    cout<<a[i];
    getch();
    }



    Posted by: meena    

    Contact meena Contact meena

    #define MAX 5
    void main()
    {
    int input[MAX]={1,2,3,4,5};
    int *output;
    int key,c;
    int* fun(int[],int);
    clrscr();
    printf("THE ELEMEST U WANT TO DELETE");
    scanf("%d",&key);
    output=(int*)malloc(sizeof(int)* (MAX-1));
    output=fun(input,key);
    printf("after deletion:");
    for(c=0;c<MAX-1;c++)
    printf("%d ",*(output+c));
    getch();
    }
    int* fun(int temp1[],int hold)
    {

    static int temp2[MAX-1];
    int index2=0;
    int count;
    for(count=0;count<MAX;count++)
    if(temp1[count]!=hold)
    temp2[index2++]=temp1[count];
    return temp2;
    }



    Posted by: RAJU SAHANI    

    Contact RAJU SAHANI Contact RAJU SAHANI

    Programming's = loads of fun!!!!!!!
    #include <stdio.h>
    #include <conio.h>
    #define size 10
    void main()
    { int a[size],del,j,i;
    clrscr();
    for(i=0;i<size;i++)
    {j=i+1;
    printf("
    Enter the %d element of array",j);
    scanf("%d",&a[i]);}
    printf("
    Enter the element to be deleted");
    scanf("%d",&del);
    del--;
    for(i=del;i<size-1;i++)
    a[i]=a[i+1];
    a[size-1]=0;
    printf("
    Element has been deleted");
    printf("
    Array now is.....
    ");
    for(i=0;i<size;i++)
    printf("%d ",a[i]);
    getch();
    }



    Posted by: sudheer    

    Contact sudheer Contact sudheer

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int arr[50],i,del,n,f=0;
    clrscr();
    printf("
    enter the number of element you want to insert=");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("
    enter element=");
    scanf("%d",&arr[i]);
    }
    printf("
    enter the index of element you want to delete");
    scanf("%d",&del);
    for(i=0;i<n;i++);
    {
    if(arr[i]==del)
    f=1;
    if(f==1)
    {
    arr[i]=arr[i+1];
    }
    printf("
    %d",arr[i]);
    }



    Posted by: ashwini     

    Contact ashwini Contact ashwini

    ThiS iS SiMPle U tRy it

    main()
    {
    int a[10];
    int key,i,pos,j,n=10;
    printf("ENtER ThE ArrAY");
    for(i=0;i<n;i++)
    scanf("%d"a[i]);
    printf("/nenTer thE elEMeNt to
    DeletE");
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
    if(a[i]==key)
    {
    for(j=i;j<n-1;j++)
    {
    a[j]=a[j+1];
    }
    n--;
    }
    }
    }



    Posted by: S.SHanKAr    

    Contact S.SHanKAr Contact S.SHanKAr

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[10],i,pos,n;
    clrscr();
    printf(" Enter the size of array:");
    scanf("%d",&n);
    printf(" Enter the elements of array:");
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    printf(" Enter the position:");
    scanf("%d",&pos);
    pos=pos-1;
    for(i=pos;i<n;i++)
    {

    a[i]=a[i+1];
    a[n]=0;
    }
    printf(" The array with deleted number is:");
    for(i=0;i<n;i++)
    {
    printf("%d",a[i]);
    }

    getch();
    }



    Posted by: Sameer    

    Contact Sameer Contact Sameer

    you cant do that with out copying it into a new array.
    since array is not a linked list. hence we use linked list to do such things. you can make an array element zero but it docent mean that you deleted the element.



    Posted by: sagar    

    Contact sagar Contact sagar

    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int a[4],*p,n,i;
    printf("Enter the elements ");
    for(i=1;i<=4;i++)
    {
    scanf("%d",&a[i]);
    }
    p=a;
    printf(" Enter the position which u want to delete ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    printf("%d ",*p);
    p++;
    }
    p++;
    for(i=p+1;i<=4;i++)
    {
    printf("%d ",*p);
    p++;
    }
    getch();
    }



    Posted by: Biswajit Talukder    

    Contact Biswajit Talukder Contact Biswajit Talukder

    #include<stdio.h>
    #include<conio.h>
    void del(int b[],int n,int pos);
    void main()
    {
    int a[10,20,30,40,50},c,p;
    printf("elements before deletion");
    for(c=0;c<=4;c++);
    {
    printf("%d ",a[c]);
    }
    printf("enter position for deletion");
    scanf("%d ",a[c]);
    del(a,5,pos);
    printf("elements after deletion");
    for(c=0;c<=3;c++)
    {
    printf("%d ",a[c]);
    }
    getch();
    }
    void del(int b[],int n,int pos)
    {
    int c;
    c=p;
    while(c<n-1)
    {
    b[c]=b[c+1];
    c++;
    }
    }



    Posted by: RENUKA    

    Contact RENUKA Contact RENUKA

    /* Proram to delete an element from array*/
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    void main()
    {
    int max,i,a,l=1;
    int arr[500];
    clrscr();
    cout<<"enter the maximum no.... ->";
    cin>>max;
    if(max>=0)
    {
    cout<<" enter the no. of your choice... ";

    for(i=1;i<=max;i++)
    {
    printf("%d-> ",l);
    cin>>arr[i];
    l++;
    }
    cout<<" enter the no. of position which you want to delete..... -> ";
    cin>>a;
    if(a<=max)
    {
    cout<<" the updated array is " ";
    for(int b=1;b<a;b++)
    {
    cout<<arr[b]<<" ";
    }

    for(int j=(a+1);j<=max;j++)
    {
    cout<<" "<<arr[j];
    }
    cout<<"" ";
    }
    else
    cout<<"invalid psition";
    }
    else
    cout<<" you can't write negative number";
    getch();
    }



    Posted by: vishal chhodwani    

    Contact vishal chhodwani Contact vishal chhodwani

    9628625063



    Posted by: navaneet kumar    

    Contact navaneet kumar Contact navaneet kumar

    If you have the better answer, then send it to us. We will display your answer after the approval.
    Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    Write a program to insert an element in a linear array array?
    View Answer
    Does mentioning the array name gives the base address in all the integers?
    View Answer
    How would you use qsort() function to sort the name stored in an array of pointers to string?
    View Answer
    Write a program for n lines
    1 2 3 4 5
    16 17 18 19 6
    15 24 25 30 7
    14 23 22 21 8
    13 12 11 10 9
    View Answer
    What is the difference between null array and an empty array?
    View Answer
    Code for swapping of two numbers without using temporary variable using C.
    View Answer
    Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?


    View Answer
    Can the sizeof operator be used to tell the size of an array passed to a function?


    View Answer
    How to remove duplicate elements from an array

    View Answer
    How can you sort the elements of the array in descending order?
    View Answer
    Array is an lvalue or not?
    View Answer
    Can the size of an array be declared at runtime?
    View Answer
    Can the sizeof operator be used to tell the size of an array passed to a function?
    View Answer
    Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
    View Answer
    When does the compiler not implicitly generate the address of the first element of an array?
    View Answer


    Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

    View ALL Arrays in C Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2014 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0 Seconds