Reverse Diagnal of matrices

Write a C++ program that inputs a square matrix and reverse the contents of its diagonal values.
Example
{1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16}

output
{4 2 3 1
5 7 6 8
9 11 10 12
16 14 15 13}

Please help in getting a code i know how to add, subtract and multiply but i cannot seem to undersand how this works.
Last edited on
Do not double-post. Other thread: http://www.cplusplus.com/forum/beginner/267372/
Last edited on
1
2
for i=0 --> 4
    swap (M[i][i], M[i][3-i])


Please don't just copy paste this code but try to understand what's going 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
53
54
55
56
/* full code in C */
#include <stdio.h>

typedef int mat4_t[4][4]; /* now 'mat_4 x;' is equivalent to
                            'int x[4][4];' */

void ScanMatrix (mat4_t M)
{
    int i, j;
    for (i = 0; i < 4; i++)
        for (j = 0; j < 4; j++)
        {
            printf ("element [%d][%d]: ", i, j);
            scanf ("%d", &M[i][j]);
        }
}

void PrintMatrix (mat4_t M)
{
    int i, j;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
            printf ("%4d", M[i][j]);
        printf ("\n");
    }
}

void Swap (int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

void ReverseDiagonalValues (mat4_t M)
{
    int i, j;

    for (i = 0; i < 4; i++)
        Swap (&M[i][i], &M[i][3-i]);
}

int main ()
{
    mat4_t M;

    ScanMatrix (M);
    
    ReverseDiagonalValues (M);

    PrintMatrix (M);

    /* maybe pause the console? */
    return 0;
}
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
#include <iostream>
#define size 4
using namespace std;

void inputVals(int arr[size][size]){
    for(int i = 0; i<size; ++i){
        cout<<"row "<<i+1<<endl;
        for(int j = 0; j<size; ++j){
            cout<<"element "<<"["<<i<<"]"<<"["<<j<<"]"<<": ";
            cin>>arr[i][j];}
    }
}
void swapVals(int arr[size][size]){
    for(int i = 0; i <size; ++i){
        for(int j = 0; j <size; ++j)
            if(i==j)swap(arr[i][j],arr[i][3-i]);}
    cout<<endl;
}

void printVals(int arr[size][size]){
    for(int i = 0; i<size; ++i){
        for(int j = 0; j<size; ++j)
            {cout.width(3);
             cout<<left<<arr[i][j];}
        cout<<endl;}

}

int main(){
    int numbers[size][size];
    inputVals((numbers));
    swapVals(numbers);
    printVals(numbers);

    return 0;
}
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
#include <iostream>
#include <valarray>
#include <algorithm>
#include <numeric>
using namespace std;

template<typename T> void swapDiagonals( valarray<T> &A, int N )
{
   auto diag1 = slice(0,N,N+1), diag2 = slice(N-1,N,N-1);
   valarray<T> temp = A[diag1];
   A[diag1] = A[diag2];
   A[diag2] = temp;
}

template<typename T> void print( const valarray<T> A, int N )
{
   int i = 0;
   for ( T e : A ) { cout << e << '\t';  if ( !((++i) % N) ) cout << '\n'; }
   cout << '\n';
}

int main()
{
   int N = 4;
   valarray<int> A(N*N);
   iota( begin(A), end(A), 1 );
   print( A, N );
   swapDiagonals( A, N );
   print( A, N );

   N = 5;
   valarray<char> B(N*N);
   iota( begin(B), end(B), 'a' );
   print( B, N );
   swapDiagonals( B, N );
   print( B, N );
}


1	2	3	4	
5	6	7	8	
9	10	11	12	
13	14	15	16	

4	2	3	1	
5	7	6	8	
9	11	10	12	
16	14	15	13	

a	b	c	d	e	
f	g	h	i	j	
k	l	m	n	o	
p	q	r	s	t	
u	v	w	x	y	

e	b	c	d	a	
f	i	h	g	j	
k	l	m	n	o	
p	s	r	q	t	
y	v	w	x	u	
Topic archived. No new replies allowed.