Arrays

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

Declaring Arrays

<p></p>
2
New cards

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

3
New cards

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)];

4
New cards

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

5
New cards

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;

6
New cards

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”

7
New cards

2-D Arrays

Elements are accessed by 2 indexes, one for the row and one for the column

<p>Elements are accessed by 2 indexes, one for the row and one for the column</p>
8
New cards

Creating an Empty 2-D Array in C

int rating[3][4];

9
New cards

Creating an Empty 2-D Array in Java

int[][] rating = new int[3][4];

10
New cards

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

<p>char c[2][3];&nbsp;</p><p>Generate each pair of indices with a ‘double loop’ or a loop within a loop</p>
11
New cards

Double Loop in C

<p></p>
12
New cards

Setup 2-D Array

(24×24 array)

<p>(24×24 array)</p>
13
New cards

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

<p>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</p>
14
New cards

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

<p>This code also uses a double loop to examine each element of the stats array</p><p>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</p>
15
New cards

Summing the Total of Results in a 2-D Array

<p></p>
16
New cards

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

17
New cards

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

18
New cards

#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

19
New cards

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