answersLogoWhite

0

Why arrays are using?

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

1. An array cuts down on the number of variables that need to be declared.

For example, if you wanted to store the numbers 1 to 10 without using an array you would literally have to declare ten separate variables.

Dim int1 as Integer = 1

Dim int2 as Integer = 2

Dim int3 as Integer = 3

Dim int4 as Integer = 4

Dim int5 as Integer = 5

Dim int6 as Integer = 6

Dim int7 as Integer = 7

Dim int8 as Integer = 8

Dim int9 as Integer = 9

Dim int10 as Integer = 10

Which is monstrously difficult to read and use further along in your code. A better way of doing it would be to use one variable; an integer array.

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Using this example you use the same variable throughout your code and access different parts of it as follows;

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Dim intTemp as Integer

MsgBox(intArray(0))

MsgBox(intArray(4))

This example displays two textboxes which will display the number 1 and 5 to the user (Note that array starts at zero)

2. An array can be used in itterative procedures such as a For... Next loop.

For example if we use the previous example of an integer array containing numbers 1 through 10, and with this array we want to display every number to the user.

Without an array this is an almighty block of code with a minimum of 20 lines, 10 declaring each variable and 10 outputting it to the user. With an array this can be reduced to four lines.

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

For i = 0 to intArray.Length

MsgBox(intArray(i))

Next

The importance of this is that in our example we only have ten numbers stored, in the real world you could have hundreds. Declaring hundreds of variables and messing around with them will take up memory, obfuscate the code immensely and make it virtually unchangeable.

With the above example that array could be any length and contain a thousand variables and still function the same.

3. An array can be declared as a variable length.

Whilst it's all well good saying we have an example array containing numbers 1 through 10, it is unlikely to ever be that simple; we will always want to store an unknown amount of data at runtime.

Just declaring variables in the code wouldn't work for this because they would need to be hardcoded.

In this example we will pretend a form has been made with a listview containing a hundred items, when a user clicks a button we want to store everything they selected in the listview. (If you don't know what a listview is I would ask that question as well)

Dim intVarArray(ListView1.SelectedItems.Count - 1) as Integer

For i = 0 to ListView1.SelectedItems.Count - 1

intVarArray(i) = ListView1.SelectedItems(i).ToString

Next

To do this using nothing but variables would be near impossible as you wouldn't know how many items the user would select, as such you would have to declare a hundred variables and write hundreds of lines of code to do something that can be covered in four lines.

4. An array can be resized on the fly, either destroying or preserving existing data.

In the above example we created an array of variable length and then stored some data from a made up listview control, but it's possible when working in the real world we may need to add things to that variable.

We can Redim an array to change its length.

Dim intArray(9) as Integer

Redim intArray(14)

This declares a variable of an array of integers, sets the length to 10 and then redefines that array as an array of length 15. This will completely remake the variable destroying any data held within it.

Dim intArray(9) as Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Redim Preserve intArray(14)

intArray(10) = 11

intArray(11) = 12

intArray(12) = 13

intArray(13) = 14

intArray(14) = 15

This declares a variable of length 10 and stores the number 1 to 10 inside it, it then redefines the variable as length 15 but preserves the data within it (It then adds the number 11 to 15 to that variable).

5. An array can have multiple dimensions.

Dim intArray(, ) as String = {{"White", "Rabbit"}, {"Brown", "Bear"}, {"Blue", "Water"}}

This declares something more unique than any other type of single variable, a multidimensional array, and stores some data within it.

--
Ac532 Answer:

Arrays can be disposed by using the .Dispose which means all allocated memory toward using the array will be lost meaning that your program will run faster.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

An array is very useful to easily handle a large set of data, or a data set that has a variable size. Here is an example: To add 100 numbers, without an array, you would have to store data to 100 different variables, and then: sum = 0; sum += item001; sum += item002; sum += item003; sum += item004; ... sum += item100; ... a total of 100 lines. To add 1000 items, you need 1000 lines. If you don't know the number of items in advance, it is even worse, because you need to execute every single line conditionally. With an array, the above example gets much more compact: sum = 0; for (i = 0; i < 100; i++) sum += items[i];

An array is very useful to easily handle a large set of data, or a data set that has a variable size. Here is an example: To add 100 numbers, without an array, you would have to store data to 100 different variables, and then: sum = 0; sum += item001; sum += item002; sum += item003; sum += item004; ... sum += item100; ... a total of 100 lines. To add 1000 items, you need 1000 lines. If you don't know the number of items in advance, it is even worse, because you need to execute every single line conditionally. With an array, the above example gets much more compact: sum = 0; for (i = 0; i < 100; i++) sum += items[i];

An array is very useful to easily handle a large set of data, or a data set that has a variable size. Here is an example: To add 100 numbers, without an array, you would have to store data to 100 different variables, and then: sum = 0; sum += item001; sum += item002; sum += item003; sum += item004; ... sum += item100; ... a total of 100 lines. To add 1000 items, you need 1000 lines. If you don't know the number of items in advance, it is even worse, because you need to execute every single line conditionally. With an array, the above example gets much more compact: sum = 0; for (i = 0; i < 100; i++) sum += items[i];

An array is very useful to easily handle a large set of data, or a data set that has a variable size. Here is an example: To add 100 numbers, without an array, you would have to store data to 100 different variables, and then: sum = 0; sum += item001; sum += item002; sum += item003; sum += item004; ... sum += item100; ... a total of 100 lines. To add 1000 items, you need 1000 lines. If you don't know the number of items in advance, it is even worse, because you need to execute every single line conditionally. With an array, the above example gets much more compact: sum = 0; for (i = 0; i < 100; i++) sum += items[i];

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Arrays are useful for containing multiple values of the same type in a list that can be traversed forward and backward. The values in the array are stored one after another in the computer's memory, and are thus efficient for storing lists if all of the values are of the same type (i.e. char, int, float, etc.)

Static arrays are arrays that have a set number of elements. Take the following declaration:

char myname[64];

This sets up a variable called "myname" with 64 values of type "char". Or this:

int seconds[10];

This tells C you want a variable called "seconds" that has 10 integer values.

On the other hand, dynamic arrays are capable of growing and shrinking at the will of the programmer. This gets into the wonderful paradigm of memory allocation and pointers. However, the following is a simple example of dynamic arrays:

// create a pointer that is set to NULL (no data)

int *intlist=NULL;

// two integer variables containing how many integers are

// in the array and a counter

int numints=10, count;

// initialize "intlist" to contain 10 integers (using "numints")

intlist=(int*)malloc(numints*sizeof(int));

// set up "intlist" to contain the numbers 0 to 9

for (count=0; count<numints; count++) intlist[count]=count;

// increase the number of ints in the list

numints++;

// adjust the size of "intlist" to reflect the new number of ints

intlist=(int*)realloc(numints*sizeof(int));

// set the last element in "intlist" to 11

intlist[numints-1]=numints;

// free all allocated pointers when you're finished with them

// to avoid memory leaks

free(intlist);

See the related links below for more information on static and dynamic arrays.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

An array is very useful to easily handle a large set of data, or a data set that has a variable size. Here is an example: To add 100 numbers, without an array, you would have to store data to 100 different variables, and then: sum = 0; sum += item001; sum += item002; sum += item003; sum += item004; ... sum += item100; ... a total of 100 lines. To add 1000 items, you need 1000 lines. If you don't know the number of items in advance, it is even worse, because you need to execute every single line conditionally. With an array, the above example gets much more compact: sum = 0; for (i = 0; i < 100; i++) sum += items[i];

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why arrays are using?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What multiplication fact be found be using the arrays 2x9 and 5x9?

7x9 is the multiplication fact that can be found using the arrays 2x9 and 5x9.


What is the way by which you can make the user defined the size of the arrays?

By using the library function #define A[] we can define the size of arrays


What is the purpose of using arrays in C language?

The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.


What is memory leakage in terms of arrays?

leakage in arrays occur when you declare an array with big size and using only very few bytes.


Write a program to read your name and reverse it using arrays?

abdulrahman


What mult fact can be found by using the arrays 2X9 and 5X9?

9


How does using smaller arrays help you to multiply?

You have misread or misunderstood something.


What multiplication can be found by using the arrays for 2x 9 and 5x9?

7x9


How you can multiply 3x24 by using arrays?

Sir, your question is not clear. If you just want to multiply 3 and 24 then why are you trying to use arrays for such simple calculation.


Are arrays in C created on the stack or the heap?

That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.


What multiplcation fact can be found by using the arrays for 29 and 59?

They both cannot be


What are the differences between structures and arrays?

Arrays are collections of repeated data items. Structures are complex data items made up of other data items, including, potentially, other structures and arrays. You can, of course, also have arrays of structures. Array items can be accessed by using its subscript whereas structure items can be accessed using its dot or "arrow" operator in C, C++, C#, Java, and JavaScript.