Question:
how to use arrays in programming?
anonymous
1970-01-01 00:00:00 UTC
how to use arrays in programming?
Eight answers:
anonymous
2007-09-08 11:00:29 UTC
You may find your answer here



http://www.freetutes.com/VisualBasic
lda
2007-09-07 16:31:35 UTC
Arrays are generally the fastest way to access a group of related data



double ages[] = { 10, 20, 25, 30, 50, 60, 70 };

double count = sizeof(ages) / sizeof(ages[0]);

double sum = 0;

for (int i = 0; i < count; ++i) {

sum += ages[i];

}

double average = sum / (double)count;
runFunning
2007-09-07 05:34:13 UTC
all of the above are very good answers... here's another angle...



You use variables to store a single value: someone's age, haircolor, the vidian boss's base damage, ect.



Arrays store a collection of similiar items: instead of using 23 variables ( and having to come up with 23 unique names )to describe something... you could use a single array to store that data and easily access it via loop conditions...



good luck
Katie
2007-09-07 05:22:26 UTC
It would probably help to mention the language you're using.



Anyway, here's an example in java:



Type[] identifier = new Type[index_count];

where Type can be any primitive type (int, float, char etc) or object (String, Socket, so on) in Java's case.

eg:

int[] ages = new int[5];



It's important to remember that the first index of an array is zero, and not 1 (in languages I've used, at least). So the five indexes are not 1 - 5, they are 0 - 4.



To access a value in an array, you simply use the syntax identifier[index].

For example:

my_age = ages[4];

In that example, my age is actually stored in the 5th and last index (remember that we start counting from zero, and not one!)



Anyway, I hope that has helped you :) Didn't bother with C/C++ because someone has done that one above. ;)
anonymous
2007-09-07 04:39:40 UTC
arrays store information.



var myA = new Array();

myA[0] = "summer";

myA[1] = "autumn";

myA[2] = "winter";

myA[3] = "spring";



alert("It is " + myA[2] + "where i am now");



or



var myA = new Array("bmw","toyota", "vw");



document.write("i would like to order " + myA[1]);

this would print "toyota".
Rk j
2007-09-07 04:37:41 UTC
It depends on the programming language you use.

Concept is just same everywhere but the syntax and symantics will differ.

For example in C the array can be declared as

int a[10]={1,2,3,4,5,6,7,8,9,0};

the above is the array of 10 integers, where 'a' is the name of array and 10 inside the square bracis is the size of the array.



The way to print the elements of the array :

for(i=0;i<10;i++)

printf("%d",a[i]);



will print all the elements of the integer array 'a'.



Arrays are again can be discusses as single dimensional and multidimensional.
anonymous
2007-09-07 04:53:37 UTC
arrays are two types , they are string array &general array

string array stores a string of characters & in general array

we can store only character.

if arrays are used as characters of specified length then

it is declared in subscript with prorer syntax depending on

languages which is to be used.

in general, the array should be declared in subscript.
Einstein
2007-09-07 04:42:33 UTC
Read the first part to get an overview. The rest is optional: a few simple examples.



An array is data structure (type of memory layout) that stores a collection of individual values that are of the same data type. Arrays are useful because instead of having to separately store related information in different variables (named memory locations), you can store them—as a collection—in just one variable. It is more efficient for a program to access and process the information in an array, than it is to deal with many separate variables.



All of the items placed into an array are automatically stored in adjacent memory locations. As the program retrieves the value of each item (or "element") of an array, it simply moves from one memory location to the very next—in a sequential manner. It doesn't have to keep jumping around to widely scattered memory locations in order to retrieve each item's value.



Imagine if you had to store—and later retrieve—the names of all of the registered voters in your city. You could create and name hundreds of thousands of distinct variable names to store the information. That would scatter hundreds of thousands of names all over memory. An alternative is to simply create a single variable that can will store all the same information, but in sequential, contiguous (adjacent) memory locations: an array.



------------------------------...



For example, if you have class with five students, and you want to store their test grades, you will create an array of the integer data type. Since you have five students, you will create a single array. This sets aside five sequential memory locations to hold the five scores. Each score is stored as an "element" in the array. The first score will be store at location (or "index") zero. The second score will be stored at array index equal to one. The third score will be stored at index equals two, and so on.



Let's name the array "Scores."



The student grades are: 70, 75, 80, 85, 90, and 100.



Let's store ( or "assign") the first grade: Scores (0) = 70.



Now, the second grade: Scores (1) = 75.



Assign the third grade: Scores (2) = 80, and so on.



Now the computer code can access the array Scores to get the value of each score.



For instance, to retrieve the first score, you need to tell the program in which memory location the data is stored, and then retrieve the data from that particular memory location. You do this by creating a new, named memory location (called a variable) . Let's name this variable firstScore.



Example: firstScore = Scores (0)

__________________



Let's say that you want to find the average score. You instruct the program to retrieve all of the grades, and then divide by five.



First create two more variables) : Sum and Average.



Initialized these new variables Sum = 0, Average = 0.



Now, let's find the average:



Sum = Score (0) + Score (1) + Score (2) + Score (3)

+ Score (4)

Average = Sum / 5

__________________



Further below is a shorter way to compute the average score—by using a loop. This is really where the power of arrays come into play.



// The code will automatically increase i by one, each time it finishes added a score to the cumulative sum.

// It will then loop back up, and get the next array element's value, located at array index equal to i + 1.

// It does this until all of the elements in the array have been processed. Then it automatically exits the loop.



For i = 0 to 4

Sum = Sum + Scores (i)

Next i



//Now compute the average:

Average = Sum / 5



__________________


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...