简体   繁体   中英

2d array to 1d array C#

I've got two algorithms converting random 2d arrays (m х n or m х m) to 1d array. I'm wondering if there is a way to make them work als in the opposite direction and convert the result to 1d array saving the order of numbers. Here is the full code of my program and a picture to see how both of my algorithms work. enter image description here Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using static System.Math;


namespace MyProgram
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter number of rows:");
            int n = int.Parse(Console.ReadLine());
            Console.Write("Enter number of columns:");
            int m = int.Parse(Console.ReadLine());
            int[] arr1 = new int[n * m];
            int[,] arr2 = new int[n, m];
            int choice; 
            do
            {
                Console.WriteLine("Select option:");
                Console.WriteLine("\t1: Diagonal");
                Console.WriteLine("\t2: Spiral");
                Console.WriteLine("\t3: Exit");
                Console.Write("Your selection: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        {
                            SetArray(arr2);
                            PrintArray(arr2);
                            Diagonal(arr2, arr1); 
                            PrintArray(arr1);
                            break;
                        }
                    case 2:
                        {
                            SetArray(arr2);
                            PrintArray(arr2);
                            Spiral(arr2, arr1);
                            PrintArray(arr1);
                            break;
                        }
                }
                Console.WriteLine();
                Console.WriteLine();
            } while (choice != 5);
        }
           static void Diagonal(int[,] array2, int[] array1)
        {
            int k = 0;
            int row = 0;
            int col = 0;
            while (k < array1.Length)
            {

                array1[k] = array2[row, col];
                if ((row + col) % 2 == 0)
                {
                    if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; }
                    else
                    {
                        if (col == array2.GetLength(1) - 1) { row++; }
                        else { row--; col++; }
                    }
                }
                else
                {
                    if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; }
                    else
                    {
                        if (row == array2.GetLength(0) - 1) { col++; }
                        else { row++; col--; }
                    }

                }
                k += 1;
            }
        }

        private static void Spiral(int[,] array2, int[] array1)
        {
            int lengthX = array2.GetLength(0);
            int lengthY = array2.GetLength(1);
            int Product = lengthX * lengthY;
            int CorrectY = 0;
            int CorrectX = 0;
            int Count = 0;
            while (lengthX > 0 && lengthY > 0)
            {
                for (int j = CorrectY; j < lengthY && Count < Product; j++)
                {
                    array1[Count] = array2[CorrectX, j];
                    Count++ ;
                }
                CorrectX++;
                for (int i = CorrectX; i < lengthX && Count < Product; i++)
                {
                    array1[Count] = array2[i, lengthY - 1];
                    Count++ ;
                }
                if (lengthY > 0 && lengthX > 0) lengthY-- ;
                else break;
                for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
                {
                    array1[Count] = array2[lengthX - 1, j];
                    Count++ ;
                }
                if (lengthY > 0 && lengthX > 0) lengthX-- ;
                else break;
                for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
                {
                    array1[Count] = array2[i, CorrectY];
                    Count++ ;
                }

                CorrectY++;
            }

        }

        public static void SetArray(int[,] arr)
        {
            Random r = new Random();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = r.Next(11, 99);
                }
            }
        }

        public static void SetArray(int[] arr)
        {
            Random r = new Random();
            for (int i = 0; i < arr.Length; i++)
            {

                arr[i] = r.Next(11, 99);

            }
        }

        public static void PrintArray(int[] arr)
        {
            Console.Write("print 1d array:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }

       public static void PrintArray(int[,] arr)
        {
            Console.WriteLine("print 2d array:");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }
        } 
    }
}

This seems to be working for me for backward Diagonal :

static void BackwardDiagonal(int[,] array2, int[] array1) {

    int k = 0;
    int row = 0;
    int col = 0;
    while (k < array1.Length) {
        array2[row, col] = array1[k];  // just swap sides of the assignment...
        if ((row + col) % 2 == 0) {
            if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; } else {
                if (col == array2.GetLength(1) - 1) { row++; } else { row--; col++; }
            }
        } else {
            if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; } else {
                if (row == array2.GetLength(0) - 1) { col++; } else { row++; col--; }
            }
        }
        k += 1;
    }
}

For backward Spiral :

private static void BackwardSpiral(int[,] array2, int[] array1)
{
    int lengthX = array2.GetLength(0);
    int lengthY = array2.GetLength(1);
    int Product = lengthX * lengthY;
    int CorrectY = 0;
    int CorrectX = 0;
    int Count = 0;
    while (lengthX > 0 && lengthY > 0)
    {
        for (int j = CorrectY; j < lengthY && Count < Product; j++)
        {
            array2[CorrectX, j] = array1[Count];  // just swap sides of the assignment...
            Count++ ;
        }
        CorrectX++;
        for (int i = CorrectX; i < lengthX && Count < Product; i++)
        {
            array2[i, lengthY - 1] = array1[Count];
            Count++ ;
        }
        if (lengthY > 0 && lengthX > 0) lengthY-- ;
        else break;
        for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
        {
            array2[lengthX - 1, j] = array1[Count];
            Count++ ;
        }
        if (lengthY > 0 && lengthX > 0) lengthX-- ;
        else break;
        for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
        {
            array2[i, CorrectY] = array1[Count];
            Count++ ;
        }

        CorrectY++;
    }
}

I also added this to the switch statement to implement it:

case 4: 
    {
        SetArray(arr2);
        PrintArray(arr2);
        Diagonal(arr2, arr1);
        PrintArray(arr1);
        int[,] arr3 = new int[n, m];  // new blank array to fill with arr1
        BackwardDiagonal(arr3, arr1);  // fill arr3 from backward Diagonal algorithm
        PrintArray(arr3);
        break;
    }
case 5: 
    {
        SetArray(arr2);
        PrintArray(arr2);
        Spiral(arr2, arr1);
        PrintArray(arr1);
        int[,] arr3 = new int[n, m];  // new blank array to fill with arr1
        BackwardSpiral(arr3, arr1);  // fill arr3 from backward Spiral algorithm
        PrintArray(arr3);
        break;
    }

While you're at it, also make sure to have } while (choice != 3); at the end of your do loop so that you can exit the program!

Actually what you are asking is quite easy. Just change your algorithm methods to receive int rows , int cols and delegate like this

delegate void Apply(int row, int col, int index);

Then replace arr2.GetLength(0) with rows , arr2.GetLength(1) with cols , and array element assignments with delegate call.

Here is the updated Diagonal method (you can do the same with the other):

static void Diagonal(int rows, int cols, Apply action)
{
    int k = 0;
    int row = 0;
    int col = 0;
    int length = rows * cols;
    while (k < length)
    {
        action(row, col, k);
        if ((row + col) % 2 == 0)
        {
            if ((row == 0) && (col != cols - 1)) { col++; }
            else
            {
                if (col == cols - 1) { row++; }
                else { row--; col++; }
            }
        }
        else
        {
            if ((col == 0) && (row != rows - 1)) { row++; }
            else
            {
                if (row == rows - 1) { col++; }
                else { row++; col--; }
            }
        }
        k += 1;
    }
}

and the usage:

SetArray(arr2);
PrintArray(arr2);
Diagonal(n, m, (r, c, i) => arr1[i] = arr2[r, c]);
PrintArray(arr1);
// Inverse
var arr3 = new int[n, m];
Diagonal(n, m, (r, c, i) => arr3[r, c] = arr1[i]);
PrintArray(arr3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM