Transpose

Am trying to find transpose for the following array but there are errors , there are not valid function protype, and passing of argument

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
  #include <iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>

using namespace std;
void transpose(int t[w][],int t [][l])
{
    for( int j=0;j<w;j++)
    {
        for(int i=0;i<l;i++)
        {
            t[i][j]=t[j][i];

        }
    }
    for(int j=0;j<w;j++)
    {
        for(int i=0;i<l;i++)
        {
            cout<<arr[i][j]

        }
    cout<<endl;
    }


}
int main ()
{
const int width=5;
const int length=5;
int arr[width][length]={1,2,3,4};
cout<<"Before\n";
   for(int i=0;i<w;i++)
    {
        for(int j=0;j<l;j++)
        {
            cout<<arr[i][j];

        }
    cout<<endl;
    }
    cout<<"After\n";
transpose(int arr[width][],int arr [][length]);

 getch();
}
Function prototype must be as follows:

void transpose(int (*t)[w],int (*t)[l])
Last edited on
error: 'w' was not declared in this scope|
w is the width of the array, which in your case is 5. Since you're working with matrices whose dimensions you know already, I'd suggest you use two global variables const int WIDTH = 5 and const int LENGTH = 5
Last edited on
After change
error: expected unqualified-id before 'int'|
error: expected ')' before ',' token|
error: 'w' was not declared in this scope|
Are you using your computer
Sharan123 wrote:
Are you using your computer
I'm using VS 12 Express, C++11

See here's the desired fragment:
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
.
.

#define WIDTH 5
#define LENGTH 5

void transpose(int (*t)[WIDTH],int (*t)[LENGTH])
{
   // Stuff...
}
.
.
int main()
{
.
.
    int mat[LENGTH][WIDTH] = {
                              {1, 2, 3},
                              {4, 5, 6},
                              {7, 8, 9}
                             };
   transpose(mat);
.
.
}
.
.


Are you doing exactly this?
Last edited on
There is error in line 49
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
#include <iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
#define WIDTH 5
#define LENGTH 5

using namespace std;
void transpose(int *t[][WIDTH],int *t1[][LENGTH])
{

    for( int j=0;j<WIDTH;j++)
    {
        for(int i=0;i<LENGTH;i++)
        {
            t[i][j]=t[j][i];

        }
    }
    for(int j=0;j<WIDTH;j++)
    {
        for(int i=0;i<LENGTH;i++)
        {
            cout<<t[i][j];

        }
    cout<<endl;
    }


}
int main ()
{
int arr[WIDTH][LENGTH]={{1,2,3,4},
                       {1,2,3,4},
                       {1,2,3,4},
                       {1,2,3,4},};
cout<<"Before\n";
   for(int i=0;i<WIDTH;i++)
    {
        for(int j=0;j<LENGTH;j++)
        {
            cout<<arr[i][j];

        }
    cout<<endl;
    }
    cout<<"After\n";
transpose(arr);//error: cannot convert 'int (*)[5]' to 'int* (*)[5]' for argument '1' to 'void transpose(int* (*)[5], int* (*)[5])'|

 getch();
}
No, look closely...

I have already told this and again tell you that function prototype must be:
void transpose(int (*t)[WIDTH],int (*t)[LENGTH])
not
void transpose(int *t[][WIDTH],int *t1[][LENGTH])

In the 1st prototype, arguments are arrays of pointer(2D pointers) whereas your prototype, ie, the 2nd one, is 3D. Hope I am clear and you can make out the difference between the two.
Last edited on
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
#include <iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
#define WIDTH 5
#define LENGTH 5

using namespace std;
void transpose(int (*t)[WIDTH],int (*t)[LENGTH])
{

    for( int j=0;j<WIDTH;j++)
    {
        for(int i=0;i<LENGTH;i++)
        {
            t[i][j]=t[j][i];

        }
    }
    for(int j=0;j<WIDTH;j++)
    {
        for(int i=0;i<LENGTH;i++)
        {
            cout<<t[i][j];

        }
    cout<<endl;
    }


}
int main ()
{
int arr[WIDTH][LENGTH]={{1,2,3,4},
                       {1,2,3,4},
                       {1,2,3,4},
                       {1,2,3,4},};
cout<<"Before\n";
   for(int i=0;i<WIDTH;i++)
    {
        for(int j=0;j<LENGTH;j++)
        {
            cout<<arr[i][j];

        }
    cout<<endl;
    }
    cout<<"After\n";
transpose(arr);

 getch();
}

still causes error i implicitly did that try it in your compiler
Here are the problems in your current code :-

1. void transpose(int (*t)[WIDTH],int (*t)[LENGTH])

Same identifiers for arguments.

2. transpose(arr);

See the prototype accepts two matrices, and you provide only one. When I gave you a pseudo code, I thought you'd add the second one by yourself and make necessary changes.

3. Incorrect procedure for calculating transpose.

I didn't give your functions a thought until now but now I see why you are confused. You've been calculating the transpose incorrectly. Just doing
1
2
3
4
5
6
7
8
for( int j=0;j<WIDTH;j++)
    {
        for(int i=0;i<LENGTH;i++)
        {
            t[i][j]=t[j][i];

        }
    }
doesn't transpose. :)

Here's the correct and fully working code(I've tested it in VS-12, ANSI C++11):

PLZ. NOTE : This code is valid for SQUARE MATRICES ONLY.

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
#include <iostream>
#define MAT_L 4
#define MAT_W 4

using namespace std;

void Transpose(int (*MAT_1)[MAT_W], int (*MAT_2)[MAT_W])
{
	for(int R = 0; R < MAT_L; R++)
		for(int C = 0; C < MAT_W; C++)
			MAT_2[C][R] = MAT_1[R][C];

	for(int C = 0; C < MAT_W; C++)
	{
		for(int R = 0; R < MAT_L; R++)
			cout << MAT_2[C][R] << " ";
		cout << endl;
	}
}

int main()
{
	int mat1[MAT_L][MAT_W] = { {1, 2, 3, 4},
				   {5, 6, 7, 8},
	                           {9, 10, 11, 12},
				   {13, 14, 15, 16}
	                         };
	int mat2[MAT_W][MAT_L] = { {0, 0, 0, 0},
				   {0, 0, 0, 0},
	                           {0, 0, 0, 0},
				   {0, 0, 0, 0}
	                         };
	
	cout << "\nBefore transposing :-\n" << endl;
	for(int R = 0; R < MAT_L; R++)
	{
		for(int C = 0; C < MAT_W; C++)
			cout << mat1[R][C] << " ";
		cout << endl;
	}

        cout << "\nAfter transposing matrix :-\n" << endl;

	Transpose(mat1, mat2);
	
	system("pause");
	return 0;
}

Nice coding you have set the 1st array column to the 2nd array row i didn't thought about that , thank you how many years have you been programming i am embarced i have been programming from an year i am nothing compared to you
Topic archived. No new replies allowed.