Skip to content

Commit

Permalink
added 2d array article (#236)
Browse files Browse the repository at this point in the history
* added 2d array article

* fix links to sols
  • Loading branch information
Allynixtor committed Apr 14, 2023
1 parent 313889e commit e15a520
Show file tree
Hide file tree
Showing 18 changed files with 415 additions and 38 deletions.
70 changes: 70 additions & 0 deletions data/articles/c-2d-arrays-1.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: 2D Arrays in C Tutorial 1 - Creating a 2D Array
date: 2023-04-14
desc: Starting the guide on 2D Arrays in C!
tags:
- 'COMP1511'
- 'Exam Revision'
- '2D Arrays in C'
author: William Huynh
coverPhoto: '/images/generic/markus-spiske-iar-afB0QQw-unsplash.jpg'
---

# 2D Arrays in C 🥳

Welcome to our guide on 2D Arrays in C!

Here you will learn how to use and understand seamlessly the very scary topic that is
2D arrays!

To break it down nice and easy, we will be covering 6 operations:

- **Creating** a 2D Array 🔧
- **Initialising** a 2D Array 📚
- **Accessing** elements in a 2D Array 🔍
- **Editing** elements in a 2D Array ✏️
- **Iterating** through a 2D Array 🔁

# Creating a 2D Array 🔧

## Preface 🐶

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!

![Array Example](/assets/1.png 'img2')

## Getting Started 🎉

To create a 2D array in C, you will need to declare it using the following syntax:

```
<datatype> <arrayname> [row_size][col_size];
```

Where:

- `<datatype>` is the datatype of the array
_(i.e. `int`, `char`, `bool`, etc...)_
- `<arrayname>` is the name of the array _(i.e. `my_array`)_
- `row_size` is the number of rows in the array
- `col_size` is the number of columns in the array

For example, to create a 2D array of integers with 2 rows and 4 columns, you would use the following declaration:

```
int my_array[2][4];
```

Pretty easy right?

### ✅ Now lets get started on **Initialising** an array 😎

<ArticleButtonContainer

next="/articles/c-2d-arrays-2"

nextName="2D Arrays in C Tutorial 2 - Initialising a 2D Array"

>

</ArticleButtonContainer>
75 changes: 75 additions & 0 deletions data/articles/c-2d-arrays-2.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: 2D Arrays in C Tutorial 2
date: 2023-04-14
desc: Initialising a 2D Array
tags:
- 'COMP1511'
- 'Exam Revision'
- '2D Arrays in C'
author: William Huynh
coverPhoto: '/images/generic/markus-spiske-iar-afB0QQw-unsplash.jpg'
---

# Initialising a 2D Array 📚

## Preface 🐶

Whats the fun in creating a 2D array if we can't store stuff in it? 😛
Here we will learn how to create a 2D array with values already present in it. This operation is called **initialisation**.

## Getting Started 🎉

So before we learnt we could create an array with **2 rows** and **4 columns** by the following:

```
int my_array[2][4];
```

But lets say that we want to initialise some values to it, specifically:

- The integers `1,2,3,4`, in the **first row**
- The integers `5,6,7,8`, in the **second row**

To do that we would use the following initialisation:

```
int my_array[2][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8} // Second row
};
```

If we were to add **another row**, it could look something like this!

```
int my_array[3][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8}, // Second row
{9, 10, 11, 12} // Third row
};
```

If we were to add **another column**, it could look something like this!

```
int my_array[2][5] = {
{1, 2, 3, 4, 5}, // First row
{6, 7, 8, 9, 10}, // Second row
};
```

Quite nice right?

### ✅ Now lets get started on **Accessing** elements in an array 😎

<ArticleButtonContainer

next="/articles/c-2d-arrays-3"
nextName="2D Arrays in C Tutorial 3 - Accessing elements"

prev="/articles/c-2d-arrays1"
prevName="2D Arrays in C Tutorial 1 - Creating a 2D Array"

>

</ArticleButtonContainer>
98 changes: 98 additions & 0 deletions data/articles/c-2d-arrays-3.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: 2D Arrays in C Tutorial 3
date: 2023-04-14
desc: Accessing elements in a 2D Arrays
tags:
- 'COMP1511'
- 'Exam Revision'
- '2D Arrays in C'
author: William Huynh
coverPhoto: '/images/generic/nasa-Q1p7bh3SHj8-unsplash.jpg'
---

# Accessing elements in a 2D Array 📚

## Preface 🐶

Whats the fun in storing elements in a 2D array if we can't access the elements? 😛
Here we will learn how to access the elements in a 2D array.

## Getting Started 🎉

To access an element in a 2D array you need **two** things!

- The **row** index
- The **column** index

With those two things we can access any element!

> Remember that indexes start at `0`, so the **first row** and **first column** will have indexes of `0`.
## Some Practice 🎯

Say we have the following array:

```
int my_array[2][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8} // Second row
};
```

Let's see if we can identify the row and column indexes of each element!

Try work it out yourself first, before clicking the dropdown answer :P

<details>
<summary>Element 1</summary>> Element 1 is at [0, 0] !
</details>

<details>
<summary>Element 2</summary>> Element 2 is at [0, 1] !
</details>

<details>
<summary>Element 4</summary>> Element 4 is at [0, 3] !
</details>

<details>
<summary>Element 5</summary>> Element 5 is at [1, 0] !
</details>

<details>
<summary>Element 6</summary>> Element 6 is at [1, 1] !
</details>

<details>
<summary>Element 8</summary>> Element 8 is at [1, 3] !
</details>

Well done!

## Writing the code ✍️

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!

For example, if I wanted to access the element in the **first row** and **second column**, I would do the following:

```
int my_num = my_array[0][1];
```

This creates an integer variable called `my_num`, and assigns it the value of whatever element is stored at `my_array[0][1]`.

Pretty easy right?

### ✅ Now lets get started on **Editing** an element 😎

<ArticleButtonContainer

next="/articles/c-2d-arrays-4"
nextName="2D Arrays in C Tutorial 4 - Editing an element"

prev="/articles/c-2d-arrays-2"
prevName="2D Arrays in C Tutorial 2 - Initialising a 2D Array"

>

</ArticleButtonContainer>
68 changes: 68 additions & 0 deletions data/articles/c-2d-arrays-4.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: 2D Arrays in C Tutorial 4
date: 2023-04-14
desc: Editing elements in a 2D Arrays
tags:
- 'COMP1511'
- 'Exam Revision'
- '2D Arrays in C'
author: William Huynh
coverPhoto: '/images/generic/simon-abrams-k_T9Zj3SE8k-unsplash.jpg'
---

# Editing elements in a 2D Array ✏️

## Preface 🐶

So we've learnt how to access elements in an array, but whats also cool is that we can change those elements too!

## Getting Started 🎉

To edit an element in a 2D array you need **two** things!

- The **row** index
- The **column** index

With those two things we can access and thus edit any element!

> Remember that indexes start at `0`, so the **first row** and **first column** will have indexes of `0`.
Say we have the following array:

```
int my_array[2][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8} // Second row
};
```

We know that to access element `2`, we can do the following:

```
int my_num = my_array[0][1];
```

Luckily, editing it is very similar!

Let say we want to change the element at `[0][1]` to be 42, we can do the following:

```
my_array[0][1] = 42;
```

Pretty cool right?
Onto the final topic we go!

### ✅ Now lets get started on **Iterating** through an array 😎 Continue [here](iterating.md)!

<ArticleButtonContainer

next="/articles/c-2d-arrays-5"
nextName="2D Arrays in C Tutorial 5 - Iterating through a 2D Array"

prev="/articles/c-2d-arrays-3"
prevName="2D Arrays in C Tutorial 3 - Accessing a 2D Array"

>

</ArticleButtonContainer>
74 changes: 74 additions & 0 deletions data/articles/c-2d-arrays-5.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: 2D Arrays in C Tutorial 5
date: 2023-04-14
desc: Iterating through a 2D Array
tags:
- 'COMP1511'
- 'Exam Revision'
- '2D Arrays in C'
author: William Huynh
coverPhoto: '/images/lofi.jpg'
---

# Iterating through a 2D Array 🔁

## Preface 🐶

Whats the point of storing stuff in an array if we can access them all?
Here we will learn how to iterate through a 2D array.

## Getting Started 🎉

To iterate through a 2D array you need:

- A variable representing the **row** index
- A variable representing the **column** index
- A **nested for-loop**!

> Remember that a nested for loop is a loop within a loop!
So again, lets say we have the following array.

```
int my_array[2][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8} // Second row
};
```

How would we iterate through the elements and print them out?

Well lets do it!

```
int row, col;
for (row = 0; row < 2; row++) {
for (col = 0; col < 4; col++) {
printf("%d ", myArray[row][col]);
}
printf("\n");
}
```

- The first loop runs through each row of the array, starting with row 0
- Then for that specific row, the second for loop, runs through every column
- Then for each column, we just print out the element!

The result will be:

```
1, 2, 3, 4, 5, 6, 7, 8
```

And thats all!

Congratulations, you are now a 2D Array King!

<ArticleButtonContainer

prev="/articles/c-2d-arrays-4"
prevName="2D Arrays in C Tutorial 4 - Editing elements"

>

</ArticleButtonContainer>
Loading

0 comments on commit e15a520

Please sign in to comment.