Chapter 2: 1D Arrays

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

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:07 PM on 3/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

50 Terms

1
New cards

Declare an array called candy with 365 pieces.

float candy[365];
// type, name, size

2
New cards

Initialize some of the values in candy using a comma separated list/initializer list

float candy[365] = {1,3,5,6,8,20};

3
New cards

What happens when you declare an array as const?

Const arrays are meant to be read-only. This means they must be initialized and declared at the same time, and after initialization, they can no longer be assigned to.

4
New cards

Declare a const array called days. It should have a size of MONTHS.

const int days[MONTHS];

5
New cards

What happens when you only partially initialize an array?

int some_data[4] = {1,2,3,4};

It will automatically fill the rest with 0.

6
New cards

What happens if you have too many initializers?

int a[3] = {13,17,100,95,63};

C does not allow more initializers than the array size. Doing this will produce a compiler error.

7
New cards

What happens if you omit the size of the array?

int a[] = {3,5,8,9};

The compiler will automatically set the array size for you based on the number of elements.

8
New cards

Write a declaration for an array of 5 integers initialized to 10,20,30,40,50.

int candy[5] = {10,20,30,40,50};

9
New cards

What can you say about the values in the array: int x[3]

They contain garbage

10
New cards

Which of these is valid

Answer: const int a[3] = {1,2,3};

This is because const arrays must always be initialized at the time of declaration.

11
New cards

What does sizeof do?

sizeof returns the amount of storage in bytes.

12
New cards

What would this give us, assuming that days is an entire array of 1ints:

sizeof days / sizeof days[0]

sizeof days would return the total size of the array in bytes

sizeof days[0] would return the size of the very first element in bytes

Each int in the array is 4 bytes. This means sizeof days = 4 × 10 = 40 bytes. Therefore sizeof days[0] = 4 bytes. So the expression becomes: 40/4 = 10.

10 represents the total number of elements in the array.

13
New cards

Predict the output of the following program:

#include <stdio.h>
int main(void) {
const int days[] = {31,28,22};
int index;

for(index = 0; index < sizeof days / sizeof days[0]; index++) {

printf("Month: %2d has %d days.\n", index+1, days[index]);

}

return 0;
}

The for loop currently looks like this: index = 0, index < 3, index++

so the output will be:

Month: 1 has 31 days.

Month: 2 has 28 days.

Month: 3 has 22 days.

14
New cards

What does a designated initializer look like? How is it different from a traditional initializer?

A designated initializer looks like this: int arr[6] = { [5] = 212 };

A traditional initializer looks like this: int arr[6] = {0,0,0,0,12};

The difference is that designated initializers only set particular elements, not all of them. Any elements not assigned to are left as 0.

15
New cards

What are the values in the following array:

int days[MONTHS] = {31,28,[4]=30,28,18,[1]=29};

days[0] = 31

days[1] = 28

days[4] = 30

days[5] = 28

days[6] = 18

days[1] = 29 // we overwrote the previous value

16
New cards

What ends up being the size of this array?

int staff[] = {1, [6]=4, 9, 10};

The size of the array ends up being 9, because there are 8 indices specified in the actual array.

17
New cards

Can you assign one array to another in C?

No, C does not support array to array assignment.

18
New cards

Are you allowed to assign values out of the bounds of an array?

Yes you are allowed. This is because C does not check the bounds of arrays. Even if you assign something out of bounds, C will not warn you about it.

19
New cards

What is the size of this array?

int nums[] = {4, 8, 15, 16, 23, 42};

Size: 6

20
New cards

What does this expression evaluate to?

double data[] = {1.1,2.2,3.3};

sizeof data / sizeof data[0];

The expression evaluates to: 24 / 8 = 3

Note: Each double is 8 bytes in size

21
New cards

What are the values in the following array?

int a[6] = {[3]=10,20};

All values in the array are 0 except for: a[3] = 10, and a[4] = 20

22
New cards

What will happen in the following assignment:

int x[3] = {1,2,3};

x[3] = 10;

This will result in undefined behavior, but C will not warn you about it, and will let you do it anyway.

23
New cards

Is this allowed?

float a4[-3];

No

24
New cards

Is this allowed?

float a2[5*2+1]

Yes

25
New cards

Is this allowed?

float a3[sizeof (int) + 1];

Yes

26
New cards

Is this allowed?

float a7[(int) 2.5];

Yes

27
New cards

Is this allowed?

float a5[0];

No

28
New cards

Is this allowed?

float a7[2.5];

No

29
New cards

What are variable-length arrays?

Arrays that use variables as their size.

30
New cards

What is a unique feature about variable-length arrays?

They cannot be static.

31
New cards

How many elements does this array contain?

float m[3][4]

12 elements

32
New cards

Which element comes immediately after rain[1][11] in memory?

Note that rain is a 5 by 12 array: rain[12][5]

rain[2][0]

33
New cards

Write the loop structure needed to sum up all elements of this 2D Array:

a[R][C]

int sum = 0;
for(int i=0; i < R; i++) {
for(int k=0; k < C; k++) {
sum += a[i][k];
}
}

34
New cards

Describe what this 3D array declaration actually looks like:

int box[2][3][4]

box is an array of 2 elements,.

each element is an array of 3 elements.

each of those 3 elements is an array of 4 ints.

Therefore, we have:

{element 1, element 2}

Then:

{ {x,x,x}, {x,x,x} }

Then:

{ {{y, y, y, y}, {y,y,y,y}, {y,y,y,y}} }, {{{y, y, y, y}, {y,y,y,y}, {y,y,y,y}} } }

35
New cards

What happens when you assign a pointer to an array directly?

int *p = arr;

The pointer decays to a pointer to the very first element in the array: arr[0]

36
New cards

If pt1 is a pointer to an array of short, what does this do?

printf("%d\n", pt1+1);

pt1 increases by 2 bytes, because 2 bytes is the size of a short. It essentially moves in the size of the pointed to type, effectively moving it one element over.

37
New cards

If ptf is a pointer to an array of doubles, what does this do?

printf("%d\n", ptf+1);

It moves the pointer over by 8 bytes (the size of a double), moving it to the next element.

38
New cards

Are these two equivalent statements?

*(dates+2) == &dates[2]

No, the first is referring to the value at a certain pointer position, while the other is referring to an address.

39
New cards

Are these two equivalent statements?

dates+2 == &dates[2]

Yes.

This is because dates is originally a pointer to dates[0]. So adding 2 to it moves it over to dates[2].

&dates[2] has the exact same meaning as the pointer. It means the address at dates[2].

40
New cards

Are these two statements equivalent?

*(dates+2) == dates[2];

Yes, we are getting the value at dates[2] and comparing it to the value at dates[2]. These are exactly the same.

41
New cards

What is different between the two of these:

*dates+2 and *(dates+2)

*dates+2 means dereference (or get the value at) dates[0], and add 2 to that value.

*(dates+2) means dereference (or get the value at) dates[2]

42
New cards

What format specifier is used for pointers?

%p is used for pointers

43
New cards

What happens when you use arrays inside of function calls?

Example:

total = sum(marbles);

In most expressions, including function calls, the array name decays to a pointer to the very first element. So now marbles is of type int *.

44
New cards

If ar is a pointer known as: int *ar

Is it okay for us to use ar[i] to refer to it in expressions?

Yes. Although ar is a pointer, ar[i] is defined as *(ar+i)

45
New cards

Are these expressions interchangeable?

int sum(int ar[], int n)

int sum(int *ar, int n)

They are only interchangeable only within function parameter lists, nowhere else.

46
New cards

If int a[5] and int *p = a, what is the relationship between a+2 and &a[2]

These are equivalent.

a+2 means &a[0]+2 = &a[2]

this is the same as &a[2]

47
New cards

Write a complete C program that declares an array of 5 shorts:
short nums[5] = {10,20,30,40,50};

Declare a pointer p to short, assign the array to the pointer, use pointer arithmetic to print each element.

#include <stdio.h>

int main(void) {
short nums[5] = {10,20,30,40,50};
short *p = nums;
for(int i=0; i < sizeof nums / size of nums[0]; i++) {
printf("%d", *(p+i));
}

return 0;
}

48
New cards

Write a complete program that declares an array of 6 integers, fills the array of values using array indexing: arr[i]…. Prints the values using pointer notation, then prints the same values again using array notation: arr[i].

#include <stdio.h>

int main(void) {
int arr[6] = {1,2,3,4,5,6};
int *ptr = arr;

for(int i=0; i < sizeof arr/sizeof arr[0]; i++) {
arr[i] = i*2;
printf("%d ", *(ptr+i));
printf("%d ", arr[i]);
}

return 0;
}

49
New cards

Write a complete program that declares an array of 8 integers, writes a function int sum(int *ar, int n). Inside the function, use pointer arithmetic to compute the sum. Call the function in main and print the result.

#include <stdio.h>

int sum(int *ar, int n);

int main(void) {

int arr[8] = {1,2,3,4,5,6,7,8};
printf("The sum is: %d", sum(arr, 8));
return 0;

}



int sum(int *ar, int n) {

int sum = 0;

for(int i=0; i < n; i++) {
sum += *(ar+i);
}

return sum;
}

50
New cards

Write the same program from the previous question, but change the function header to int sum(int ar[], int n). Use pointer arithmetic inside the function.

News flash: Nothing actually changes about the previous function or code. It stays exactly the same. Remember: int sum(int ar[], int n) is equivalent to int sum(int *ar, int n), but ONLY within a function prototype.

Explore top notes

note
Chapter 9: Chemical Equilibrium
Updated 1095d ago
0.0(0)
note
Wedding Wind
Updated 1257d ago
0.0(0)
note
College Prep Chemistry, Elements
Updated 1281d ago
0.0(0)
note
Grade 10 Biology: Lesson 9
Updated 1181d ago
0.0(0)
note
Chapter 9: Chemical Equilibrium
Updated 1095d ago
0.0(0)
note
Wedding Wind
Updated 1257d ago
0.0(0)
note
College Prep Chemistry, Elements
Updated 1281d ago
0.0(0)
note
Grade 10 Biology: Lesson 9
Updated 1181d ago
0.0(0)

Explore top flashcards

flashcards
Odyssey Test review
85
Updated 535d ago
0.0(0)
flashcards
duits kapitel 2 woordenschat
101
Updated 878d ago
0.0(0)
flashcards
US State Capitals
50
Updated 937d ago
0.0(0)
flashcards
APUSH 23-25 Simple IDs
90
Updated 60d ago
0.0(0)
flashcards
French Indefinite Articles
34
Updated 705d ago
0.0(0)
flashcards
AP Psych Unit 3
79
Updated 861d ago
0.0(0)
flashcards
Waves key words and definitions
21
Updated 476d ago
0.0(0)
flashcards
Map of East Asia- Physical map
34
Updated 428d ago
0.0(0)
flashcards
Odyssey Test review
85
Updated 535d ago
0.0(0)
flashcards
duits kapitel 2 woordenschat
101
Updated 878d ago
0.0(0)
flashcards
US State Capitals
50
Updated 937d ago
0.0(0)
flashcards
APUSH 23-25 Simple IDs
90
Updated 60d ago
0.0(0)
flashcards
French Indefinite Articles
34
Updated 705d ago
0.0(0)
flashcards
AP Psych Unit 3
79
Updated 861d ago
0.0(0)
flashcards
Waves key words and definitions
21
Updated 476d ago
0.0(0)
flashcards
Map of East Asia- Physical map
34
Updated 428d ago
0.0(0)