Arrays

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

1/36

flashcard set

Earn XP

Description and Tags

Information about c++

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

37 Terms

1
New cards

What is an array?

An array is a block of contiguous memory locations to hold values of a single specified type. 

2
New cards

What type of values can an array hold?

Array can hold values of ANY type.

3
New cards

What is an arrays index base?

Arrays are 0 based, this means that the first element has index 0. 

4
New cards

What structure is used when declaring an array? 

To declare an array, use the following structure  

*type variable_name[kSize]; 

*Ex. Int my_array[5] 

5
New cards

Name each part of this declared array

Int my_array[5]

Int- the data type

my_array- the variable name

[5]- the size of the array

6
New cards

Explain what this array is doing

Int my_array[5]

Explanation: declares an array named my-array that can hold 5 integers 0, 1, 2, 3, 4. 

7
New cards

What type of arrays are we using? 

Static Arrays

8
New cards

What must be declared when using a static array and is it allowed to be changed during the program?

The size must be set when the array is declared and cannot be changed during the course of the program.  

9
New cards

What kind of variables should be used for the array size?

Use constant variables for the array’s size so that modifications to the size can be made in one place.  

10
New cards

In c++ what does the name of the array refer to?

  The name of the array gives the address of the first element (i.e., a pointer to the beginning of the array). 

11
New cards

int x[5] = {3, 7, 2};
cout << x[4]; // what is the output?
cout << x; // what is the output?

  1. The out would be 0 since there are only 3 elements in the array, so there fore the rest of the values will be set to 0.

  2. x prints out the memory address. So it would look something like “0x7ffee4c37000”.

12
New cards

What should be done in order to access and element in an array?

In order to access an element in the array, follow the name of the array with square brackets containing the elements index. 

13
New cards

True or False: Declaring an array automatically initializes the values within the array

False. Declaring an array does not automatically initialize the values in an array the programmer has to initialize the values. 

14
New cards

How can we initialize an array with values?

We can initialize our array in the declaration statement by assigning it a list of values, the list must be held in braces { }. The values in the list must be separated by commas.  

Example: int my_array[5] = {14, 3, 7, 0, 1};

Note: You can only initialize the array with a list in the array declaration.   

15
New cards

What will happen to any values that are left out of the list? 

*If we are initializing an array of numeric values, any values left out of the list will be set to 0.  

Example: int my_array[5] = {14}; 

14 0 0 0 0  

16
New cards

Can you leave out the size of an array and if so what will this print?

int my_array[ ] = {14, 3, 6, 9, 2}; 

You can leave out the size of the array when using a list to initialize the array in the declaration. The array’s size will be the number of values in the initialization list.  

Example: int my_array[ ] = {14, 3, 6, 9, 2}; 

14 3 6 9 2  

17
New cards

Example: 
for( int i = 0; i < 5; ++i )  { 
cout << “Enter next number”; 
cin >> my_array[ i ]; 

What does this code do and what will be its output?

To read values into a non-character array, use a counter-controlled loop to move through the array indices. 

18
New cards

Example: 
for ( int i = 0; i < 5; ++i )  { 
cout << my_array[ i ] << “ “; 

What does this code do and what will be its output?

To output the values in a non-character array, use a counter-controlled loop to move through the array.

19
New cards

What does it mean to overstep an array?

Overstepping an arrays bounds can attempt to modify values in memory locations being used by other variables or programs.

*Your compiler will not produce an error if an index is used outside of the 0 to size – 1 range.

Example:
int my_array[] = {1,2,3,4,5};
for ( int i = 0; i <=5; ++i ) {
my_array[i] *= 2;
}
Before: 1 2 3 4 5
After:
2 4 6 8 10 ???

20
New cards

How do you specify an array argument? 

To specify an array argument,
place a set of empty brackets
beside the array’s data type in the
function’s parameter list (for
single-subscripted arrays).
Example:
void DoubleValues( int [], int );

21
New cards

When calling the function, use the
name of the array (no brackets) as
an argument of the function.

Write out how this would look?

Example:
int my_array[8]={1,2,3,4,5,6,7,8};
DoubleValues( my_array, 8 );

22
New cards

When passing an array argument to a function what must be sent?

When passing an array argument
to a function we’re sending the
address of the first element in the
array, allowing the function direct
access to the array elements.
• You can make array and reference
parameters constant.
principle of least privilege
void PrintArray( const int [], int );

23
New cards

What is Linear Search regarding arrays?

Start at one end of the array.
If the current element contains the
search value, return its array index
If not, move to the next element.
Continue until the element is found
or there are no more elements to
examine.

24
New cards

What is a binary search regarding arrays?

If the middle element contains
the value you’re searching for
return its array index.
Else, if the middle element is
larger than the value you’re
searching for, search the left
sub-array.
Else search the right sub-array.

25
New cards

What is a bubble sort regarding arrays?

Start at the end of the array swapping
all pairs of unordered elements until
you reach the beginning of the array.
Now the smallest element is at the
front.
Repeat the process until all of the
elements are in order.

26
New cards

What is a selection sort regarding arrays?

front = position 0
Find the smallest element and swap it
to the front. Increment the front
Repeat until front = size - 1

27
New cards

What is insertion sort regarding arrays?

sorted = 1
insert element the next element into
its correct position within the first sorted elements
Repeat until sorted = size

28
New cards

How do you access individual arrays?

You use the square brackets [] to access individual elements of an array by index.

Example:

int x = numbers[0]; // x = 10

numbers[2] = 99; // changes 3rd element (30 → 99)

29
New cards

Why is an array like a constant pointer?

  • An array name acts like a pointer to its first element.

  • But unlike a pointer variable, the array name cannot be changed (you can’t do x = new int[10];).

  • That’s why we say an array is like a constant pointer.

30
New cards

What would be the output of this multi dimensional array?

int matrix[2][3] = {

{1, 2, 3},

{4, 5, 6}

};

  • You need to fix all dimensions at compile-time.

  • You can initialize using nested lists (list of lists).

  • Elements are stored in row-major order.

  • Look at photo for break down.

<ul><li><p>You need to fix <strong>all dimensions at compile-time</strong>.</p></li><li><p>You can <strong>initialize using nested lists</strong> (list of lists).</p></li><li><p>Elements are stored in <strong>row-major order</strong>.</p></li><li><p>Look at photo for break down.</p></li></ul><p></p>
31
New cards

What are character arrays?

Character arrays are just arrays of type char. They are commonly used to store C-style strings.

32
New cards

In c++ what does a null character do?

  • In C++, strings end with a null character: '\0'

  • It marks the end of the string.

  • It’s automatically added when you initialize a string with double quotes ("...").

Example: Look at photo

<ul><li><p>In C++, <strong>strings end with a null character</strong>: <code>'\0'</code></p></li><li><p>It marks the <strong>end of the string</strong>.</p></li><li><p>It’s automatically added when you initialize a string with double quotes (<code>"..."</code>).</p></li></ul><p>Example: Look at photo</p>
33
New cards

What happens when you initialize to c-string using double quotes? 

  • Allocates space for all the characters plus one extra for the null character (\0)

  • char carray[] = "hi there";

  • So "hi there" has 8 characters, plus 1 null, totaling:

Size of carray = 9

34
New cards

What does the overloaded << operator do?

  • The << operator is overloaded in C++ to print strings

  • It will print characters until it hits the null terminator \0

35
New cards

What are array function parameters? (Empty for first dimension)

1. Empty [] for the First Dimension

When defining a function that takes an array, you can leave the first dimension empty, because it's just a pointer:

void printArray(int arr[], int size);    // Same as: void printArray(int* arr, int size);

The compiler only needs the

second argument (size) to know how many elements to access.

36
New cards

What are array functions?

2. [constant] for Additional Dimensions

For multidimensional arrays, all dimensions except the first must be specified (i.e., not left empty), because the compiler needs this info to calculate element positions in memory.

void printMatrix(int matrix[][3], int rows);
 

Here:

  • The first dimension (rows) can be empty.

  • The second (columns) must be fixed, so the compiler knows the width of each row in memory.

37
New cards

Whats passed in array arguments?

knowt flashcard image

Explore top flashcards

respiratory system
Updated 695d ago
flashcards Flashcards (22)
Exam 2 For Dorth
Updated 229d ago
flashcards Flashcards (110)
The Immune System
Updated 324d ago
flashcards Flashcards (35)
Biology Unit 7
Updated 908d ago
flashcards Flashcards (210)
religion final
Updated 887d ago
flashcards Flashcards (29)
respiratory system
Updated 695d ago
flashcards Flashcards (22)
Exam 2 For Dorth
Updated 229d ago
flashcards Flashcards (110)
The Immune System
Updated 324d ago
flashcards Flashcards (35)
Biology Unit 7
Updated 908d ago
flashcards Flashcards (210)
religion final
Updated 887d ago
flashcards Flashcards (29)