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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:08:48+00:00 2026-05-16T08:08:48+00:00

for example, given a matrix: 1 2 3 4 5 6 7 8 9

  • 0

for example, given a matrix:

1 2 3

4 5 6

7 8 9

if you are goint to swap row[0] and row[1], resulting matrix would be:

4 5 6

1 2 3

7 8 9

can you guys help me get a code in C for this?

  • 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-16T08:08:49+00:00Added an answer on May 16, 2026 at 8:08 am

    The answer depends entirely on how your “matrix” is implemented, because the c language has no notion of such a thing.

    Are you using two dimensional arrays?

    double m[3][3];
    

    Or something else?

    Two dimensional arrays

    You will have to move individual elements by hand.

    for (i=0; i<ROWLENGTH; ++i){
      double temp;
      temp = m[r2][i];
      m[r2][i] = m[r1][i];
      m[r1][i] = temp;
    }
    

    (here r1 and r2 are ints that have been set to the two row you want to swap) or see James’ memcpy implementation which may well be faster but requires a whole rows worth of temporary memeory.

    Ragged Arrays

    If this operation is very common and profiling reveals that it is consuming a lot of time, you might consider using a ragged array implementation of the matrix. Something like this:

    double **m;
    m = malloc(sizeof(double*)*NUMROWS);
    /* put error checking here */
    for (i=0; i<NUMROWS; ++i){
      m[i] = malloc(sizeof(double)*ROWLENGTH);
      /* error checking again */
    }
    

    The fun part about this structure is that you can still access it with the [][] notation, but the row swap operation becomes

    double *temp;
    temp = m[r2];
    m[r2] = m[r1];
    m[r1] = temp;
    

    Ragged arrays have two disadvantages from your point of view (well, three ’cause of the memory management hassle): they require extra storage for the row pointers, and you can’t use inline initialization.

    Row-as-astructure

    C does not support array assignments of the form;

    double r[3], q[3] = { 1, 2, 3 };
    r = q; /* ERROR */
    

    but it does support by-value assignment semantics for structures. Which gives you the implementation that several people have suggested without explaining:

    typedef struct { double r[ROWLENGTH] } row;
    row m[NUMROWS] = { {1, 2, 3}, {4, 5, 6}, {7, 8 9}};
    
    row temp = m[2];
    m[2] = m[1];
    m[1] = temp;
    

    which is slick. It requires a whole row of memory, but if the compiler is any good is probably fast. The big disadvantage is that you can not address individual matrix elements with the [][] syntax anymore. Rather you write m[i].r[j];

    Others

    There are many, many other ways to implement a “matrix” in c, but they are mostly much more complicated and useful only in specialized situations. By the time you need them you’ll be able to answer this questions for yourself in the context of each one.

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For example, given a matrix: 01|02|03|04|05 06|07|08|09|10 11|12|13|14|15 And knowing that the matrix is
Given a matrix of boolean values (example): a b c +-------- X | 1
I have a m x n matrix where each row consists of zeros and
I'm going to use the sample code from http://gettinggeneticsdone.blogspot.com/2009/11/split-apply-and-combine-in-r-using-plyr.html for this example. So, first,
I would like to store and load numpy arrays from binary files. For that
This is in continuation with the question posted here: Finding the center of mass
I am working on a project for a client and going through the initial
I am trying to fit collected data to a polynomial equation and I found
I've been trying to set up some sort of geometry batching for a week
i cannot understand a certain part of the paper published by Donald Johnson about

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.