Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Traversing 2D array, Increment array elements by 1, Matrix addition

Notes:

I. Traversing 2D array: [Starts at: 00min:00sec]
II. Incrementing elements in an array by one: [Starts at: 06min:15sec]
III. Matrix Addition: [Starts at: 11min:38sec]

I. Traversing 2D array: [Starts at: 00min:00sec]

Example Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// Creating and intializing 2d array
int[,] numbers = new int[2, 2]{
{1,2},
{3,4},
};

// traversing 2D array
for (int row = 0; row < numbers.GetLength(0); row++) {
for (int col = 0; col < numbers.GetLength(1); col++) {
Debug.Log (numbers [row, col]);
}
}
}
}

II. Incrementing elements in an array by one: [Starts at: 06min:15sec]

Example Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// Creating and intializing 2d array
int[,] numbers = new int[2, 2]{
{1,2},
{3,4},
};

// Incrementing elements in an array by one
for (int row = 0; row < numbers.GetLength(0); row++) {
for (int col = 0; col < numbers.GetLength(1); col++) {
numbers [row, col] += 1; // numbers [row, col] = numbers [row, col] +1
Debug.Log (numbers [row, col]);
}
}
}
}

III. Matrix Addition: [Starts at: 11min:38sec]

Example Code:

using UnityEngine;
public class ArraysDemo : MonoBehaviour {
void Start () {

// Matrix addition using 2d arrays
int[,] matrix1 = new int[2, 2]{
{1,2},
{3,4},
};

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

int[,] matrix3 = new int[2, 2];

// Incrementing values in an array by one
for (int row = 0; row < matrix3.GetLength(0); row++) {
for (int col = 0; col < matrix3.GetLength(1); col++) {
matrix3 [row, col] = matrix1 [row, col] + matrix2 [row, col];
Debug.Log (matrix3[row, col]);
}
}
}
}