Arrays

0.0(0)
studied byStudied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:54 PM on 6/17/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

20 Terms

1
New cards

Declaring Arrays

int counts [26];
//int = data type
//counts = variable name
//[] = indicate were declaring an array
//26 = size - contains 26 integer elements

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

<p>Upper bound (upb): index of the last element</p><p>Lower bound (lwb): index of the first element</p><ul><li><p>upb: 25; lwb: 0 for `counts’&nbsp;</p></li></ul><p>In C (and Java), the lwb is always zero</p><p>The possible (valid) values for an index lie in the range lwb .. upb</p><p>In this example, the index range would be 0 .. 25</p>
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

<pre><code class="language-C">char c[2][3]; </code></pre><p>Generate each pair of indices with a ‘double loop’ or a loop within a loop</p>
11
New cards

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;
	}
}

<pre><code class="language-C">char c[2][3];

for (int row = 0; row &lt; 2; row++)
{
	for (int col = 0; col &lt; 3; col++)
	{
		print row, col;
	}
}</code></pre><p></p>
12
New cards

Setup 2-D Array

#define NUMBER_OF_LANGS 24

int counts[NUMBER_OF_LANGS][NUMBER_OF_LANGS];

24×24 array

13
New cards

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

14
New cards

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

15
New cards

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;
}

16
New cards

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;
}

17
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

18
New cards

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

19
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

20
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

Explore top notes

note
Elements Compounds and Mixtures
Updated 1411d ago
0.0(0)
note
anatomy
Updated 759d ago
0.0(0)
note
Chapter 25: The Urinary System
Updated 1060d ago
0.0(0)
note
Ionic and Covalent Bonds
Updated 1256d ago
0.0(0)
note
COUNTRIES AFFECTED AND EVENTS
Updated 1350d ago
0.0(0)
note
Lecture 2: Gene & Genetic Disease
Updated 493d ago
0.0(0)
note
Elements Compounds and Mixtures
Updated 1411d ago
0.0(0)
note
anatomy
Updated 759d ago
0.0(0)
note
Chapter 25: The Urinary System
Updated 1060d ago
0.0(0)
note
Ionic and Covalent Bonds
Updated 1256d ago
0.0(0)
note
COUNTRIES AFFECTED AND EVENTS
Updated 1350d ago
0.0(0)
note
Lecture 2: Gene & Genetic Disease
Updated 493d ago
0.0(0)

Explore top flashcards

flashcards
Spanish 2: Mi Rutina Diaria
50
Updated 872d ago
0.0(0)
flashcards
microbio chapters 4, 5 6
78
Updated 30d ago
0.0(0)
flashcards
vocabulario: las abuelas solares
46
Updated 1098d ago
0.0(0)
flashcards
Ordlista 1
30
Updated 523d ago
0.0(0)
flashcards
Functions of Brain Structures
22
Updated 669d ago
0.0(0)
flashcards
DKE Final Exam
106
Updated 736d ago
0.0(0)
flashcards
English Vocab 7
30
Updated 1005d ago
0.0(0)
flashcards
1000 Most Common SAT Words
991
Updated 478d ago
0.0(0)
flashcards
Spanish 2: Mi Rutina Diaria
50
Updated 872d ago
0.0(0)
flashcards
microbio chapters 4, 5 6
78
Updated 30d ago
0.0(0)
flashcards
vocabulario: las abuelas solares
46
Updated 1098d ago
0.0(0)
flashcards
Ordlista 1
30
Updated 523d ago
0.0(0)
flashcards
Functions of Brain Structures
22
Updated 669d ago
0.0(0)
flashcards
DKE Final Exam
106
Updated 736d ago
0.0(0)
flashcards
English Vocab 7
30
Updated 1005d ago
0.0(0)
flashcards
1000 Most Common SAT Words
991
Updated 478d ago
0.0(0)