Monday, November 25, 2013

Dot Net: Matrix Arrangement

Write a function to return an array where the midst position contains the smallest value followed by the next smallest value on the right of the midst position, followed by the next smallest value to the left of the midst position and the rest of numbers continue in this format.

Function signature:
public static int[ ] ArrangeElements(int[,] inputArray) 
{ 
   // write the code here
}




SOLUTION: 

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixArrangement
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter the order of matrix: ");
            Int32 order = Int32.Parse(Console.ReadLine());
            if (order % 2 == 0)
            {
                Console.WriteLine("Invalid Input!!");
                return;
            }

            Int32[,] matrix = new Int32[order, order];
            for (int i = 0; i < order; i++)
            {
                for (int j = 0; j < order; j++)
                {
                    Console.Write("Enter the element at [" + i + "][" + j + "] : ");
                    matrix[i, j] = Int32.Parse(Console.ReadLine());
                }
            }

            //Converting the matrix to singel dimentional array
            Int32[] matrixSingel = new Int32[order * order];
            int k = 0;
            for (int i = 0; i < order; i++)
            {
                for (int j = 0; j < order; j++)
                {
                    matrixSingel[k] = matrix[i, j];
                    k++;
                }
            }

            //S
            if (MatrixArrangement.ArrangeMatrix(matrixSingel))
            {
                Console.Write("\n{ ");
                for (int i = 0; i < order * order - 1; i++)
                {
                    Console.Write(matrixSingel[i] + ", ");
                }
                Console.WriteLine(matrixSingel[order * order - 1] + " }\n");
            }
            else
            {
                Console.WriteLine("Invalid Matrix!!");
            }
        }
    }
}

MatrixArrangement.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixArrangement
{
    class MatrixArrangement
    {
        public static bool ArrangeMatrix(Int32[] matrix)
        {
            if (matrix.Length % 2 == 0)
            {
                return false;
            }

            //Sorting the received matrix
            Boolean change = false;
            Int32 temp = 0;
            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < matrix.Length - 1; j++)
                {
                    if (matrix[j] > matrix[j + 1])
                    {
                        change = true;
                        temp = matrix[j];
                        matrix[j] = matrix[j + 1];
                        matrix[j + 1] = temp;
                    }
                }
                if (!change)
                {
                    break;
                }
            }

            //Creating the output array
            Int32[] outMatrix = new Int32[matrix.Length];
            temp = matrix.Length / 2;
            int k = 0;
            outMatrix[temp] = matrix[0];
            k++;

            for (int i = 1; i <= temp; i++)
            {
                outMatrix[temp + i] = matrix[k];
                k++;
                outMatrix[temp - i] = matrix[k];
                k++;
            }

            //Copying the outmatrix to matrix for returning back
            for (int i = 0; i < matrix.Length; i++)
            {
                matrix[i] = outMatrix[i];
            }
            return true;
        }
    }
}

No comments:

Post a Comment