[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Array copying

Posted on 2003-11-09
8
Medium Priority
?
599 Views
Last Modified: 2010-04-01
I am working on a project using different sorting algorithms.  The original unsorted array must be used by each sorting algorithm.  My question is, how is the best way to pass the array into a function but have it return to the original array when the function is done?  Should I just pass a copy of the original?  A code example would be very helpful.  Thanks,

Ant_Dogg
0
Comment
Question by:Ant_Dogg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:monkesdb
ID: 9711787
STL is simplest


#include <algorithm>


MyObj myArray[NUM_ELEMENTS];
// fill myArray with values.

MyObj cpyArray[NUM_ELEMENTS];

copy(myArray, myArray + NUM_ELEMENTS, cpyArray);


then cpyArray is a copy of myArray which you can pass to your sorting algorithm.

MyObj can be any class which has a copy constructor, any of the primitives like int, double, etc. will be fine.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9711874
Where is this being assigned? You should take a look at two other questions here:

    http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20782423.html
    http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20786730.html

Both pertain to sorting a an array containing strings or integers and counting the number of comparisons and timing the results.  Sounds a lot like another student doing the same work. Take a look for discussion of approach and some code; you will want to actually write your own code for reasons of academic honesty.

Hope this helps, -bcl
0
 
LVL 3

Expert Comment

by:freewell
ID: 9711893
Declare your original unsorted input array as a constant parameter of your sorting function, which means the function will not change the input array. Create a temporary working array in your function to manipulate the array, so that the implementaion of your sorting is transperent to outside world.

int MySort(const int Data[],int nElement)
{
//      Data[0] = 1; // This is not allowed.

      int *m_Data = new int[nElement];
      memcpy(m_Data,Data,nElement);

// your algorithms here

      delete [] m_Data;
      return 0;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:freewell
ID: 9712034
I think the memcpy function should allocate the actual size of array

memcpy(m_Data,Data,nElement * sizeof(int));
0
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 9712977
The best way is to use STL function sort.
template<class RandomAccessIterator>
   void sort(
      RandomAccessIterator _First,
      RandomAccessIterator _Last
   );
It doesnot need a copy of original array.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9714563
rendaduiyan-

Using the STL is, of course, the right answer if the goal is solely to have the array end up in sorted order. The original poster mentions that the project is for comparing different sorting algorithms. With STL sort you get a sort with certain performance bounds but with any given STL implementation you get one sort (okay, you get one general sort and one list<>::sort). The question asked was how to present the same test data to multiple sorts. The choices are reading the file multiple times (loading each array from a persistent copy of the data) or read the file once into a "cached" copy of the contents and then copy the in-memory version of the array (or seed a pseudo-random number generator with a know value and generate the "random" contents of the array before each sort but this is really close to reading it from the file).

0
 
LVL 22

Accepted Solution

by:
grg99 earned 240 total points
ID: 9715436
The easiest way is to put the array in a struct.  C can copy structs with the assignment operator, so it's as simple as:

typedef struct foo { int TheArray[1000] }  EasyCopyArrayType;

 
EasyCopyArrayType   Main, Copy;

for( i= .... )   Main.TheArray[i] = rand( 9999 );


Copy = Main;

SortByInsertion( Copy );

Copy = Main;

SortByInsertion( Copy );

... etc...

0
 

Author Comment

by:Ant_Dogg
ID: 9716380
>>typedef struct foo { int TheArray[1000] }  EasyCopyArrayType;

That sounds like it will work for me.  But what is the foo there for if it isn't used when calling the structure? Would this work just the same?

typedef struct {int Array[1000]} EasyArrayCopy;
EasyArrayCopy Main, Copy;

Ant_Dogg
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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