How to Initialize an Array

Identify the data that you want to work with.,Refine and decide the programming language that you will be using., Decide whether you will need to use a one-dimensional array or a two-dimensional array., Understand that a one-dimensional array has an...

9 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: Identify the data that you want to work with.

    Do you want to read in sixteen pupil's names? Use five duplicates? Arrays make it so that you don't have repetitive code because you'll be setting several data values into a single variable, instead of assigning each individual data value an individual variable, which in turn makes for efficient coding.,, Two-dimensional arrays are best suited for "tables of data"

    which are of the same type e.g. measurement conversions may be best suited to be stored in a two-dimensional array called "conversions." You could think of it like a graph that has a "Y-axis" and an "X-axis"., A single element contains one data item and the index is a number that refers to an array element e.g. "Steve" could be an element written in the second index. , With one-dimensional arrays, only the top boundary needs to be mentioned when you declare it.

    In that sense, you'll need to mention that, for example, you want to read in '8' data values, which are names of car manufacturers for an example.

    Therefore, you will need an array of '7' because the array's index starts at '0'i.e. 0, 1, 2, 3, 4, 5, 6, 7 are eight individual values., This can be done in the same statement that it has been declared in, and the elements can be assigned one at a time.  This is an example in "Pascal code":
    Var Car: array of string=('Honda'

    'Bentley'

    'Mercedes-Benz'

    'Peugeot'

    'BMW'

    'Renault'

    'Toyota'

    'Nissan');   **this declares the array index** Car:= 'Honda'; **this assigns the array element** Car:= 'Bentley';  Car:= 'Mercedes-Benz';  Car:= 'Peugeot'; .. and so on and so fourth. , This is another example, this time in "Visual Basic 2005 code" for a "console application":
    Dim Car() As String = {"Honda"

    "Bentley"

    "Mercedes-Benz"

    "Peugeot"

    "BMW"

    "Renault"

    "Toyota"

    "Nissan"}**declares the array elements as alphabetical characters** Dim Index as Integer**declares the array index as a 'whole number' For Index = 0 to 7**start of the conditional loop, also states the size of the array** Next For Index = 7 to 0 Step
    -1 Console.Writeline(Name(Index)) Next
  2. Step 2: Refine and decide the programming language that you will be using.

  3. Step 3: Decide whether you will need to use a one-dimensional array or a two-dimensional array.

  4. Step 4: Understand that a one-dimensional array has an index and an element.

  5. Step 5: Declare the array first

  6. Step 6: just as you would with any variable.

  7. Step 7: Initialize the array.

  8. Step 8: If you want to have access to each array in order

  9. Step 9: then you could create and use a loop to do so to prevent repetitive and unnecessary code.

Detailed Guide

Do you want to read in sixteen pupil's names? Use five duplicates? Arrays make it so that you don't have repetitive code because you'll be setting several data values into a single variable, instead of assigning each individual data value an individual variable, which in turn makes for efficient coding.,, Two-dimensional arrays are best suited for "tables of data"

which are of the same type e.g. measurement conversions may be best suited to be stored in a two-dimensional array called "conversions." You could think of it like a graph that has a "Y-axis" and an "X-axis"., A single element contains one data item and the index is a number that refers to an array element e.g. "Steve" could be an element written in the second index. , With one-dimensional arrays, only the top boundary needs to be mentioned when you declare it.

In that sense, you'll need to mention that, for example, you want to read in '8' data values, which are names of car manufacturers for an example.

Therefore, you will need an array of '7' because the array's index starts at '0'i.e. 0, 1, 2, 3, 4, 5, 6, 7 are eight individual values., This can be done in the same statement that it has been declared in, and the elements can be assigned one at a time.  This is an example in "Pascal code":
Var Car: array of string=('Honda'

'Bentley'

'Mercedes-Benz'

'Peugeot'

'BMW'

'Renault'

'Toyota'

'Nissan');   **this declares the array index** Car:= 'Honda'; **this assigns the array element** Car:= 'Bentley';  Car:= 'Mercedes-Benz';  Car:= 'Peugeot'; .. and so on and so fourth. , This is another example, this time in "Visual Basic 2005 code" for a "console application":
Dim Car() As String = {"Honda"

"Bentley"

"Mercedes-Benz"

"Peugeot"

"BMW"

"Renault"

"Toyota"

"Nissan"}**declares the array elements as alphabetical characters** Dim Index as Integer**declares the array index as a 'whole number' For Index = 0 to 7**start of the conditional loop, also states the size of the array** Next For Index = 7 to 0 Step
-1 Console.Writeline(Name(Index)) Next

About the Author

D

Diana Turner

Specializes in breaking down complex pet care topics into simple steps.

44 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: