• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Sunday, August 16, 2020

How to convert a two-dimensional array to one-dimensional array in C#

 Admin     August 16, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss how to convert a two-dimensional array to a one-dimensional array in C# with some examples. Please read our previous article where we discussed How to Find All Possible Substring of a Given String in C# with some examples. As part of this article, we will discuss the following two approaches.
  1. How to convert a 2d array into 1d array column-wise in C#?
  2. How to convert a 2d array into 1d array row-wise in C#?
Program Description:
Here, the user will input a two-dimensional array (i.e. matrix) and we need to convert that 2D array to a one-dimensional array. Here, we will create the one-dimensional array row-wise as well as column-wise. For better understanding please have a look at the following diagram.
How to convert a two-dimensional array to one-dimensional array in C#

Creating a 1D Array from 2D Array Column Wise in C#:
The following program is self-explained. So, please go through the comment lines for better understanding. In the following example, we convert the 2d array to 1d array column-wise.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a 2d Array with 2 rows and three columns
            int[,] int2DArray = new int[2, 3];
            Console.Write("Enter 2D Array Elements : ");
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    int2DArray[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            int index = 0;
            //Getting the no of rows of 2d array 
            int NoOfRows = int2DArray.GetLength(0);
            //Getting the no of columns of the 2d array
            int NoOfColumns = int2DArray.GetLength(1);
            //Creating 1d Array by multiplying NoOfRows and NoOfColumns
            int[] OneDimensionalArray = new int[NoOfRows * NoOfColumns];
            
            //Assigning the elements to 1d Array from 2d array
            for (int y = 0; y < NoOfColumns; y++)
            {
                for (int x = 0; x < NoOfRows ; x++)
                {
                    OneDimensionalArray[index] = int2DArray[x, y];
                    index++;
                }
            }
            //Printing the 1d array elements
            Console.WriteLine("1D Array Elements : ");
            foreach (int item in OneDimensionalArray)
            {
                Console.Write(item + " ");
            }
            Console.ReadKey();
        }
    }
}
Output:
Creating a 1D Array from 2D Array Column Wise in C#

Creating the 1D Array From 2D Array Row Wise in C#:
In the following program, we convert the two-dimensional array to one-dimensional row-wise. For better understanding please go through the comment lines.
using System.Linq;
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a 2d Array with 2 rows and three columns
            int[,] int2DArray = new int[2, 3];
            Console.Write("Enter 2D Array Elements : ");
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    int2DArray[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            int index = 0;
            //Getting the no of rows of 2d array 
            int NoOfRows = int2DArray.GetLength(0);
            //Getting the no of columns of the 2d array
            int NoOfColumns = int2DArray.GetLength(1);
            //Creating 1d Array by multiplying NoOfRows and NoOfColumns
            int[] OneDimensionalArray = new int[NoOfRows * NoOfColumns];
            
            //Assigning the elements to 1d Array from 2d array
            for (int y = 0; y < NoOfRows ; y++)
            {
                for (int x = 0; x < NoOfColumns; x++)
                {
                    OneDimensionalArray[index] = int2DArray[y, x];
                    index++;
                }
            }
            //Printing the 1d array elements
            Console.WriteLine("1D Array Elements : ");
            foreach (int item in OneDimensionalArray)
            {
                Console.Write(item + " ");
            }
            Console.ReadKey();
        }
    }
}
Output:
Creating the 1D Array From 2D Array Row Wise in C#

In the next article, I am going to discuss how to convert a one-dimensional array to a two-dimensional array in C# with examples. I hope now you understood how to convert a two-dimensional array to a one-dimensional array in C# with different mechanisms.

Summary:
I hope this post will be helpful to understand How to convert a two-dimensional array to one-dimensional array in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Creating and Working with Database
    Hi friends! In our last post we have seen different approaches how we can connect with SQL Server Management Studio(SSMS). Today, We are g...
  • Entity Types in Entity Framework
    In this article, I am going to discuss the Entity Types in Entity Framework in detail. Please read our previous article where we discussed...
  • Introduction to Entity Framework
    Before .NET 3.5 as a developer, we often used to write ADO.NET code to perform CRUD operation with the underlying database. For this, we ne...
  • Filters in ASP.Net MVC
    Hi friends! Today we are going to discuss about using a very important feature of MVC i.e. “Filters“ . Filters are a unique feature of Asp...
  • Data Parallelism in C#
    Introduction: Hi, in this blog we are going to discuss a very important feature of C# that is data parallelism. Data parallelism means the ...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape