one dimensional array in c with example

C Program to Perform deletion Operation on one dimensional array                    


 

Code: 

#include <stdio.h>
#include<conio.h>
int main()
{
        int a[50],pos,i,n;
        printf("Enter no.of elements in array:");
        scanf("%d",&n);
        printf("Enter %d elements\n",n);
        for(i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
        }
        printf("Enter the location where You wish to delete an element:");
        scanf("%d",&pos);
        if(pos>=n+1)
        {
            printf("Deletion Not Possible");
            
        }
        else
        {
            for(i=pos-1;i<=n-1;i++)
            {
                a[i]=a[i+1];
            }
            printf("Resultant Array\n");
            for(i=0;i<n-1;i++)
            {
                printf("%d\t",a[i]);
            }
            
        }
        
        
        getch();
    return 0;
}


 

Sample Input and Output: 

Enter no.of elements in array:4                                                                                                      
Enter 4 elements                                                                                                                     
1                                                                                                                                    
5                                                                                                                                    
2                                                                                                                                    
4           
Enter the location where You wish to delete an element:2                                                                             
Resultant Array                                                                                                                      
1       2       4 

 

What is array?

Array is a collection of element of similar datatype.It store the element in contagious memory location.We can access array element by indexing.Let's understand the main motive of using the array.Let's Suppose you are creating one C Program and want to store the roll no of 10 Student so to achieve this,we have to create 10 variable with different name,it's ok to create 10 variable for doing purpose but if number of student increases then it would difficult to create variable for the same.So to Overcome this problem we use something called as array in which we need to create one variable with some predefined size(eg(total_no_of_student)).


Explanation of above program 

So Now Let's understand the code line by line,In the first line of code we have simply included one header file i.e stdio.h header file which help us to use some of the built-in function like printf(),scanf() etc.In the next line of our code we have included one more header file conio.h which helps us to use getch() and clrscr() function which we use to hold the screen and clear the output screen of previously executed program respectively.

In the next of line of code,we have  declared 3 integer variable and one array namely a[],pos,i,n, all of integer type ,here we will use array(a) to store all the element of an array,pos variable to store the position of which we have to delete the element.i variable for iteration purpose and n to store total no.of elements.

In the next line,we are simply printing one statement 'Enter no.of elements in array:' this statement will indicate the user that We have proceed some input  since,In the next line we are using scanf() function to take user input which indirectly pause the program execution and start only when the input is provided.To store user input we are using %d format specifier since it is integer type variable.

In the next line we have printed one statement printf('Enter %d element',n),the meaning of %d here is it will print the value of n,So it will be easier for user to get to known that how much input we have to provide because after this print statement we are iterating over for loop for taking user input of elements of an array.the for loop will be iterated from 0 to n-1,for eg if user provide n=4 then the for loop will be iterated 4 times and will take 4  element as input for an array.

As we known that the array a[50] is of integer data type therefore we will use %d format specifier in scanf() function.

In the next line of code we are printing one statement printf("Enter the location where You wish to delete an element:"); this statement will indicate the user that they have to put a position value of which they want to delete an element,to take user input we have scanf() in the next line only with %d format specifier since we are storing the input in pos(position) variable which is of int(Integer) type variable.


In the next line we have defined one condition if(pos>=n+1) the meaning of this condition is that if value of pos(position) variable is greater than or equal to the value of n+1 then go inside the if block and print the 'Deletion Not Possible' statement.Let's summarize the meaning of this condition if the value of position to be deleted is more than the length of array then it is not possible to delete an element because there is no element in that position.If the if-block is executed then the control of program will directly to the getch(); statement(second last line of our code).

If the if-condition is not satisfied then the else block will be executed in which The actual logic of our program will be executed in which we are deleting the element of particular position.


Let's understand the else block step by step

The first line in else Block contains one for loop in which we initialised the value of i with pos-1 variable this mean if you want to delete element stored at position 4 then the value of i will be 3 and the for loop condition is i<=n-1 this mean iterate te for loop till the value of i become n-1


for(i=pos-1;i<=n-1;i++)

 

Now let's go inside the for loop,In for loop we are shifting the element by 1 position so that the positioned element will be deleted 

By following statement a[i]=a[i+1]  


In the next section we are simply printing the line the updated array.



            



  Previous Next

Post a Comment

Previous Post Next Post