Hello again. Sorry this problem is stressing you out.
As I see the problem, there is no need to do anything with the array Name[10][50] after initializing it as you have done with:
char Name[10][50] = {"budz" , "pain" , "konan","nagato", "itachi", "tobi", "madara", "naruto", "danzou", "kakashi"};
There is no need to move the names around. Just print them out in random order.
The following is an outline for a method I have used before.
Declare an array of 10 integers and initialize the values in straight order:
|
int randomIndex[10] = {0,1,2,3,4,5,6,7,8,9};
|
Then use this for loop to rearrange the elements randomly
1 2 3 4 5 6
|
srand( (unsigned)time(0) );
for(int j=9; j>1; j--)
{
int r = rand()%j;// produces a random # between 0 and j-1 inclusive.
// TODO: Swap elements j and r. Please work this part out
}
|
What that does is swap the last element with one randomly picked from before it.
That last element is now a random value. Shrink the length of the array being considered by one (which is done by j--). Now swap the next to last element with one at random from before it.
Repeat until you are down to the last 2 elements. The last elements position is forced.
You can use this to display the elements of randomIndex to verify that the element swap looks good:
1 2
|
for(int j=0; j<10; j++)
cout << randomIndex[j];
|
Now, when you print out the names, use the values in the randomIndex array as the indexes to the names. ie:
Name[ randomIndex[j] ]
and the names should appear in random order.
You will need to tinker around with the output to get it looking as desired.
I hope that's enough of a hint to work off of. Good luck in your studies.