answersLogoWhite

0


Best Answer

You don't need to physically convert the array, you simply need to point at the first element and read the elements sequentially. The following code shows how to achieve this, as well as how to physically convert a 2D array to a separate 1D array. As can be seen, there is no practical benefit to making the conversion.

Example

#include

int main()

{

const int rows = 2;

const int cols = 3;

const int size = rows * cols;

// Create a static 2D array, intialise and print the matrix

int arr2[rows][cols];

std::cout << "2D array:\n" << std::endl;

for( int r=0; r

{

for( int c=0; c

{

arr2[r][c]= (r+1) * (c+1);

std::cout << arr2[r][c] << " ";

}

std::cout << std::endl;

}

std::cout << std::endl;

// Efficient conversion to 1D array:

// Point to the first element then print sequential values.

int* ptr = (int*) arr2;

std::cout << "1D array (using pointer):\n" << std::endl;

for( int i=0; i

std::cout << *ptr++ << " ";

std::cout << "\n" << std::endl;

// Inefficient conversion to 1D array:

// Copy the 2D array to a separate 1D array

int arr1[size];

ptr = (int*) arr2;

for( int i=0; i

arr1[i]=*ptr++;

// Print the copy:

std::cout << "1D array (using copy):\n" << std::endl;

for( int i=0; i

std::cout << arr1[i] << " ";

std::cout << "\n" << std::endl;

return( 0 );

}

Output

2D array:

1 2 3

2 4 6

1D array (using pointer):

1 2 3 2 4 6

1D array (using copy):

1 2 3 2 4 6

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

All arrays are one-dimensional. Consider the following C-style array:

int array2d[5][4];

for (int row=0; row<5; ++row) for (int col=0; col<4; ++col) array2d[row][col] = row * col;

Given that the elements of array2d are allocated contiguously, we can easily convert it to a one-dimensional array of type int[20] simply by casting the start address to the appropriate type (pointer to int):

int* array1d = static_cast(&array2d);

for (int i=0; i<20; ++i) array1d[i] = i;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to convert 2D array to single dimensional array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Subtraction of 2d array in java?

looda le lo mera


What is a program to sort a two dimensional integer array?

Any multi-dimensional array can be flattened into a linear array. For instance,[[1,2,3],[4,5,6],[7,8,9]]can be flattened into[1,2,3,4,5,6,7,8,9].So a solution to your problem (certainly not the most efficient) would be to flatten the 2d array into a linear array, and sort using a traditional sorting algorithm or Arrays.sort. You would then insert the sorted elements back into the 2d array. This would have nlog(n) complexity.An implementation below:public static void sort2d(int[][] arr){int r = arr.length;int c = arr[0].length;int[] flat = new int[r*c];for (int i = 0; i < r; i++)for (int j = 0; j < c; j++)flat[i*c+j] = arr[i][j];Arrays.sort(flat);for (int i = 0; i < flat.length; i++)arr[i/r][i%c] = flat[i];}


What is an array to show 4X4?

An array of 4 times 8 is a 2D array with 4 Rows and 8 Columns. // Declaration of a 2D array [ROWS] [COLUMNS] int [][] arr = new int[4][8]; // What a 2D Array looks like populated with numbers // // _____________________ // Row 0 | 1 0 0 0 0 0 0 7 // Row 1 | 0 0 0 0 0 0 0 0 // Row 2 | 0 0 0 0 0 0 4 0 // Row 3 | 0 0 0 0 0 0 0 0 The example array above has the number 1 place in the first row of the first column. In the first row of the eight column the number 7 is placed. In the third row of the seventh column the number 4 is placed. // Accessing these values int one = arr[0][0]; int seven = arr[0][7]; int four = arr[2][6];


What is Difference between one dimensional array and two dimensional?

A matrix (two dimensional array) is a group of lists, or arrays that are organized into one data set. To understand them, first look an example of the standard one dimensional array:Array "A":1689905You can reference any point in the "array" by naming the array and following it with a number representing the position of a piece of data on the list. The first data on the array is represented with a "0", the second with a "1", and so on. For example, A[2] (in bold) represents 99 because it is the third figure of array A. You could imagine the references of the above table as the following:Array "A":[0][1][2][3][4]A matrix is a set of arrays. Visualize the following table:Matrix "B":16 17 98 88 7499 12 210 6 405 19 18There are 3 different arrays in a single data set, "B". In Array A, you could reference any data by naming the point on the list, such as "1" or "3". However, with a matrix, you must give both a row and a column. You can reference data on a matrix in the following format:MatrixName[row][column]For example, B[3][2] would represent 40 because it is the it is on the fourth row down and the third column. Remember, matrix references also start at zero, not one! You can reference any of the data on this table with this chart:Matrix "B":[0][0] [0][1] [0][2][1][0] [1][1] [1][2][2][0] [2][1] [2][2][3][0] [3][1] [3][2][4][0] [4][1] [4][2]Two-dimensional arrays are used everywhere, but are especially prevalent in computer programming and accounting. If you know any programmers, ask them the last time the last time they used a matrix- I'm sure they'll give you plenty of examples!


How do you declare an int array?

we define the array isArray array[]={1,2,3,4,5} this is the integer ArrayArray array[]={"apple","banana","carrot","mango"} this is the String Array