Check box state based on bolded/non bolded selection

Determine the state of a check box based on bolded/non bolded selection from a given matrix. If only bolded elements are selected, then the state should be “B”, if only non bolded elements are selected, then the state should be “NB” and if hybrid then the state should be “G” (grayed out). The function should be optimized for both time and space.

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            string[,] testMatrix = {

                                    {"B", "NB", "NB", "B"},

                                    {"B", "NB", "NB", "B"},

                                    {"B", "NB", "B", "B"},

                                    {"B", "NB", "B", "B"}

                                   };

            //dimension of the selection matrix

            string[,] selectionMatrix = new string[2, 2];

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

            {

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

                {

                    //coordinates of the first element of selection matrix in test matrix

                    selectionMatrix[i, j] = testMatrix[i + 2, j + 2];

                }

            }

            Console.WriteLine(CheckSelection(selectionMatrix));

        }

        public static bool IsBold(string[,] testMatrix, int i, int j)

        {

            if (i < testMatrix.GetLength(0))

            {

                if (j < testMatrix.GetLength(1))

                {

                    if (testMatrix[i, j] == "B")

                    {

                        return true;

                    }

                    else

                    {

                        return false;

                    }

                }

            }

            return false;

        }

        public static string CheckSelection(string[,] selectionMatrix)

        {

            int boldFlag = -1;

            for (int i = 0; i < selectionMatrix.GetLength(0); i++)

            {

                for (int j = 0; j < selectionMatrix.GetLength(1); j++)

                {

                    if (IsBold(selectionMatrix, i, j))

                    {

                        if (boldFlag == -1 || boldFlag == 1)

                        {

                            boldFlag = 1;

                        }

                        else

                        {

                            return "G";

                        }

                    }

                    else

                    {

                        if (boldFlag == -1 || boldFlag == 0)

                        {

                            boldFlag = 0;

                        }

                        else

                        {

                            return "G";

                        }

                    }

                }

            }

            if (boldFlag == 1)

            {

                return "B";

            }

            else

            {

                return "NB";

            }

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s