Search

Demystifying Array Insertion:

Welcome to the demystifying series of programming. We shall go over programs and explain how’s and why’s of creating an instruction to create a program that becomes a better process. In this series, I will go through the foundation first to give easier understanding when we go over bigger one which are actually used to solve real problems. I would suggest you to code in parallel, believe me writing a code in practice will give you new doubts and questions which helps you understand even more. I’m using Xcode IDE on my MAC, you can choose any free IDE or can code in online IDE platform which are also really great.


If you’re a computer engineer you problems heard of an array unless you’re sleeping under a rock. Nevertheless, if you’re sleeping, an array is simply declaring bunch of variables of same data-type.

Example: int a, b, c, d, e;

If you want to declare multiple variable of same data-type, you might follow the above process but this is really TDS and never be practical when you build real world applications. Instead you can use the below declaration.

Int array[5] = {1, 2, 3, 4, 5};

In the above declaration we are simply created an integer array which can hold upto 5 variables. When you maintain a huge code arrays are the way to go for better probability and to save lot of time.


Array Insertion Program:

The instruction from the line 1 -6 are just comments. Comments are basically used to describe your program/instructions. These comments will be ignored by the compiler. Usually we use // to comment single line but if you want to comment multiple lines at the same time you can also use /* <Comments> */.


On the line 8 I just declared a standard input/output library. This header has all the printf and scanf API built-in. By the way don’t get confused with the term headers & libraries. Usually in C we call them headers but in java we call them libraries, just a terminology difference but the meaning is same.


On line 10 I have declared a macro “ARRAYSIZE” and assigned a value of 5. This I have declared just for better readability when you read the variable/code. These predefined macros will be compiled initially by the compiler.


Next when we come inside main, initially I have taken input from the user to assign values into the array using for loop. Later I have requested the position and element in where they want to include the array. Finally, I have just printed the modified array list with the values. Here you might get little confused with the for loop but you can modify it in much simpler term if you’ve would like to.


Output:

Here I have received input from the user to enter 5 values I.e. 1, 2, 3, 4, 5

Next the position to be replace the new value I.e. 3

Next the new element to be inserted into the position I.e. 11

Finally printed the modified/newly inserted array.