# Thread: Swapping rows in a 2D array

1. ## Swapping rows in a 2D array

So let's say I have a 3x4 array filled with these values:

15 7.5 1 15
12 6 1 12
23 11.5 1 23

Now I want to swap rows so that the row with the largest value in the first column is on top and next largest value is in the second row
So I'd end up with this

23 11.5 1 23
15 7.5 1 15
12 6 1 12

Question is.. Is there a way to deal with an entire row of a 2D array at once? Or do I have to sort them one colum at a time?

Here's sort of what I have going now. It swaps the values in the first column fine but leaves the rest of the columns alone. I can do the same thing for the rest of the colums, but I was just wondering if there was an easier way to do it..

Code:
for( i = 0; i < 3; i++)
{
//Set initial value as the current max
index_of_max = i;
max_value = h[i][0];

for( j = i + 1; j < 3; j++)
{
if( h[j][0] > max_value)
{
max_value = h[j][0];
index_of_max = j;
}
}

//Swap h[i][0] and max value
if( index_of_max != i)
{
tmp = h[i][0];
h[i][0] = h[index_of_max][0];
h[index_of_max][0] = tmp;
}
}

2. You always have to copy one element at a time, one way or another. Even functions like memcpy just loop through however long they're copying. You can hide it by pawning it off to another function, but you're still going to end up copying one element at a time.

Quzah.

3. Yeah I just figured it out. Put another for loop above that one for the column. Thanks though!

SOLVED!

4. ## 2D Array swap and sum

I need help with this problem

Write a program with a function called ManipulateArray. This function should accept a 2 dimensional array of size 5 by 5 and perform the following

• Initialize this array with values upon declaration
• Reverse the first column
• print the array on the screen
• Swap the contents of column 3 and column 4
• print the array on the screen
• Sort row 5 in descending order
• Sum the contents of the entire array
Your program should define a function call print, to print the array each time it is required.

this is what I have :

Code:
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <ctype.h>

#define ROW 5
#define COL 5

int ManipulateArray (int[][COL]);
//void swapArray (int [][]);

int mina()
{
int array [ROW][COL]={ {2,4,6,8,10},{12,14,16,18,20},{22,24,26,28,30},{32,34,36,38,40},{42,44,46,48,50}};

int i,j,sum=0;

//Reverse the first column
//Print the array on the screen

//Swap the contents of column 3 and column 4
//Print the array on the screen

//Sort row 5 in descending order

//Sum the contents of the entire array

return 0;
}

5. Welcome to the forum, xxshankar.

You'll want to put your posts into a new thread, rather than onto someone else's, in the future. It will be much more visible and noticed that way.

By "reverse the first column", do you mean sort it by value in ascending order (biggest to smallest), keeping it with the rest of the row data it's with now? Or is it OK to move the first column's values, independently of the rest of it's row?

int main() { works, but not int mina() {

If you're stuck on something specifically, post back, and give details (either here or in a new thread).

6. Thanks, I started a new post..
Need lots of help on that one