1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Declaring Arrays
int counts [26];
//int = data type
//counts = variable name
//[] = indicate were declaring an array
//26 = size - contains 26 integer elements
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
char c[2][3];
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 3; col++)
{
print row, col;
}
}
Setup 2-D Array
#define NUMBER_OF_LANGS 24
int counts[NUMBER_OF_LANGS][NUMBER_OF_LANGS];
24×24 array
Initialising a 2-D Array
void initCounts()
{
for (int r = 0; r < NUMBER_OF_LANGS; r++)
{
for (int c 0; c < NUMBER_OF_LANGS; c++)
{
counts[r][c] = 0;
}
}
counts[18][18] = 569;
counts[18][22] = 32;
counts[22][18] = 24;
counts[22][22] = 743;
}
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
void printCounts()
{
for (int r = 0; r < NUMBER_OF_LANGS; r++)
{
for (int c 0; c < NUMBER_OF_LANGS; c++)
{
if (counts[r][c] != 0)
{
printf("%d, %d = %d\n", r, c, counts[r][c]);
}
}
}
}
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
void getTotal()
{
int total = 0;
for (int r = 0; r < NUMBER_OF_LANGS; r++)
{
for (int c 0; c < NUMBER_OF_LANGS; c++)
{
total = total + counts[r][c];
}
}
return total;
}
Summing When the Row and Column are Equal in a 2D Array
int getTotalCorrect()
{
int correct = 0;
for (int l = 0, l < NUMBER_OF_LANGS; l++)
{
correct = correct + counts[l][l];
}
return correct;
}
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
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 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