Copy array function

Hello. I have write a function that copies array A[3][4] into array B[4][3] such that, the first column of array A is copied into the first row of array B, and the second column of, array A is copied into the second row of array B.
I have done this:
1
2
3
4
5
6
7
8
void Copy_Array (int A[][columns], int B[][rows], int rows, int columns)
{
for (int i = 0; i < 3 ; i++){
for (int j = 0; j < 4; j++){
B[i][j] == A[j][i];
}
}
}

(columns = 4, rows = 3)
Though it looks like A was modified into B, and I want to keep them both (And print them later on with two different functions).
How do I do that?
1
2
3
4
5
6
7
	int arrA[4][3];
	int arrB[3][4];
	for(int i=0; i<4; i++) {
		for(j=0;j<3;j++) {
			arrB[j][i] = arrA[i][j];
		}
	}
Your function is copying array B into array A, if you want A to be constant then you would need a third array.

@jlb
I can only use an initial "Fill array" function to fill A, the "Copy array" to copy A to B, Print A function and Print B function. I need A to be constant with these. How do you do it with a third array?
Last edited on
you can copy A to B as i showed without a third array.
Please show a small complete program that illustrates your problem.

Remember that when you pass arrays into your function any changes made to the arrays in that function change the variables in the calling function.

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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int columns = 4;
const int rows = 3;
void Fill_Array (int [][columns] , int , int );
void Copy_Array (int [][columns], int [][rows], int, int);
void Print_A (int [][columns] , int , int  );
void Print_B (int [][rows] , int , int  );
int main() {
 int A[rows][columns];
 int B[columns][rows];
Fill_Array ( A, rows, columns);
Copy_Array (A, B, rows, columns );
Print_A(A, rows, columns);
Print_B (B, rows, columns);
	return 0;
}
void Fill_Array (int A[][columns], int rows, int columns)

{   
	
	srand(time (0)*2 -1);
	 
for (int i =0; i < 3; i++){
for (int j = 0; j < 4 ; j = j + 2){
	
A[i][j] = 2*(1 + rand() % 24) + 1;
}
for (int j = 1; j < 4; j = j + 2)
A[i][j] = 2*(1 + rand() % 25) ;
}

}
void Copy_Array (int A[][columns], int B[][rows], int rows, int columns)
{
for (int i = 0; i < 3 ; i++){
for (int j = 0; j < 4; j++){
A[i][j] == B[i][j];

}
}
}
void Print_A (int A[][columns], int rows, int columns)
{
for (int i = 0; i < 3; i++){
	for (int j = 0; j < 4; j++){
cout << A[i][j] << "   ";
if (j == 3)
cout << endl;
}
}
}

void Print_B (int B[][rows], int rows, int columns)
{
	cout << endl;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 4; j++){
cout << B[j][i] << "   ";
if (j == 2)
cout << endl;
}
}
}


This is the complete program up until the problem. I need to print A and B independently and not to modify A in the process.
1. if you use global variables then you need not pass them to functions as arguments.

2. sometimes you used rows\columns and sometimes 3\4 > use any one type.

3. line40, A[i][j] == B[i][j]; what? are you coping B to A ?!

4. line 50, if (j == 3) if condition isn't required. just cout<< endl; after inner loop.

5. use single function to print all arrays.
In your original post, you say that columns=4 and rows=3. That means Copy_Array is the same as:
1
2
3
4
5
6
7
8
void Copy_Array (int A[][4], int B[][3], int rows, int columns)
{
    for (int i = 0; i < 3 ; i++){
        for (int j = 0; j < 4; j++){
            B[i][j] == A[j][i];
        }
    }
}

The last pass through the loop you execute B[2][3] = A[3][2]. Here B's second index is out of bounds.
Sorry I modified that just before, but in fact it doesn't work even like 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
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
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int columns = 4;
const int rows = 3;
void Fill_Array (int [][columns] , int , int );
void Copy_Array (int [][columns], int [][rows], int, int);
void Print_A (int [][columns] , int , int  );
void Print_B (int [][columns] , int , int  );
void Print_Statistics (int [][columns], int, int);
int main() {
int A[rows][columns];
int B[columns][rows];
Fill_Array ( A, rows, columns);
Print_A(A, rows, columns);
Copy_Array (A, B, rows, columns );
Print_B (B, rows, columns);
Print_Statistics(A, rows, columns);
	return 0;
}
void Fill_Array (int A[][columns], int rows, int columns)

{   
	
	srand(time (0)*2 -1);
	 
for (int i =0; i < 3; i++){
for (int j = 0; j < 4 ; j = j + 2){
	
A[i][j] = 2*(1 + rand() % 24) + 1;
}
for (int j = 1; j < 4; j = j + 2)
A[i][j] = 2*(1 + rand() % 25) ;
}

}
void Copy_Array (int A[][columns], int B[][rows], int rows, int columns)
{
for (int i = 0; i < 3 ; i++){
for (int j = 0; j < 4; j++){
A[i][j] == B[j][i];
}
}
}
void Print_A (int A[][columns], int rows, int columns)
{
for (int i = 0; i < 3; i++){
	for (int j = 0; j < 4; j++){
cout << A[i][j] << "   ";
if (j == 3)
cout << endl;
}
}
}

void Print_B (int B[][rows], int rows, int columns)
{
	cout << endl;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 4; j++){
cout << B[j][i] << "   ";
if (j == 2)
cout << endl;
}
}
}


Maybe it is something simple but I don't know what I'm missing.
Check the differences between how the B array is defined and how the function prototype/definition expects it (e.g using 'rows' vs 'columns')

error: cannot convert 'int (*)[3]' to 'int (*)[4]' for argument '1' to 'void Print_B(int (*)[4], int, int)'


1
2
3
4
5
6
7
8
9
10
const int columns = 4;
const int rows = 3;
//
void Print_B (int [][columns] , int , int  );
//
int B[columns][rows];
//
Print_B (B, rows, columns);
//
void Print_B (int B[][rows], int rows, int columns)



Also - compiler is warning about this line.

warning: statement has no effect [-Wunused-value]
1
2
3
4
5
6
7
8
9
10
void Copy_Array (int A[][columns], int B[][rows], int rows, int columns)
{
    for (int i = 0; i < 3 ; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            A[i][j] == B[j][i];
        }
    }
}
Please indent your code. It will make it much more readable.

Here is what your main program does:
1
2
3
4
5
6
    int A[rows][columns];
    int B[columns][rows];
    Fill_Array(A, rows, columns);   // fill A
    Print_A(A, rows, columns);      // print A
    Copy_Array(A, B, rows, columns);   // copy uninitialized array B to A
    Print_B(B, rows, columns );     // print B, which is still uninitialized. 


I think part of the reason that you're having so much trouble is that you're using constants 3 and 4, and sometimes using const int's columns and rows, and sometimes using arrays with indeterminate sizes, and sometimes passing parameters columns and rows, which override the constants. Do away with all of that. Use the constants columns and rows throughout. You'll find that the code is much easier to write and to understand. Here is part of it:
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
using namespace std;
const int columns = 4;
const int rows = 3;
void Fill_Array(int[rows][columns]);
void Copy_Array(int src[rows][columns], int dst[columns][rows]);
void Print_A(int[rows][columns]);
void Print_B(int[columns][rows]);
void Print_Statistics(int[rows][columns]);
int
main()
{
    int A[rows][columns];
    int B[columns][rows];
    Fill_Array(A);
    Print_A(A);
    Copy_Array(A, B);
    Print_B(B);
    // Print_Statistics(A);
    return 0;
}

void
Fill_Array(int A[rows][columns])
{

    srand(time(0) * 2 - 1);

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j = j + 2) {
            A[i][j] = 2 * (1 + rand() % 24) + 1;
        }

        for (int j = 1; j < columns; j = j + 2)
            A[i][j] = 2 * (1 + rand() % 25);
    }

}

void
Copy_Array(int src[rows][columns], int dst[columns][rows])
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            dst[j][i] = src[i][j];
        }
    }
}



Topic archived. No new replies allowed.