Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

Using Single Dimensional Array to Represent Two Dimensional Data

Rate me:
Please Sign up or sign in to vote.
4.50/5 (9 votes)
30 Aug 2013CPOL2 min read 62.4K   561   11   13
Shows how to treat a single dimensional array for both single and two dimensional data at the same time

Introduction

The techniques presented in this article have many useful applications. For example, in one of my previous articles, I used a List<double> to store and manipulate all the matrices.

When you want to store some tabular data, you usually think of a two dimensional data structure. However, in this article we will see that on many occasions storing the data itself in a single dimensional way will make internal tasks simpler. The single dimensional source does not stop us from presenting or using the data in two dimensional way. So we can, at the same time, perform two dimensional operations on it.

Uses

One obvious question that comes to your mind is why store two dimensional data in a single dimensional data source. Here is a list of why one would want to do so:

  • The number of rows and columns is not known in advance and will only be known when needed.
  • The data is bound to some inherently single dimensional source at the back end but needs to be accessed in two dimensional way at run-time.
  • The data can be accessed in single and/or two dimensional manner at the same time.
  • The number of rows and columns change dynamically.

Two Dimensional Operations on Single Dimensional Data Structures

In the following sections, we shall see how to perform different two dimensional operations on a single dimensional list.

Getting Row Number of an Item

Given the column count and index of an item in the array, the row number can be obtained by calculating the quotient using integral division like this:

C#
int [] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int columnCount = 3;
for (int i=0; i < numbers.Length; i++)
{ 
    int rowNumberOfItem = i/columnCount; //row number of the item at index 'i'
}

Getting Column Number

Given the index of an item in the array, the column number can be obtained by calculating the remainder using integral division like this:

C#
int [] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int columnCount = 3;
for (int i=0; i < numbers.Length; i++)
{ 
    int columnNumberOfItem = i%columnCount; //column number of the item at index 'i'
}

Swapping Rows and Columns

Following is one of the possible solutions:

C#
int [] original = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int [] transpose = new int[original.Length];
int columns = 3;
int rows = original.Length/columns;
for (int i = 0; i < columns; i++)
{
    for (int j = 0; j < rows; j++)
    {
        transpose[i * rows + j] = original[j * columns + i]; 
    }
}

Accessing all the Column Values at a Given Index using a Loop

This task can be carried out using something like this:

C#
int [] original = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int columns = 3;
int rows = original.Length/columns;
int [] columnValues = new int [rows]; //to store the item values at the given column index!
int index = 2; //the column of which the values are to be accessed. 
               //Any valid column index will do!
for (int i = 0; i < rows; i++)
{
    columnValues[i] = original[i*columns + index];
}

Accessing all the Row Values at a Given Index using a Loop

This task can be carried out using something like this:

C#
int [] original = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int columns = 3;
int rows = original.Length/columns;
int [] rowValues = new int [columns]; //to store the item values at the given row index!
int index = 2; //the row of which the values are to be accessed. Any valid row index will do!
for (int i = 0; i < columns; i++)
{
    rowValues[i] = original[index*columns + i];
}

Conclusion

We provided a quick overview of how a single dimensional array can be treated as a two dimensional data structure. We also noted how and when this approach can be useful.

For a much more elaborate use of this technique, refer to the Matrix implementation of the sample project provided with my previous article: Building a General Purpose Interpreter, Math Engine and Parser in C#, where all the matrix support is built using the simple techniques presented here.

Please let me know if you find anything unclear or missing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Shanth Kumar2-Sep-13 16:04
Shanth Kumar2-Sep-13 16:04 
GeneralMessage Closed Pin
2-Sep-13 18:10
Kashif_Imran2-Sep-13 18:10 
GeneralRe: My vote of 3 Pin
Shanth Kumar3-Sep-13 5:01
Shanth Kumar3-Sep-13 5:01 
GeneralMy vote of 4 Pin
phil.o30-Aug-13 6:21
professionalphil.o30-Aug-13 6:21 
GeneralRe: My vote of 4 Pin
Kashif_Imran30-Aug-13 6:32
Kashif_Imran30-Aug-13 6:32 
Questionnice code Pin
ashumeerut13-Aug-13 22:20
ashumeerut13-Aug-13 22:20 
AnswerRe: nice code Pin
Kashif_Imran13-Aug-13 23:10
Kashif_Imran13-Aug-13 23:10 
GeneralMy vote of 4 Pin
fredatcodeproject11-Aug-13 21:46
professionalfredatcodeproject11-Aug-13 21:46 
GeneralRe: My vote of 4 Pin
Kashif_Imran11-Aug-13 23:00
Kashif_Imran11-Aug-13 23:00 
QuestionSingle dimensional array to represent multi-dimensional array PinPopular
Mizan Rahman29-Jul-13 21:44
Mizan Rahman29-Jul-13 21:44 
AnswerRe: Single dimensional array to represent multi-dimensional array Pin
Kashif_Imran29-Jul-13 21:47
Kashif_Imran29-Jul-13 21:47 
GeneralMy vote of 3 Pin
Charles J Cooper27-Dec-12 8:37
Charles J Cooper27-Dec-12 8:37 
GeneralRe: My vote of 3 Pin
Kashif_Imran27-Dec-12 8:55
Kashif_Imran27-Dec-12 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.