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 6947183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:39:41+00:00 2026-05-27T13:39:41+00:00

I want to create a transpose function for dynamic 2d arrays. I want the

  • 0

I want to create a transpose function for dynamic 2d arrays. I want the functions to have as parameters the 2d array and the rows and columns. I ve decided to use double pointer. However i m a bit confused about how i gonna call the function from main. So i ve got the above code

#include<iostream>
using namespace std;


void transposeMatrix(double **mat, int rows, int columns)
{


   mat = new double*[rows];

   for (int i = 0; i < rows; ++i)
   {
      mat[i] = new double[columns];
   }


   double temp;

   for (int i = 0; i<rows; i++)
   {

      for (int j = i+1; j<columns; j++)
      {

         temp=mat[i][j];
         mat[i][j]=mat[j][i];
         mat[j][i]=temp;
      }
   }


   cout<< "\n";

   for (int i = 0; i<rows; i++)
   {

      for (int j = 0; j<columns; j++)
      {

         cout << mat[i][j] << " \t";
      }

      cout << "\n";
   }
}


int main()
{
   int rows = 10;
   int columns = 10;
   double mat[rows][columns];

   for (int i = 0; i<rows; i++)
   {

      for (int j = 0; j<columns; j++)
      {

         mat[i][j] = j;
      }
   }

   for (int i = 0; i<rows; i++)
   {

      for (int j = 0; j<columns; j++)
      {

         cout << mat[i][j] << " \t";
      }

      cout << "\n";
   }

   //mat = new double[50][1];
   transposeMatrix(mat, 10, 10);



   system("pause");
   return 0;
}

Any idea?

  • 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-05-27T13:39:42+00:00Added an answer on May 27, 2026 at 1:39 pm

    You’re very close. You’re calling the function correctly and the function’s parameter list is correct. First, remove this section from the transpose function:

     mat = new double*[rows];
    
     for (int i = 0; i < rows; ++i)
         mat[i] = new double[columns];
    
     }
    

    Now make sure all your brackets match up. (There was one missing.)
    You can’t define a static array (one that looks like this: x[y][z]) with non-constant variables as the size arguments. (I.e. y and z must be constants.) But actually, you’re passing a dynamic array to the transpose function anyway, and rows and columns don’t have to be constants to do that. So, in main, define a dynamic array like this:

    double** mat = new double*[rows];
    for (int i = 0; i < rows; i++)
        mat[i] = new double[columns];
    

    After that, your code should work. But you could also make it better by putting your matrix display code in a function. Then, instead of cutting and pasting it everywhere, all you have to do is call the function! It’s an important habit to get into. Have fun!

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

Sidebar

Related Questions

I have a table and want to transpose its rows to columns, similar to
i want create image animation , i have 50 images with png format now
I want to create a function that performs a function passed by parameter on
I want create a process under another user. So I use LogonUser and CreateProcessAsUser.
I want create subdomains like this: domain.com/type/city An examples: domain.com/restaurants/new_york domain.com/hotels/new_york domain.com/restaurants/chicago I have
i use cakephp and i want create login element . this layout work as
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the

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.