• 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

  • Web API Versioning using Query String Parameter
    In this article, I am going to discuss Web API Versioning using the Query String Parameter with an example. Please read our previous artic...
  • Static Files Middleware in ASP.NET Core
    In this article, I am going to discuss how to serve static files using Static Files Middleware in ASP.NET Core Application. Please read ou...
  • Cross-Origin Resource Sharing in WEB API
    In this article, I am going to discuss how to enable Cross-Origin Resource Sharing in Web API which allows cross-domain AJAX calls. Pleas...
  • Custom Method Names in Web API
    In this article, I am going to discuss how to create Custom Method Names in Web API application. Please read our previous article before p...
  • Web API Versioning Using Accept Header
    In this article, I am going to discuss Web API Versioning Using Accept Header with an example. This is a continuation part of our previous...
  • Authentication and Authorization in Web API
    In this article, I am going to discuss the Authentication and Authorization in Web API . Here I will give you an overview of Authentication...
  • Parameter Binding in ASP.NET Web API
    In this article, I am going to discuss one of the most important concepts i.e. Parameter Binding in ASP.NET Web API Application . Please re...

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