• 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 one-dimensional array to a two-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 one-dimensional array to a two-dimensional array in C# with an example. Please read our previous article where we discussed how to convert a two-dimensional array to a one-dimensional array in C# with some examples.

Program Description:
Here, the user will input a one-dimensional array along with the number of rows and columns for the two-dimensional array. Then we need to convert the one-dimensional array to a two-dimensional array based on the given rows and columns. For a better understanding of what we want to do, please have a look at the following diagram.
How to convert a one-dimensional array to a two-dimensional array in C#

Note: The number of elements in the array should be the multiplication of the number of rows and number of columns.

Creating a 2d Array from 1D Array in C#:
In the following program, first, we take the number of rows and columns for the 2d array from the user. Then we create and populate the 1d array with the required data. Based on the number of rows and columns we created the 2d array and using two loops we populate the 2d array with data coming from the 1d array.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Number of Rows : ");
            int Rows = int.Parse(Console.ReadLine());
            Console.Write("Enter the Number of Columns : ");
            int Columns = int.Parse(Console.ReadLine());
            //Creating a 1d Array 
            Console.WriteLine("Enter the 1D Array Elements : ");
            int[] oneDimensionalArray = new int[Rows * Columns];
            for(int i = 0; i< oneDimensionalArray.Length; i++)
            {
                oneDimensionalArray[i] = int.Parse(Console.ReadLine());
            }
            
            //Creating 2d Array
            int index = 0;
            int[,] twoDimensionalArray = new int[Rows, Columns];
            
            for (int x = 0; x < Rows; x++)
            {
                for (int y = 0; y < Columns; y++)
                {
                    twoDimensionalArray[x, y] = oneDimensionalArray[index];
                    index++;
                }
            }
            //Printing the 2D array elements
            Console.WriteLine("2D Array Elements : ");
            foreach (int item in twoDimensionalArray)
            {
                Console.Write(item + " ");
            }
            Console.ReadKey();
        }
    }
}
Output:
How to convert a one-dimensional array to a two-dimensional array in C#

In the next article, I am going to discuss How to perform Left circular rotation of an array in C# with examples. I hope now you understood How to convert a one-dimensional array to a two-dimensional array in C#.

Summary:
I hope this post will be helpful to understand How to convert a one-dimensional array to a two-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

  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • Usability of SecureString object in C#
    Introduction Hi friends! In this blog we will be discussing a very interesting as well as useful topic in C# and that is Securestring objec...
  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...
  • 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...
  • Application Life cycle of Asp.Net MVC
    Today i am going to tell you the Application life cycle of an Asp.Net MVC Application, this post is important for those who are new to Asp.N...
  • C# Programming Interview Questions
    Today i am going to tell you some of the basic programming questions that are frequently asked in interview for fresher and experienced as w...
  • OOPS – Difference Between Class and Struct
    Hi friends! Today we are going to discuss a very quite interesting and important topic of Object Oriented Programming(OOPs) i.e. “Classes an...

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