Understanding Arrays in Programming




The world of programming is full of wonders and problems. Most of the wonders are created due to said problems. It is a constant game of writing and testing anything that comes to your mind. People looking for app development training don’t go through with it a lot of times thinking they don’t know how to code. Well it is true programming is a major part of creating an app, but that does not mean you cannot learn it.

Today there are literally sources ranging in the six figures category that can help you learn programming at very affordable prices.

To understand arrays, let’s look at an example:

Taking a situation where there is a need to store five integers, using programming’s basic data and variable concept, 5 variables of the int data type will be written:

 

#include <stdio.h>

 

int main() {

   int number1;

   int number2;

   int number3;

   int number4;

   int number5;

  

  number 1 = 10;     

   number2 = 20;  

   number3 = 30;  

   number4 = 40;

   number5 = 50;    

 

   printf( "number1:  %d\n", number 1);

   printf( "number2:  %d\n", number 2);

   printf( "number3:  %d\n” , number3);

   printf( "number 4:  %d\n", number 4);

   printf( "number5 : %d\n", number5);

}

Easy isn’t it? Well suppose if we have to try the same thing with a 1000 variables, that’s where the basic int data input will have you pull your hair out, but not to worry, that’s where our friends the arrays come in.

An array is a structure that can store fixed size elements of the same data type. Here, we won’t be inputting individual variables like 1,2,4…,1000 but instead we declare and array variable called “number” of integer type and use number1 [0], number1 [1]… number1[100] for independent variable representation. Now, 0,1,2,3….,100 have been index associated with the var variable and will be representing individual elements available in this array.

Successfully Creating Arrays

Creating arrays in C, the programmer at can write the type of elements and their quantity to be stored in the specified array. For example:

Type arrayNAME [arraySize];

This is known as a single dimensional array. The “arraySize” will be an integer constant greater than zero.

Int number[15];

So here we have number as a variable array which can hold up to 15 integer numbers.

Array Initialization

 

Int number[5] = {10,20,30,40,50};

Using a similar single statement such as the one above, arrays in C can be initiated. Elements inside these brackets { } cannot exceed the number of elements declared in these [ ].

If you leave the [ ] brackets blank in the previous example, an array of the same type will be created for initialization.

Following will show you how to assign a single element included in the array:

number[4] = 50;

According to the statement above, the value assigned here will be 50 as the index begins with a 0 corresponding to number10, so logically number4 will contain 50.

Arrays For Java

When seeking to complete a Java developer certification, arrays are one of the most useful things you will be taught in the process.

Java has the ability to create arrays, but the process and code is a bit different than the ones in C as Jave uses the newoperator input.

Both examples for C and Java are shown below to execute programs that will give a similar output:

In C

#include <stdio.h>

 

int main () {

   int number[10]; /* number is an array of 10 integers */

   int i = 0;

 

   /* Initialize elements of array n to 0 */        

   while( i < 10 ) {

               

      /* Set element at location i to i + 100 */

      number[ i ] = i + 100;

      i = i + 1;

   }

  

   /* Output each array element's value */

   i = 0;

   while( i < 10 ) {

               

      printf("number[%d] = %d\n", i, number[i] );

      i = i + 1;

   }

  

   return 0;

}

When the above program is executed in C, you get the results:

number[0] = 100

number[1] = 101

number[2] = 102

number[3] = 103

number[4] = 104

number[5] = 105

number[6] = 106

number[7] = 107

number[8] = 108

number[9] = 109

In Java

public class DemoJava {

   public static void main(String []args) {

      int[] number = new int[10];

      int i = 0;

     

      while( i < 10 ) {

                               

         number[ i ] = i + 100;

         i = i + 1;

      }

 

      i = 0;

      while( i < 10 ) {

         System.out.format( "number[%d] = %d\n", i, number[i] );

         i = i + 1;

      }

   }

}

When you execute the above program in Java, you get the following results:

number[0] = 100

number[1] = 101

number[2] = 102

number[3] = 103

number[4] = 104

number[5] = 105

number[6] = 106

number[7] = 107

number[8] = 108

number[9] = 109

 

Both the languages are able to produce the same result for the same problem, but the method is a bit different. For those seeking app development training, learning about arrays is extremely important as they cut your tasks into half the work. All it takes is a bit of knowledge and a lot of practice and you will be creating in your apps code in no time.

About The Author
Travis
Account Manager (Northwestern United States) at QuickStart

Travis Hameed

Travis Hameed is an expert when it comes to navigating complex sales, and making prospects' lives easier and hassle-free. As a sales rep with over 6 years of experience, he has decided to be a part of QuickStart's blog and share his thoughts regarding the questions that he comes across frequently. When he is not helping customers with their IT skills training challenges, he loves to cook.