You are here:

C++/rand and array

Advertisement


Question
Hello Titus B. Ledbetter, Jr.,
Can you teach me how to
select one element from an array via random order
then finally all the elements of that array are used(selected) once.
For example,there is an array
{ 2,4,6,8 }
I may need the following selection.
2,6,8,4    or
4,8,6,2     and so on.
How to do this in C?
I also use TCL.
If you can program this in TCL
I will be grateful to this

Answer
Dear alan:

I can think of another way, but it would probably take longer than this one.

Read the number into an array of four numbers.  Pick a random number between 0 and 3 inclusively.  Remove the element with that index and store it as the first element of a separate array. Rearrange the original array so it is an array of three elements that excludes the first number picked.  Then pick a number from 0 to 2 inclusively and follow the same procedure until the second array has four elements and the first array has no more elements.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.