FAQ: Arrays: Lesson - Element Access in Multidimensional Arrays

This community-built FAQ covers the “Element Access in Multidimensional Arrays” exercise from the lesson “Arrays: Lesson”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C

FAQs on the exercise Element Access in Multidimensional Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why does the lesson require using sizeof(double) when the matrix is made of ints? And does sizeof(matrix[0][0]) work as well?

1 Like

Same question here. Could it be a mistake?
The array matrix was initialized as an int array.
When using sizeof(double) in the nested for loop to determine the length of the columns, not all numbers get printed out.
Here is my solution (I added print statements to check if my loops iterate through all numbers in the array and they do.):

#include<stdio.h>

int main() {
  int matrix[][4] = {{14, 10, 6, 4}, {3, 7, 18, 11}, {13, 9, 5, 17}, {19, 12, 2, 1}}; 
  int sum = 0;

  // Checkpoint 1 code goes here.
  printf("%i\n", matrix[3][1]);
  // Checkpoint 2 code goes here.
  for (int i = 0; i < sizeof(matrix)/sizeof(matrix[0]); i++) {
    for (int j = 0; j < sizeof(matrix[0])/sizeof(int); j++) {
      printf("%i\t", matrix[i][j]);
      sum += matrix[i][j];
    }
  }
  printf("%i\n", sum);
}
1 Like

Hey @toshymoshy , thank you for u explanation, I wish to have another one…

can u explain why in the inner loop u mention sizeof(int) ? r u intentionally using the size of the datatype ?

EDIT: after reading this everything fell into place: How do I determine the size of my array in C? - Stack Overflow

the middle row in the snippet:

  for (int i = 0; i < sizeof(matrix)/sizeof(matrix[0]); i++) {
    for (int j = 0; j < sizeof(matrix[0])/sizeof(int); j++) {  // <<< here
      printf("%i\t", matrix[i][j]);

Thanks in advance :slight_smile:

1 Like

I appreciate this explanation, what I would like to understand though is how the sum+= function knows what numbers to add? In better words, how does it get the current value of the index and how does it know to add the next index value to the previous?

How can I make it so that ONLY the total is printed? When I wrote this code below, I get the total which is 151, but the terminal also shows the previous results that were added before getting to 151. I included the output as well so you can understand what I mean

int main() {
int matrix[4] = {{14, 10, 6, 4}, {3, 7, 18, 11}, {13, 9, 5, 17}, {19, 12, 2, 1}};
int sum =0;
int rowDimension = sizeof(matrix) / sizeof(matrix[0]);
int columnDimension = sizeof(matrix[0]) / sizeof(int);

for (int i = 0 ; i < rowDimension ; i++) {
for (int j = 0 ; j < columnDimension ; j ++) {
sum+= matrix[i][j];
printf(“%d\n”, sum);

}

}
}

Output:
14
24
30
34
37
44
62
73
86
95
100
117
136
148
150
151

If all you wish to report is the final sum, move that line down to below the next closing brace.

If

int len = sizeof(arr)/sizeof(int);

Gives us the lenght of an array, and sizeof(arr) is the size of the array in bytes and sizeof(int) gives us the size of the data type of the array,

int rowDimension = sizeof(mat)/sizeof(mat[0]);

What does sizeof(matrix) and sizeof(matrix[0]) give us

Suppose you have a one-dimensional array of integers,

int arr[] = {4, 5, 6, 88, 1, 3, 66};

int length = sizeof(arr) / sizeof(int);
printf("%i\n", length);

// Output: 7

sizeof(arr) gives how much bytes of memory have been allocated to the arr array.
Since arr is an array of integers, so sizeof(int) will tell us how much memory is taken by one integer.
Dividing the total bytes allocated to array BY the bytes taken by a single integer tells us how many integers are present in the arr array.

Suppose you have a two-dimensional array of integers e.g.

int matrix[][5] = {{2, 4, 6, 7, 1}, {3, 5, 34, 3, 1}, {1, 0, 3, 77, 93}};
    
int rowDimension = sizeof(matrix) / sizeof(matrix[0]);
int columnDimension = sizeof(matrix[0]) / sizeof(int);

printf("%i\n", rowDimension); 
// 3
printf("%i\n", columnDimension);
// 5

sizeof(matrix) gives how much bytes of memory have been allocated to the matrix array.

Each element of matrix is an array, so sizeof(matrix[0]) gives us the bytes of memory allocated to the first array i.e. the bytes of memory allocated to {2, 4, 6, 7, 1}

sizeof(matrix) / sizeof(matrix[0]) tells us how many sub-arrays are present in the matrix array.
Since all the sub-arrays have the same number of elements/integers, so the bytes allocated to the array {2, 4, 6, 7, 1} will be the same as the bytes allocated to {3, 5, 34, 3, 1}, and the same bytes as allocated to {1, 0, 3, 77, 93}

sizeof(matrix[0]) / sizeof(int) calculates how many numbers are present in each sub-array.