CIS3355:
Business Data Structures |
~Arrays~
Now you’re probably thinking, what the heck is an array?? -a good way of looking at it is thinking of a table, or a row in Excel like this one:
Defining an array would be requesting all that space for whatever you want to put into it.
If you’re familiar with your COBOL, you could define an array like this:
01 WS-ARRAY PIC X(10) OCCURS 10 TIMES.
In this case you are declaring a variable named “WS-ARRAY” that is one alpha character long, and will happen 10 times. (In other words, 10 consecutive blocks)
The definition: A data structure which contains a fixed number of contiguous storage elements. A one-dimensional array is referred to as a vector and multi-dimensional arrays are referred to as Matrices (or a Matrix).
In C/C++ an array can be defined like this:
int array [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
You can then picture the variable “array” as looking like this:
The numbers within the brackets( { }) are what you’re defining will be in each element in the array. Each block is called an element.
Questions:
1). If you declare the array: int yahoo[5]; There will be 0’s in each element of the array. True / False.
Answer: False. The array “yahoo” is not begin initialized with anything therefore whatever was in RAM before requesting that amount of space, will be inserted into those elements.
2). If you declare the array: int array[6]; What are we requesting? a. 60 bits of contiguous storage in RAM. b. 12 bits of contiguous storage in RAM. c. 12 bytes of contiguous storage in RAM. d. 12 bytes of RAM at random addresses. Answer: C
3). Just like COBOL the elements in the array are subscripted. The first element in an array would have what subscript? a. 1 b. 0 c. -1 d. none of the above Answer: B
4). What are the advantages of using arrays in programming? a. faster calculating b. less typing in defining variables & shorter program c. easy manipulation of large amounts of related data d. all of the above. Answer: D
5). How would you define an array called “utep”consisting of 40 bytes in total with the numbers 12,15,32,46,30 in any of its elements?
Answer: double utep [5] = {12,15,32,46,30};
References:
· Array http://www.brainyencyclopedia.com/encyclopedia/a/ar/array.html
· More array info
· Array and Array Dimensions http://cplus.about.com/library/weekly/aa040802e.htm
|