# 5 Coding Puzzles – how to use arrays

This blog continues our series of C# coding brainteasers, which teach you how to read code without compiling it. This blog will provide 5 puzzles on Arrays. The puzzles will help you to learn practical programming skills.

An array is a container object of values of a single type. The length of array is established when array is created and it’s length is fixed. In order to know more how to use arrays please go to C# tutorial.

Try to solve these 5 arrays with your C# or other coding knowledge. Predict the output without executing the code. What language feature or standard algorithm do they illustrate? Is there more effective way that produces the same output? If you need some help, check our C# course.  Write your answer in the comments below. Or add your own puzzle.

1).

int[] a = { 5, 3, 2 };

int m = a;

if (m > a)

{

m = a;

}

if (m > a)

{

m = a;

}

Console.WriteLine(m);

2).

int[] b = {  9, 8, 7,  10, 4, 6, 5,  1, 3, 2 };

int temp;

for (int i = 0; i < 9; i++)

{

for (int j = 0; j < 9-i; j++)

{

if (b[j] < b[j + 1])

{

temp = b[j];

b[j] = b[j + 1];

b[j + 1] = temp;

}

}

}

for (int i = 0; i < 10; i++)

{

Console.Write(b[i]);

}

3).

const int r = 4;

const int c = 3;

int[,] t =

{

{20, 21, 22}, {30, 31, 32}, {40, 41, 42}, {50, 51, 52}

};

for (int i = 0; i < r; i++)

{

for (int j = 0; j < c; j++)

{

Console.WriteLine(“t[{0},{1}] = {2}”,

i, j, t[i, j]);

}

}

4).

String[] s =  {“Learn”, “arrays”, “with”, “TalkIT.”};

Array.Sort(s);

foreach (object obj in s)

{

Console.WriteLine(“Value: {0}”, obj.ToString());

}

Array.Reverse(s);

foreach (object obj in s)

{

Console.WriteLine(“Value: {0}”, obj.ToString());

}

5).

const int rows = 2;

const int rowZero = 5;

const int rowOne = 6;

int[][] ja = new int[rows][];

ja = new int[rowZero];

ja = new int[rowOne];

ja = 15;

ja = 12;

ja = 9;

for (int i = 0; i < rowZero; i++)

{

Console.WriteLine(ja[i]);

}

for (int i = 0; i < rowOne; i++)

{

Console.WriteLine(ja[i]);

}

}

}