Q:

Write C# Program to Convert a 2D Array into 1D Array

0

Write C# Program to Convert a 2D Array into 1D Array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
 
namespace Program
{
    class twodmatrix
    {
        int p, r;
        int[,] a;
        int[] b;
        twodmatrix(int x, int y)
        {
            p = x;
            r = y;
            a = new int[p, r];
            b = new int[p * r];
        }
        public void readmatrix()
        {
            for (int i = 0; i < p; i++)
            {
                for (int j = 0; j < r; j++)
                {
                    Console.Write("a[{0},{1}]=", i, j);
                    a[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public void printtwodimentionalarray()
        {
            for (int i = 0; i < p; i++)
            {
                for (int j = 0; j < r; j++)
                {
                    Console.Write("{0}\t", a[i, j]);
 
                }
                Console.Write("\n");
            }
        }
        public void convert()
        {
            int k = 0;
            for (int i = 0; i < p; i++)
            {
                for (int j = 0; j < r; j++)
                {
                    b[k++] = a[i, j];
                }
            }
        }
        public void printonedimentionalarray()
        {
            for (int i = 0; i < p * r; i++)
            {
                Console.WriteLine("{0}\t", b[i]);
            }
        }
 
 
        public static void Main(string[] args)
        {
            twodmatrix obj = new twodmatrix(2, 3);
            Console.WriteLine("Enter the Elements : ");
            obj.readmatrix();
            Console.WriteLine("\nGiven 2-D Array() is : ");
            obj.printtwodimentionalarray();
            obj.convert();
            Console.WriteLine("\nConverted 1-D Array is : ");
            obj.printonedimentionalarray();
            Console.ReadLine();
        }
    }
}

Result:

Enter the Elements : 

a[0,0]=1

a[0,1]=2

a[0,2]=3

a[1,0]=4

a[1,1]=5

a[1,2]=6

Given 2-D Array() is : 

1 2 3

4 5 6

Converted 1-D Array is : 

1

2

3

4

5

6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C# Program to Demonstrate Jagged Arrays... >>
<< Write C# Program to get the Length of the Array...