1/18
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Declaring Arrays
Accessing a Single Element of an Array
use an index value
counts[11] = counts[11] + 1;Â
printf(â%dâ,counts[7]);
once we have selected a single element using an index, we can treat that element as we would treat any simple variable of that type
Array Indexes
in C (and Java), the type of value used for an index is an int
Anywhere we would expect an index value, we could have:
a literal int value,
an int variable,
an expression that evaluated to an int,
a method call that returns an int.Â
counts[x] = 7;Â
counts[x+2] = 144;Â
z = z + counts[nextElement(4)];
Upper and Lower Bounds
Upper bound (upb): index of the last element
Lower bound (lwb): index of the first element
upb: 25; lwb: 0 for `countsâÂ
In C (and Java), the lwb is always zero
The possible (valid) values for an index lie in the range lwb .. upb
In this example, the index range would be 0 .. 25
Array and for loop
To initialize the counts array, we have a loop as follows:Â
for (int i = 0; i < 26; i++ )Â
counts[i] = 0;
Bounded Length Queue Using Arrays
Assume queue can have at most MAX_SIZE elements
Can use array of length MAX_SIZE
If enqueue(), when queue size is MAX_SIZE, it fails: âQueue Fullâ
2-D Arrays
Elements are accessed by 2 indexes, one for the row and one for the column
Creating an Empty 2-D Array in C
int rating[3][4];
Creating an Empty 2-D Array in Java
int[][] rating = new int[3][4];
2-D Array Indices in C
char c[2][3];Â
Generate each pair of indices with a âdouble loopâ or a loop within a loop
Double Loop in C
Setup 2-D Array
(24Ă24 array)
Initialising a 2-D Array
This code uses a âdouble loopâ to ensure that every element of the stats array is set to zero. And we add some dummy data
Examining Elements of 2-D Array
This code also uses a double loop to examine each element of the stats array
If the value of the element is not zero, it prints out the values of the two indices (r and c) and the value of the element
Summing the Total of Results in a 2-D Array
Pros of Arrays
We can use a single name to represent multiple data items of the same type
Random access â very fast
We can pick a valid index (at ârandomâ) from the index range of an array and very quickly âaccessâ the value stored at that position within the array
As opposed to sequentially going through the array
Cons of Arrays
Arrays are of fixed size â cannot be resized
If it is possible to dynamically allocate space, we can allocate bigger arrays but there is an associated cost of copying elements over
Insertions and deletions from arrays are costly
A similar cost to the one indicated above
#define
E.g. #define NUMBER_OF_LANGS 24
Allows us to associate a (meaningful) name or label with a value
Isnât a variable - It is a constant
Manifest Constants
If we use constants correctly, if the number of languages changes then we only have to change one line of code
#define NUMBER_OF_LANGS 30
recompile our code and the program should continue to function as expected
Even if we never have to change the number of languages, using a label or meaningful name increases the readability (and therefore understandability) of our code