473,396 Members | 1,779 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.

2d array swap

hi all, I'm trying to swap two array location within a 2d array using the swap function. I know that how to swap with in a 1d array eg " swap(array, inner, inner+1); " but how do I use swap within a 2da array?

e.g

1 2 3
4 5 6

I want to swap the columns only.

1 2 6
4 5 3

can someone please help me with this problem
thanks
Apr 23 '07 #1
8 10987
sicarie
4,677 Expert Mod 4TB
What have you tried on this? (where are you getting stuck?)
Apr 23 '07 #2
AdrianH
1,251 Expert 1GB
I am not familiar with the swap that you are referring to. I do not see it in the standard algorithms.

One can do swapping arrays using recursion. I.e. write a function that takes in two 2d arrays, then in a loop swap each element individually.

pseudo code:

Expand|Select|Wrap|Line Numbers
  1. template <int SIZE>
  2. function swap2d (int & array1[SIZE], int & array2[SIZE])
  3.   for i = 0 to SIZE-1
  4.     swap1d(array1[i], array2[i])
  5.   end for
  6. end function
  7.  
  8. template <int SIZE>
  9. function swap1d (int & array1[SIZE], int & array2[SIZE])
  10.   for i = 0 to SIZE-1
  11.     swap(array1[i], array2[i])
  12.   end for
  13. end function
  14.  
Note, if you are doing fairly large array swapping, you may want to reconsider using the older style of arrays that are an array of pointers to an array of elements, and then just swap the pointers. It is far more efficient.


Adrian
Apr 23 '07 #3
Ganon11
3,652 Expert 2GB
I think the OP is talking about a swap function to swap two integer values within an array:
Expand|Select|Wrap|Line Numbers
  1. int *myArray = {1, 2, 3, 4, 5, 6};
  2. swap(myArray, 0, 3);
  3. // myArray is now {4, 2, 3, 1, 5, 6};
Apr 23 '07 #4
What have you tried on this? (where are you getting stuck?)
Expand|Select|Wrap|Line Numbers
  1.  if (dir == 'v') // v is the command for column move
  2.     {
  3.       for (v=0; v<COLS-1; v--)  // step through the array
  4.  
  5.       {
  6.  
  7.        temp = puzzle[v][rowcol];
  8.  
  9.  
  10.        puzzle[v][rowcol] = puzzle[v][rowcol-1];
  11.        puzzle[v][rowcol-1] = temp;
  12.  
  13.      }
  14.  
  15.      printPuzzle(puzzle); // print result
  16.   }
  17.  
  18. }
Apr 24 '07 #5
Savage
1,764 Expert 1GB
One question:

why is v-- in for loop?


I fear that this loop will never end.

If u have need of v-- than v is aint gona start from zero it will start from COL-1 and loop will execute while v>=0.

Savage
Apr 24 '07 #6
it works now thanks for the help.
Apr 26 '07 #7
AdrianH
1,251 Expert 1GB
I think the OP is talking about a swap function to swap two integer values within an array:
Expand|Select|Wrap|Line Numbers
  1. int *myArray = {1, 2, 3, 4, 5, 6};
  2. swap(myArray, 0, 3);
  3. // myArray is now {4, 2, 3, 1, 5, 6};
I couldn't find any reference to swap with that signature.


Adrian
Apr 26 '07 #8
sicarie
4,677 Expert Mod 4TB
I couldn't find any reference to swap with that signature.


Adrian
I think it's a user-defined function. I was only able to find one that was applicable to strings, not ints.
Apr 26 '07 #9

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

Similar topics

11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
1
by: Puddle | last post by:
Hi everyone, I'd just like to preface by saying any input or direction in this would be very much appreciated; i've been working on this endlessly and can't figure out what's going wrong. I'm...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.