C Program to Perform insertion Operation on one dimensional array
Code:
Sample Input and Output
In this article we are going to see how to write Perform insertion Operation on one dimensional array So Let's start the explanation of above program!
In the first line of code,we are including stdio.h header file which will help us to use some of the built-in functions like printf() and scanf() which are defined in this header file.this function will help us to perform some
basic operations like Taking user input from user,printing the statement etc.
In the next line of code,we have imported one more header file conio.h header file which provides us getch(),clrscr() and many more function which are defined in conio.h header file but we will be using the getch(),clrscr() only.The main use of getch() function is used to hold the screen during the execution of program if we don't use this statament in the traditional compiler's like Turbo C Compiler then the output will come and go so fast that we wan't be able to see it,so to overcome this we will use getch().
The use of clrscr() function is to clear the output screen of previously executed program.In the next line we have create main function with int return type so we can say that we have to return some value from main function in our case we will return 0.we will define all the code inside the main function itself.
In the first line of main function,we have declared some variable's like array a of type int and size 50 which we will use to store all the element of an array taken from user,pos variable is for position which will help us to insert element at that particular position.i will be used as iterative variable which we are going to use for for loop,n variable will be used to store total no.of element of an array taken from user,val variable will be used to take the value which user want to insert at particular position.
In the next line of code,we are printing "Enter no.of elements in array:" statement which will be printed as it is inside the double quote('') and we already known that whatever we put inside the double quote('') will be printed as it is.we have printed this statement to indicate the user that we have to provide some input then only the program will proceed since after the printf() statement we have written one scanf() statement which actually pause the execution unless the input is provided. here we are taking total no.of element of an array and storing the input in n variable,we are using %d to format specifier because n is of int(Integer) data type.
In the next line of code, we have wrote one more print statement which will help user to understand that how much element we have to provide printf("Enter %d elements\n",n); here if the value of n is 6 then the statement would be 'Enter 6 elements' from this,user will get to known that that we have to provide 6 elements,to take user input we will be iterating through for loop.
Let's understand iteration by iteration
the sample of n = 3
1st iteration
i=0 //(for(i=0;i<n;i++)
0<3 //Condition satisfied Therefore the control will go inside the block
scanf("%d",&a[0]) //The user input will be stored at a[0] since the value of i is 0
Increment the value of i
2nd iteration
i=1 //Incremented the value of i therefore i become 1 from 0.
1<3 //Condition satisfied Therefore the control will go inside the block
scanf("%d",&a[1]) //The user input will be stored at a[1] since the value of i is 1
Increment the value of i
3rd iteration
i=2 //Incremented the value of i therefore i become 2 from 1.
2<3 //Condition satisfied Therefore the control will go inside the block
scanf("%d",&a[2]) //The user input will be stored at a[2] since the value of i is 2
Increment the value of i
4th iteration
i=3 //Incremented the value of i therefore i become 3 from 2.
3<3 //Condition is not satisfied therefore the control will come outside the loop.
friends.