Solved

swap rows in a two dimensional array

Posted on 2006-07-14
18
3,277 Views
Last Modified: 2012-05-05
Hi,

Can you tell me how to swap rows in a two dimensional array something like:
      int *b;
      int a[3][3]={0,0,0,1,1,1,2,2,2};
      b = a[2];
      a[2] = a[0];
      a[0] = b;

Thanks a lot in advance.
0
Comment
Question by:learn
  • 6
  • 4
  • 3
  • +3
18 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17109359
Do this

int b[3];
int a[3][3]={0,0,0,1,1,1,2,2,2};

memcpy(b, a[2], sizeof(int)*3);
memcpy(a[2], a[0], sizeof(int)*3);
memcpy(a[0], b, sizeof(int)*3);
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17109373
>> int *b;
>> int a[3][3]={0,0,0,1,1,1,2,2,2};
You cannot use a pointer to swap the rows. Since two-dimensional array is allocated consecutively.
You need to explicitly swap the data in it.
0
 

Author Comment

by:learn
ID: 17109710
Thank you.
However, do you think there is a pointer to a row in the two dimensional array?
int *b;
     int a[3][3]={0,0,0,1,1,1,2,2,2};
     b = a[2];    Is a[2] a pointer to the last row of a[][]?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 45

Expert Comment

by:Kdo
ID: 17111053

>Is a[2] a pointer to the last row of a[][]?

Kind of.  a[][] is an array of integers, not pointers.


a[2] is the first element of the last row.


Kent
0
 
LVL 5

Accepted Solution

by:
cryptosid earned 90 total points
ID: 17111996
Hi,

Easier way is to use structures.

struct RowStruct {
int cols[3];
};

struct RowStruct row[3];

struct RowStruct temp;

then to swap rows use this..

temp = row[0];
row[0] = row[2];
row[2] = temp;

this will swap the first and the last rows.

Structures allow us to copy complex data structures with ease, since it does member by member copy.

Regards,
Siddhesh
0
 

Author Comment

by:learn
ID: 17112170
Thanks a lot.

I can do
     int *b;
     int a[3][3]={0,0,0,1,1,1,2,2,2};
     b = a[2];
without any error meanning a[2] is a pointer to the last row? If it is, why we cannot use it to point something else: a[2] = b or a[2] = a[1]?
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17112238
Hi learn,

     int *b;
     int a[3][3]={0,0,0,1,1,1,2,2,2};
     int *row[3] = {&a[0][0],&a[1][0],&a[2][0]};
     b = row[0]; row[0] = row[1]; row[1]=b;

It doesnt actually swap the data, it just moves the goalposts. :-)

Paul
0
 

Author Comment

by:learn
ID: 17112368
OK......at least we swap the rows by pointer in C++ using "new", can we do the similar job using mallocate?
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17112753
Kdo's method will work nicely with malloc.

Paul
0
 

Author Comment

by:learn
ID: 17112841
Paul,

How Kdo's method will work with malloc?
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17113879
if you just want to swap the row then rajeev_devin's method will do.

for the question of using pointers to swap the row, then theres a catch,
if you dynamically allocated the array like this

int* arr[3];

for(int i=0; i < 3; i++)
   arr[i]=new int[3];

//then you can swap row like this
int *temp;

temp=arr[1];
arr[1]=arr[2];
arr[2]=temp;

but if declared like this

int arr[3][3];

you cant swap the rows using pointers. cause the compiler actually stores the elements in a one dimentional form.
so when you just want to access a value at position a[i][j], the compiler calculates the position using this

arr[(i*3)+j]

a generalized formula would be a[(i*columnSize)+j]

but if you want to access it in that way in your code, the compiler will throw a subscript error, but you can try this in the following way

int *a=(int*)arr;
i=0,j=2;
printf("%d", a[(i*3)+j]);


so if you try to just swap the pointers it wont actually change the rows.
hope you understand this, you can ask more if you want.

Nafis
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17115609
Hi learn,

>>How Kdo's method will work with malloc?

typedef struct Row
{
 int cell[3];
} Row;

typedef Row [3] Matrix;

Matrix m = (Matrix*)malloc(sizeof(Matrix));

// Swap row 0 and 1.
Row temp = m[1];
m[1] = m[0];
m[0] = temp;

I havent tested this. You may have to tinker with it but you get the idea.

Paul
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 17115721
Paul,

the structs method is not Kent's method, its mine, the author is interested in Kent's method.

Regards,
Siddhesh
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17115732
Siddesh,

You are correct. Using structs was your way. My apologies. I think asker meant your method as Kent posted about another side of the question, my previous post was misleading. :-(

Paul
0
 

Author Comment

by:learn
ID: 17207761
nafis_devlpr,

Are you talking "new" in C++?
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17210189
>>Are you talking "new" in C++?

I don't understand that.
0
 

Author Comment

by:learn
ID: 17297441
nafis_devlpr,

In your code
   for(int i=0; i < 3; i++)
   arr[i]=new int[3];

I think we cannot use "new" in C:
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 17305037
you can replace

arr[i] = new int[3];

with

arr[i] = (int ) malloc(sizeof(int)*3));

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now