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
Solved

Constructing combinations of elements of arrays.

Posted on 2000-05-15
9
205 Views
Last Modified: 2010-04-02
Suppose you have a list of DISTINCT intergers. Store this list of intergers in an array of length LENGTH.

int array[LENGTH] = [0,1,6,34,43,55,...]
const int num;

Suppose num < LENGTH.

int array2[num];

I want to fill array2 with elements of array.  That is, I want to pick num DISTINCT elements of array and put them in array2.  array2 cannot be filled with the same number.
0
Comment
Question by:mm162
  • 4
  • 3
  • 2
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2811759
it is unethical for us to do your schoolwork for you.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2812236
Agreed.

And, erm, is there anything special about how the elements should be picked. Otherwise, just copy them...
0
 

Author Comment

by:mm162
ID: 2812255
Adjusted points from 50 to 100
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:mm162
ID: 2812256
I am not a computer science student.  This is NOT for a course.  I am an economics PhD student trying to learn how to use C+ to do evolutionary game theory for a doctoral thesis.

The question above is not trivial to the novice.  The statement of the question might not have been clear.  I want to RANDOMLY pick num distinct elements from a list of intergers of size length.

As the old proverb goes: if you have nothing useful to say, don't say anything.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2812457
You must understand that due to ethical constraints and the rules of EE we have to be sure before answering a question like this.  (It clearly is not the question of an experienced program so it is very reasonable to assume it is an assignment.)

Its relatively easy to randomly pull items from one array and place them in another.  The hard part is making sure that you don't pull the same item twice.  To accomplish this, you will effectively shrink the array each time an item is pulled from it.  To do this when you pull an item from the array you will sawp the item witht eh item at the "effective" end of the array and then shrink the "effective" size of the array by 1 so the item is no longer in the effective array.

code sample follows.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2812489
const int SrcAryLen = 100;
const int DstAryLen = 50;

int SrcAry[SrcAryLen] = {0,1,6,34,....}; // Array to pull from.
int DstAry[DstAryLen]; // Array to fill.

int EffSrcAryLen = SrcAryLen; // length of the source array that can be used.

for (int i = 0; i < DstAryLen; ++i)
{
   int SrcPos = rand() % EffSrcAryLen; // Position to pull from.
   
   DstAry[i] = SrcAry[SrcPos]; // Pull a value.
   SrcAry[SrcPos] = SrcAry[EffSrcAryLen]; // Put effective last item in position  pulled from.

   // this step can be skipped if the source array will not
   // be reused.  
   SrcAry[EffSrcAryLen] = DstAry[i]; // Put item pulled from the array
                // in the effective last position.
   
   --EffSrcAryLen; // Make the source array effectively smaller.
}

let me know if you have any questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2812494
Is anybody else having trouble with EE?  i.e is the problem on my end or their end?
0
 

Author Comment

by:mm162
ID: 2812963
Still learning netiquette.  Sorry about the vague question.  Thanks for the suggestion.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2814775
Not sure where the problem is, but my browser seems to go to sleep sometimes after on EE. Retrying until there is reponse seems to work.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

860 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