Swap an array...

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Swap an array...

 
0
  #1
May 16th, 2008
hi guys, it's been a while since i used c for anything so here goes a question tha may seem silly.


I have 2 variable length arrays....

  1.  
  2. int main()
  3. {
  4. int n = 5;
  5. int m = 6;
  6.  
  7. int before[n][m];
  8. int after[n][m];
  9. array_function( 5, 6, before, after );
  10. }
  11.  
  12. void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
  13. {
  14. //for some iterations
  15. for(;;)
  16. {
  17.  
  18. //for all the elements int the array
  19. for (i=1; i<rows; i++)
  20. for(j=1; j<cols; j++)
  21. after[i][j] = 4 * before[i][j]
  22.  
  23. //afterwards prepare for the next iteration [1]
  24. for (i=1; i<rows; i++)
  25. for(j=1; j<cols; j++)
  26. before[i][j]=after[i][j];
  27. }
  28. }

...What i want to do is find a way to swap the pointers to the arrays. So that i can get rid of the last loop.
I figured that since i only need to calculate "after" based on "before" and then make "after"->"before" and recalculate "after".
It would save a lot of time if i just swapped the 2 pointers.

i tried the following but with no luck:

  1. //define function like this:
  2. void array_function(int rows, int cols, float &after[rows][cols] , float &before[rows][cols])
  3.  
  4. //call it like this:
  5. array_function( 5, 6, &before, &after );

thanks in advance
-nicolas
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 518
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Swap an array...

 
0
  #2
May 16th, 2008
Hi
U can try Memory functions like memcopy () .... something like that for quick swapping. I dont much about memory functions. but it may helpful to fast swapping
Selva
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Swap an array...

 
0
  #3
May 16th, 2008
the way i think it, i try to avoid copying anything than 2 pointers... maybe the thread title is a bit misleading

maybe a moderator could change to something like "swap the pointers to 2 arrays" or anything better!
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Swap an array...

 
0
  #4
May 16th, 2008
may this will work:
  1. function swap_array(int **after, int **before) {
  2. int **temp = after;
  3. after = before;
  4. before = temp;
  5.  
  6. }


I have not checked it. Hope it will work.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Swap an array...

 
0
  #5
May 16th, 2008
nop man this won't work.... because i am asking about variable lenght arrays....

check this out::

http://www.ddj.com/cpp/184401444
Last edited by n.aggel; May 16th, 2008 at 12:01 pm.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Swap an array...

 
1
  #6
May 16th, 2008
> i try to avoid copying anything than 2 pointers...
swapping pointers is easy; swap pointers to arrays just like you swap any other pointers.
  1. #include <stdio.h>
  2.  
  3. void swap( int rows, int cols, float (**after)[rows][cols] ,
  4. float (**before)[rows][cols] )
  5. {
  6. float (*temp)[rows][cols] = *after ;
  7. *after = *before ;
  8. *before = temp ;
  9. }
  10.  
  11. int main()
  12. {
  13. int rows = 3, cols = 4 ;
  14. float one[rows][cols] ; one[0][0] = 1.1 ;
  15. float two[rows][cols] ; two[0][0] = 2.2 ;
  16. float (*p1)[rows][cols] = &one ;
  17. float (*p2)[rows][cols] = &two ;
  18. printf( "%f\t%f\n", (*p1)[0][0], (*p2)[0][0] ) ;
  19. swap( rows, cols, &p1, &p2 ) ;
  20. printf( "%f\t%f\n", (*p1)[0][0], (*p2)[0][0] ) ;
  21. }

this means that you will have to write the array_function in terms of pointers to arrays:
  1. void array_function( int rows, int cols, float (*after)[rows][cols],
  2. float (*before)[rows][cols] )
  3. {
  4. //for some iterations
  5. for(;;)
  6. {
  7. int i, j ;
  8. //for all the elements int the array
  9. for (i=1; i<rows; i++)
  10. for(j=1; j<cols; j++)
  11. (*after)[i][j] = 4 * (*before)[i][j] ;
  12.  
  13. //afterwards prepare for the next iteration
  14. swap( rows, cols, &after, &before ) ;
  15. }
  16. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Swap an array...

 
0
  #7
May 16th, 2008
Good grief!

n.aggel, you had the right idea to begin with, only your syntax needs help. Don't change the definition of the function, but just pass the appropriate pointer.

  1. void array_function(int rows, int cols, float after[rows][cols] , float before[rows][cols])
  2. {
  3. ...
  4. }
  5.  
  6. int main()
  7. {
  8. int n = 5;
  9. int m = 6;
  10.  
  11. int before[n][m];
  12. int after[n][m];
  13.  
  14. array_function( 5, 6, before, after ); // this works
  15. array_function( 5, 6, after, before ); // this works
  16.  
  17. ...
  18. }

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Swap an array...

 
0
  #8
May 18th, 2008
thanks vijayan121, this solved my problem.

Duoas, i wanted to swap the arrays inside the function...

-nicolas
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Swap an array...

 
0
  #9
May 18th, 2008
Sorry for the double post, it was an accident.
Last edited by n.aggel; May 18th, 2008 at 9:18 am.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC