# Swapping rows in a 2D array

• 04-17-2009
bassist11
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;                 }         }```
• 04-17-2009
quzah
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.
• 04-17-2009
bassist11
Yeah I just figured it out. Put another for loop above that one for the column. Thanks though!

SOLVED!
• 03-11-2010
xxshankar
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; }```
• 03-11-2010