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

This is a C# Program to convert a 2D array into 1D array.

Problem Description

This C# Program Converts a 2D Array into 1D Array.

Problem Solution

Here the elements of the 2-Dimensional matrix are obtained from the user and are then converted and displayed as a 1-Dimensional array.

Program/Source Code

Here is source code of the C# Program to Convert a 2D Array into 1D Array. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Convert a 2D Array into 1D Array
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class twodmatrix
    {
        int m, n;
        int[,] a;
        int[] b;
        twodmatrix(int x, int y)
        {
            m = x;
            n = y;
            a = new int[m, n];
            b = new int[m * n];
        }
        public void readmatrix()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.WriteLine("a[{0},{1}]=", i, j);
                    a[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public void printd()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("{0}\t", a[i, j]);
 
                }
                Console.Write("\n");
            }
        }
        public void convert()
        {
            int k = 0;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    b[k++] = a[i, j];
                }
            }
        }
        public void printoned()
        {
            for (int i = 0; i < m * n; 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("\t\t Given 2-D Array(Matrix) is : ");
            obj.printd();
            obj.convert();
            Console.WriteLine("\t\t Converted 1-D Array is : ");
            obj.printoned();
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# Program, we are reading the elements of the 2-Dimensional matrix. Using for loop assign the value of ‘a[i,j]’ variable to b[] array variable. Increment the value of base index ‘k’ variable. Print the value of one dimensional array.

advertisement
advertisement
Runtime Test Cases
 
Enter the Elements :
a[0,0]=3
a[0,1]=7
a[0,2]=1
a[1,0]=9
a[1,1]=34
a[1,2]=23
Given 2-D Array(Matrix) is :
1  4  3
7  3  8
Converted 1-D Array is :
1
4
3
7
3
8

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.