Skip to content

Commit e15a520

Browse files
authored
added 2d array article (#236)
* added 2d array article * fix links to sols
1 parent 313889e commit e15a520

18 files changed

+415
-38
lines changed

data/articles/c-2d-arrays-1.mdx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: 2D Arrays in C Tutorial 1 - Creating a 2D Array
3+
date: 2023-04-14
4+
desc: Starting the guide on 2D Arrays in C!
5+
tags:
6+
- 'COMP1511'
7+
- 'Exam Revision'
8+
- '2D Arrays in C'
9+
author: William Huynh
10+
coverPhoto: '/images/generic/markus-spiske-iar-afB0QQw-unsplash.jpg'
11+
---
12+
13+
# 2D Arrays in C 🥳
14+
15+
Welcome to our guide on 2D Arrays in C!
16+
17+
Here you will learn how to use and understand seamlessly the very scary topic that is
18+
2D arrays!
19+
20+
To break it down nice and easy, we will be covering 6 operations:
21+
22+
- **Creating** a 2D Array 🔧
23+
- **Initialising** a 2D Array 📚
24+
- **Accessing** elements in a 2D Array 🔍
25+
- **Editing** elements in a 2D Array ✏️
26+
- **Iterating** through a 2D Array 🔁
27+
28+
# Creating a 2D Array 🔧
29+
30+
## Preface 🐶
31+
32+
A 2D array is often referred to as an array of arrays, or a matrix! This is because it consist of rows and columns, and thus takes the shape of a matrix!
33+
34+
![Array Example](/assets/1.png 'img2')
35+
36+
## Getting Started 🎉
37+
38+
To create a 2D array in C, you will need to declare it using the following syntax:
39+
40+
```
41+
<datatype> <arrayname> [row_size][col_size];
42+
```
43+
44+
Where:
45+
46+
- `<datatype>` is the datatype of the array
47+
_(i.e. `int`, `char`, `bool`, etc...)_
48+
- `<arrayname>` is the name of the array _(i.e. `my_array`)_
49+
- `row_size` is the number of rows in the array
50+
- `col_size` is the number of columns in the array
51+
52+
For example, to create a 2D array of integers with 2 rows and 4 columns, you would use the following declaration:
53+
54+
```
55+
int my_array[2][4];
56+
```
57+
58+
Pretty easy right?
59+
60+
### ✅ Now lets get started on **Initialising** an array 😎
61+
62+
<ArticleButtonContainer
63+
64+
next="/articles/c-2d-arrays-2"
65+
66+
nextName="2D Arrays in C Tutorial 2 - Initialising a 2D Array"
67+
68+
>
69+
70+
</ArticleButtonContainer>

data/articles/c-2d-arrays-2.mdx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: 2D Arrays in C Tutorial 2
3+
date: 2023-04-14
4+
desc: Initialising a 2D Array
5+
tags:
6+
- 'COMP1511'
7+
- 'Exam Revision'
8+
- '2D Arrays in C'
9+
author: William Huynh
10+
coverPhoto: '/images/generic/markus-spiske-iar-afB0QQw-unsplash.jpg'
11+
---
12+
13+
# Initialising a 2D Array 📚
14+
15+
## Preface 🐶
16+
17+
Whats the fun in creating a 2D array if we can't store stuff in it? 😛
18+
Here we will learn how to create a 2D array with values already present in it. This operation is called **initialisation**.
19+
20+
## Getting Started 🎉
21+
22+
So before we learnt we could create an array with **2 rows** and **4 columns** by the following:
23+
24+
```
25+
int my_array[2][4];
26+
```
27+
28+
But lets say that we want to initialise some values to it, specifically:
29+
30+
- The integers `1,2,3,4`, in the **first row**
31+
- The integers `5,6,7,8`, in the **second row**
32+
33+
To do that we would use the following initialisation:
34+
35+
```
36+
int my_array[2][4] = {
37+
{1, 2, 3, 4}, // First row
38+
{5, 6, 7, 8} // Second row
39+
};
40+
```
41+
42+
If we were to add **another row**, it could look something like this!
43+
44+
```
45+
int my_array[3][4] = {
46+
{1, 2, 3, 4}, // First row
47+
{5, 6, 7, 8}, // Second row
48+
{9, 10, 11, 12} // Third row
49+
};
50+
```
51+
52+
If we were to add **another column**, it could look something like this!
53+
54+
```
55+
int my_array[2][5] = {
56+
{1, 2, 3, 4, 5}, // First row
57+
{6, 7, 8, 9, 10}, // Second row
58+
};
59+
```
60+
61+
Quite nice right?
62+
63+
### ✅ Now lets get started on **Accessing** elements in an array 😎
64+
65+
<ArticleButtonContainer
66+
67+
next="/articles/c-2d-arrays-3"
68+
nextName="2D Arrays in C Tutorial 3 - Accessing elements"
69+
70+
prev="/articles/c-2d-arrays1"
71+
prevName="2D Arrays in C Tutorial 1 - Creating a 2D Array"
72+
73+
>
74+
75+
</ArticleButtonContainer>

data/articles/c-2d-arrays-3.mdx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: 2D Arrays in C Tutorial 3
3+
date: 2023-04-14
4+
desc: Accessing elements in a 2D Arrays
5+
tags:
6+
- 'COMP1511'
7+
- 'Exam Revision'
8+
- '2D Arrays in C'
9+
author: William Huynh
10+
coverPhoto: '/images/generic/nasa-Q1p7bh3SHj8-unsplash.jpg'
11+
---
12+
13+
# Accessing elements in a 2D Array 📚
14+
15+
## Preface 🐶
16+
17+
Whats the fun in storing elements in a 2D array if we can't access the elements? 😛
18+
Here we will learn how to access the elements in a 2D array.
19+
20+
## Getting Started 🎉
21+
22+
To access an element in a 2D array you need **two** things!
23+
24+
- The **row** index
25+
- The **column** index
26+
27+
With those two things we can access any element!
28+
29+
> Remember that indexes start at `0`, so the **first row** and **first column** will have indexes of `0`.
30+
31+
## Some Practice 🎯
32+
33+
Say we have the following array:
34+
35+
```
36+
int my_array[2][4] = {
37+
{1, 2, 3, 4}, // First row
38+
{5, 6, 7, 8} // Second row
39+
};
40+
```
41+
42+
Let's see if we can identify the row and column indexes of each element!
43+
44+
Try work it out yourself first, before clicking the dropdown answer :P
45+
46+
<details>
47+
<summary>Element 1</summary>> Element 1 is at [0, 0] !
48+
</details>
49+
50+
<details>
51+
<summary>Element 2</summary>> Element 2 is at [0, 1] !
52+
</details>
53+
54+
<details>
55+
<summary>Element 4</summary>> Element 4 is at [0, 3] !
56+
</details>
57+
58+
<details>
59+
<summary>Element 5</summary>> Element 5 is at [1, 0] !
60+
</details>
61+
62+
<details>
63+
<summary>Element 6</summary>> Element 6 is at [1, 1] !
64+
</details>
65+
66+
<details>
67+
<summary>Element 8</summary>> Element 8 is at [1, 3] !
68+
</details>
69+
70+
Well done!
71+
72+
## Writing the code ✍️
73+
74+
So luckily writing the code to access an element is very similar to our thought process above - we need the **row and column indexes** of the element we want to access!
75+
76+
For example, if I wanted to access the element in the **first row** and **second column**, I would do the following:
77+
78+
```
79+
int my_num = my_array[0][1];
80+
```
81+
82+
This creates an integer variable called `my_num`, and assigns it the value of whatever element is stored at `my_array[0][1]`.
83+
84+
Pretty easy right?
85+
86+
### ✅ Now lets get started on **Editing** an element 😎
87+
88+
<ArticleButtonContainer
89+
90+
next="/articles/c-2d-arrays-4"
91+
nextName="2D Arrays in C Tutorial 4 - Editing an element"
92+
93+
prev="/articles/c-2d-arrays-2"
94+
prevName="2D Arrays in C Tutorial 2 - Initialising a 2D Array"
95+
96+
>
97+
98+
</ArticleButtonContainer>

data/articles/c-2d-arrays-4.mdx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: 2D Arrays in C Tutorial 4
3+
date: 2023-04-14
4+
desc: Editing elements in a 2D Arrays
5+
tags:
6+
- 'COMP1511'
7+
- 'Exam Revision'
8+
- '2D Arrays in C'
9+
author: William Huynh
10+
coverPhoto: '/images/generic/simon-abrams-k_T9Zj3SE8k-unsplash.jpg'
11+
---
12+
13+
# Editing elements in a 2D Array ✏️
14+
15+
## Preface 🐶
16+
17+
So we've learnt how to access elements in an array, but whats also cool is that we can change those elements too!
18+
19+
## Getting Started 🎉
20+
21+
To edit an element in a 2D array you need **two** things!
22+
23+
- The **row** index
24+
- The **column** index
25+
26+
With those two things we can access and thus edit any element!
27+
28+
> Remember that indexes start at `0`, so the **first row** and **first column** will have indexes of `0`.
29+
30+
Say we have the following array:
31+
32+
```
33+
int my_array[2][4] = {
34+
{1, 2, 3, 4}, // First row
35+
{5, 6, 7, 8} // Second row
36+
};
37+
```
38+
39+
We know that to access element `2`, we can do the following:
40+
41+
```
42+
int my_num = my_array[0][1];
43+
```
44+
45+
Luckily, editing it is very similar!
46+
47+
Let say we want to change the element at `[0][1]` to be 42, we can do the following:
48+
49+
```
50+
my_array[0][1] = 42;
51+
```
52+
53+
Pretty cool right?
54+
Onto the final topic we go!
55+
56+
### ✅ Now lets get started on **Iterating** through an array 😎 Continue [here](iterating.md)!
57+
58+
<ArticleButtonContainer
59+
60+
next="/articles/c-2d-arrays-5"
61+
nextName="2D Arrays in C Tutorial 5 - Iterating through a 2D Array"
62+
63+
prev="/articles/c-2d-arrays-3"
64+
prevName="2D Arrays in C Tutorial 3 - Accessing a 2D Array"
65+
66+
>
67+
68+
</ArticleButtonContainer>

data/articles/c-2d-arrays-5.mdx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: 2D Arrays in C Tutorial 5
3+
date: 2023-04-14
4+
desc: Iterating through a 2D Array
5+
tags:
6+
- 'COMP1511'
7+
- 'Exam Revision'
8+
- '2D Arrays in C'
9+
author: William Huynh
10+
coverPhoto: '/images/lofi.jpg'
11+
---
12+
13+
# Iterating through a 2D Array 🔁
14+
15+
## Preface 🐶
16+
17+
Whats the point of storing stuff in an array if we can access them all?
18+
Here we will learn how to iterate through a 2D array.
19+
20+
## Getting Started 🎉
21+
22+
To iterate through a 2D array you need:
23+
24+
- A variable representing the **row** index
25+
- A variable representing the **column** index
26+
- A **nested for-loop**!
27+
28+
> Remember that a nested for loop is a loop within a loop!
29+
30+
So again, lets say we have the following array.
31+
32+
```
33+
int my_array[2][4] = {
34+
{1, 2, 3, 4}, // First row
35+
{5, 6, 7, 8} // Second row
36+
};
37+
```
38+
39+
How would we iterate through the elements and print them out?
40+
41+
Well lets do it!
42+
43+
```
44+
int row, col;
45+
for (row = 0; row < 2; row++) {
46+
for (col = 0; col < 4; col++) {
47+
printf("%d ", myArray[row][col]);
48+
}
49+
printf("\n");
50+
}
51+
```
52+
53+
- The first loop runs through each row of the array, starting with row 0
54+
- Then for that specific row, the second for loop, runs through every column
55+
- Then for each column, we just print out the element!
56+
57+
The result will be:
58+
59+
```
60+
1, 2, 3, 4, 5, 6, 7, 8
61+
```
62+
63+
And thats all!
64+
65+
Congratulations, you are now a 2D Array King!
66+
67+
<ArticleButtonContainer
68+
69+
prev="/articles/c-2d-arrays-4"
70+
prevName="2D Arrays in C Tutorial 4 - Editing elements"
71+
72+
>
73+
74+
</ArticleButtonContainer>

0 commit comments

Comments
 (0)