Make a 2d Array into a Row?

It seems (from http://c-faq.com/aryptr/dynmuldimary.html) that it's possible to convert a 2d array into a row. Is this possible if, say, I've passed a 2d array to a function? Can it then be converted? Thanks, James.
Thats interesting. Do you know how many elements the 2d array would have?? I want to say this is in fact possible if you were wanting to hard code it like if you had a 2 d array u would know how big the single array would have to be, and u could just copy the stuff over. Now if you are trying t transform the 2 d array into a single, that might be impossible.

For a function though you would have to pass in the 2d array and the size aka row and columns of it. Then more then likely in the function you would create something like this

int* pTr = new array[columnsize + rowsize];

the above would create a pointer that points to a single dimmension array . The other code would just loop through and copy the values over.

im just not sure how to return from the function. Unless in the function you passed in a &pTr

Im not sure i might work on this later though. Hope i was of some assistance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;
void mult_to_row(int arg[][3],int columns, int rows, int* &pTr);

/*==================================================
remember to change the number in the prototype and the function below
if for example you want the function to work for a 3 by 7 array it would be
arg[][7] instead of 3. You must specify the depth of the array.
==================================================*/

void main()
{
	
	int x[5][3]; //if the 3 were to be a 4, the function would change to arg[][4] etc
	int* pTr; 

//u will have to change the function if you are not dealing with int data types


 
	

	for(int i = 0; i < 5; i++)  //filling the contents of the array
		for(int j = 0; j < 3; j++)
			x[i][j] = 2;

	mult_to_row(x,5,3,pTr); // function call

	for(int i = 0; i < 15; i++) 
		cout << *(pTr + 0);  // printing out the ptr after it went through function
                 
                 // above i use 15 because to convert to a row it is the row * col  etc
                // this will change if you had say a 2 by 4 array it would be 8 instead of 15


	delete [] pTr; // delete so u dont have memory leak

	
	system("pause"); // yea i know im not supposed to use this

}

void mult_to_row(int arg[][3],int columns, int rows, int* &pTr)
{
	int count = 0; 
	pTr = new int[columns*rows]; 
	for(int i = 0; i < columns; i++)
		for(int j = 0; i < rows; i++)
		{
			*(pTr + count) = arg[i][j]; //copies the array data to the pointer
			count++; //increment to goto next position in the ptr array
		}
}
				


I tried to explain as best as i could. This can be done though, as you can see i did it. However i dont think it is a good practice to use. It can be done though. Just remember if you try to use a different array for example a 5 by 6 array the the function would change to this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void mult_to_row(int arg[][6],int columns, int rows, int* &pTr); // prototype

void mult_to_row(int arg[][6],int columns, int rows, int* &pTr)
{
	int count = 0; 
	pTr = new int[columns*rows]; 
	for(int i = 0; i < columns; i++)
		for(int j = 0; i < rows; i++)
		{
			*(pTr + count) = arg[i][j]; //copies the array data to the pointer
			count++; //increment to goto next position in the ptr array
		}
}

//and you will have to change the first for loop to this
for(int i = 0; i < 5; i++)  //filling the contents of the array
         for(int j = 0; j < 6; j++)
	x[i][j] = 2;

//the function call would look like this
mult_to_row(x,5,6,pTr); 


//and to print the last loop would be this
for(int i = 0; i < 30; i++) //this is 30 because a 5 by 6 array has 30 elements
             cout << *(pTr + 0);  




please feel free to ask any questions.
p.s. i underlined and bolded the pieces that would change lol
Topic archived. No new replies allowed.