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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:33:35+00:00 2026-05-27T07:33:35+00:00

I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns

  • 0

I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns order. The matrix is very big that’s why I have decided just to change pointers instead of copying all of elements. I also have another array which specifies a computation.
Rows permutation specifies this way:
4,3,2,1 – it means that the first row must be on the fourth place, the second row must be on the third place, etc.
The same situation is with the columns.
http://xmages.net/storage/10/1/0/8/6/thumb/thumb_40561a35.jpg

How to implement such algorithm of changing of rows order(permutation of of pointers)? Here it is my version, but it doesn’t work. I want to copy pointers but element is copied instead of it and then appears a segmentation fault.
When I add ‘&’ to get address, the compiler says that it is a syntax error:

orderOfRows[i] = &auxMatrix[computation[i]];

Here it is my code:

static int N = 6;
static int **orderOfRows;
int **sourceMatrix;
int **auxMatrix;

int main() {
int* computation = (int*)malloc(N*sizeof(int));

computation[0] = 1;
computation[1] = 6;
computation[2] = 3;
computation[3] = 7;
computation[4] = 4;
computation[5] = 2;

}
printf("After computation has been done: \n");
changeRowOrder(computation);

 void changeRowOrder(int *computation) {
int i;
// change rows order and dopy them into a temp array
for(i = 0; i < N; ++i) {
    // static arrays
    orderOfRows[i] = auxMatrix[computation[i]];
}
// recover matrix
for(i = 0; i < N; ++i) {
    auxMatrix[i] = orderOfRows[i];
}


void allocate2dMemory() {

int i = 0;
sourceMatrix = (int**)malloc(N * sizeof(int *));

if(sourceMatrix == NULL) {
    fprintf(stderr, "out of memory\n");
    exit(2);
}

for(i = 0; i < N; i++) {
    sourceMatrix[i] = (int*)malloc(N * sizeof(int));
    if(sourceMatrix[i] == NULL) {
        fprintf(stderr, "out of memory\n");
        exit(2);
    }
}

auxMatrix = (int**)malloc(N * sizeof(int *));

if(auxMatrix == NULL) {
    fprintf(stderr, "out of memory\n");
    exit(2);
}

for(i = 0; i < N; i++) {
    auxMatrix[i] = (int*)malloc(N * sizeof(int));
    if(auxMatrix[i] == NULL) {
        fprintf(stderr, "out of memory\n");
        exit(2);
    }
}

 orderOfRows = (int**)malloc(N * sizeof(int *));

if(orderOfRows == NULL) {
    fprintf(stderr, "out of memory\n");
    exit(2);
}

for(i = 0; i < N; i++) {
    orderOfRows[i] = (int*)malloc(N * sizeof(int));
    if(orderOfRows[i] == NULL) {
        fprintf(stderr, "out of memory\n");
        exit(2);
    }
}

}
}

I will spend 2*N(copy pointers and then recover) operations instead of N*N.
And I have another question: how I can do the permutation of columns using this idea? If it isn’t possible how can I do the permutation of columns but not to copy all elements of the matrix? The programming language is only C.

  • 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-27T07:33:36+00:00Added an answer on May 27, 2026 at 7:33 am

    Instead of

    orderOfRows[i] = &auxMatrix[computation[i]];
    

    you should have

    for (int j = 0; j < N; ++j)
    {
        orderOfRows[i][j] = auxMatrix[computation[i]][j];
    }
    

    And one more thing:

    if you havecomputation[1] = 6, it means that by doing auxMatrix[computation[i]] you try to access auxMatrix[6], which you can’t do because auxMatrix‘s size is 6×6.

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

Sidebar

Related Questions

I have a quadratic bezier curve and I need the Y coordinate of a
I have found some mentions in another question of matrix addition being a quadratic
I need to get two points that have biggest distance between. The easiest method
I need to find the quadratic equation term of a graph I have plotted
I have a quadratic bezier curve and I want to calculate the slope of
I have the following code (generates a quadratic function given the a, b, and
I have a quadratic bezier curve described as (startX, startY) to (anchorX, anchorY) and
I have a quadratic curve drawn on html canvas using context.quadraticCurveTo(controlX, controlY, endX, endY);
I have a function to calculate the inverse of a quadratic equation. By default
I have 2 points in X,Y + Rotation and I need to calculate a

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.