C# Tutorial for Beginner : Chapter 10 Array 2-Dimension

Introduction

An array monitors various snippets of data in single line order, a one-dimensional record list. Notwithstanding, the information related with specific frameworks (an advanced picture, a tabletop game, and so forth.) lives in two measurements. To picture this information, we require a multi-dimensional information structure, that is, a multi-dimensional array. A two-dimensional array is extremely simply an array of arrays (a three-dimensional array is an array of arrays of arrays). The accompanying declaration makes a two-dimensional array of four lines and two segments.

float[,] numbers = new float[3, 20];

Example Explanation 

To make you more understand to this 2-dimension array, follow this example. Given a 2 dimensional array with size 6x6 as shown below. This array is illustrated with rows and columns. The first dimension is like a line and a second dimension is like a column

Array 6 x 6
We focus on the position of the blue array

Focusing Array Position
Note that the array in the red box. We just take the existing value in the blue colored array position with the center position on the red circle.

Getting other specific position from core position
The value of the blue array position is summed. Run this process on all array positions with a red circle as shown.

How Far you should run your sum process
So in coding we only run if the position in the red circle only, the rest just skipped it. After getting the sum of the 16 processes, take the biggest value.

Implementation


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    //get max value from array
        static int getMax(int[]arr)
        {
            int max = arr[0];
            for (int i=0;i<arr.Length;i++)
            {
                if (arr[i]>max)
                {
                    max = arr[i];
                }
            }
            return max;
        }

        static void Main(string[] args)
        {
            int[,] array = new int[6,6];
            int[] arraySumSelection = new int[16];
            int countForSum = 0;

            //input array value
            for (int x=0;x<6;x++)
            {
                string[] arr_temp = Console.ReadLine().Split(' ');
                int []arr_temp_int=Array.ConvertAll(arr_temp, Int32.Parse);
                for (int y =0; y<6;y++)
                {
                    array[x, y] = arr_temp_int[y];
                }
                
            }


            //Selection 
            for (int x= 0; x < 6; x++)
            {
                for (int y = 0; y < 6; y++)
                {
                    Boolean check = x == 0 || y == 0 || x == array.GetLength(0) - 1 || y == array.GetLength(1) - 1;
                    if (!check)
                    {
                        arraySumSelection[countForSum] = array[x - 1, y - 1] + array[x - 1, y]+array[x - 1, y + 1]
                                                                            + array[x,y]
                                                         +array[x + 1, y - 1]+array[x + 1, y]+array[x + 1, y + 1];
                        countForSum++;
                    }
                }
            }//END OF Selection

            //print the result
            Console.WriteLine(getMax(arraySumSelection));
        }//END MAIN
}

Comments

Popular posts from this blog

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Quantization of Image Data Implementation in C#

Computer Graphics Project "MINION" Source Code with Glut OpenGL