473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Iterate a 2D matrix in order to exchange columns and rows using pointers

2
Hello! I am trying to iterate a matrix in order to exchange rows and columns element by element. Although the function that exchanges the rows has come out well, i don't seem to figure out how to do the same on columns.The columns switch for a matrix like
1 2 3
1 2 3
1 2 3
if i try to switch column 3 and 2,is:
1 3 2
1 3 2
1 2 3

Any suggestion would be much appreciated. Here are both of the functions:

Expand|Select|Wrap|Line Numbers
  1. void interchange_rows(int *p,int n,int r1,int r2){
  2.     int temp;
  3.     for(int i=0;i<n;i++){
  4.             temp=*(p+r1*n+i);
  5.             *(p+r1*n+i)=*(p+r2*n+i);
  6.             *(p+r2*n+i)=temp;
  7.         }
  8. }
  9.  
  10. void interchange_columns(int *p,int n, int c1,int c2){
  11.     int temp;
  12.     for(int i=0;i<n;i++){
  13.         temp=*(p+(i*n)-c1);
  14.         *(p+(i*n)-c1)=*(p+(i*n)-c2);
  15.         *(p+(i*n)-c2)=temp;
  16.     }
  17. }
  18.  
Jan 19 '14 #1
1 1244
weaknessforcats
9,208 Expert Mod 8TB
This works:
Expand|Select|Wrap|Line Numbers
  1. void interchange_columns(int *p,int n, int c1,int c2){
  2.     int temp;
  3.     for(int i=0;i<n;++i){
  4.          temp = *(p+ c1 + i * 3);
  5.                  *( p + c1 + i * 3) =   * ( p + c2 + i*3);
  6.                  * ( p + c2 + i*3) = temp;
  7.     }
  8. }
However, I made the loop cycle for the number of columns and I had to hard-code the width of the row since it is not an argument. Probably, you need to supply that value as an argument.

The logic is based on incrementing p by a column number, like 0 1 2, incremented by the row width.
Jan 19 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dan V. | last post by:
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select. I read at : microsoft.public.dotnet.framework.adonet "As others have posted: There is no...
2
by: Hadley Willan | last post by:
Hi all, Can it speed things up to index the order by columns? Thanks. Hadley
8
by: Andy Mabbett | last post by:
I have an HTML document which has a long table, with five columns. I want to swap the order of columns four and five, without breaking (indeed, without otherwise altering) the valid markup. Is...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
1
by: ncsthbell | last post by:
Here is the scenario. I have a form-"formA", I created another form-"FormB" which will be placed on FormA . I go to FormA in desgn view and use the button for "create subform" to bring in "FormB"...
0
by: teddy06 | last post by:
I have written code for read mails from exchange server. I have used webdav in my code. Its was working fine once upon a time. Currently configuration settings of client has been changed . so i...
69
by: Horacius ReX | last post by:
Hi, I have the following program structure: main ..... int A=5; int* ptr= A; (so at this point ptr stores address of A) ..... .....
2
by: Claudia Fong | last post by:
Hi, How can I create a grid of 20 Columns by 30 Rows using asp.net? Cheers! Claudi *** Sent via Developersdex http://www.developersdex.com ***
1
by: chelluchittibabu | last post by:
hi, i am working with exhange server 2007 using cdo api's. pls tell me how to retrieve appointments from exchange server 2007 using cdo in c#. regards, chittibabu 9945299471
2
by: gchidam | last post by:
please help me out in matrix multiplication using pointers in C++ and give me brief description about usage of pointers
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.