Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Using Single Dimensional Array to Represent Two Dimensional Data

, 30 Aug 2013
Rate this:
Please Sign up or sign in to vote.
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.

I have used the presented techniques in many of my production level applications including the following Silverlight games:

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:

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:

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:

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:

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:

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)

Share

About the Author

Kashif_Imran
Software Developer MathiVersity
Pakistan Pakistan
I am a web developer mostly working in JavaScript. Formerly I used to be a C#/.Net developer. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.
 
I am founder of the Project MathiVersity, which aims to create free software, games and services related to mathematics and its allied disciplines.

Comments and Discussions

 
GeneralMy vote of 3 PinmemberCharles J Cooper27-Dec-12 8:37 
GeneralRe: My vote of 3 PinmemberKashif_Imran27-Dec-12 8:55 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 30 Aug 2013
Article Copyright 2012 by Kashif_Imran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid