Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:07:48+00:00 2026-06-04T20:07:48+00:00

I am trying to transpose a matrix, like below. I am passing the array

  • 0

I am trying to transpose a matrix, like below. I am passing the array by reference, and somehow it is not working (AFTER is same as BEFORE). I am not sure why.

But, What I am really surprised about is that the array is not transposed even inside the transpose() function (how can INSIDE be the same as BEFORE??). What am I missing?

#include <iostream>

template <int M, int N>
void print1(int (&src)[M][N]) {
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++)
            printf("%2d\t", src[i][j]);
        std::cout << std::endl;
    }
}

template <int M, int N>
void transpose(int (&src)[M][N]) {
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
            if (i == j) continue;
            int temp = src[i][j];
            src[i][j] = src[j][i];
            src[j][i] = temp;
        }
    }
    std::cout << "\nINSIDE:\n";
    print1(src);
}

int main() {

    int src[][4] = {
        { 1,  2,  3,  4},
        { 5,  6,  7,  8},
        { 9, 10, 11, 12},
        {13, 14, 15, 16},
    };

    std::cout << "BEFORE:\n";
    print1(src);
    transpose(src);

    std::cout << "\nAFTER:\n";
    print1(src);
}

Output:

$ ./a.out
BEFORE:
 1       2       3       4
 5       6       7       8
 9      10      11      12
13      14      15      16

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

AFTER:
 1       2       3       4
 5       6       7       8
 9      10      11      12
13      14      15      16
$
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:07:51+00:00Added an answer on June 4, 2026 at 8:07 pm

    Are you maybe transposing it twice:

    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
            if (i == j) continue;
            int temp = src[i][j];
            src[i][j] = src[j][i];
            src[j][i] = temp;
        }
    }
    

    i.e. substitute src(i,j) for src(j,i)… and then substitute src(j,i) for src(i,j)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do an Matrix animation where I both scale and transpose a
Trying to comply with StackOverflow's suggestion of asking a question, not creating a discussion,
I'm trying to calculate the inverse matrix in Java. I'm following the adjoint method
I am trying to build an elegant transpose function using functions mapn and zip
I'm trying to get a recipe working that I found online for doing expectation
I am trying to delete both a matrix in mathematica. An inelegant way of
I am trying to transpose columns to rows using query similar to the following...
I am trying move rows of values into a column in Excel. The transpose
I'm trying to test the DB2 adapter for BizTalk 2006 (not R2). While trying
Just a general Matlab matrix trick I am trying to understand? What does this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.