how can i ramdomize the names into once only.

heres my code..can u help me how to ramdom the names with not duplicating it.? tnx..

#include <iostream>
#include <stdlib.h>
#inlucde <string.h>

using namespace std;

int main()
{string NameArray[10] = {"budz" , "pain" , "konan","nagato", "itachi", "tobi", "madara", "naruto", "danzou", "kakashi"};

cout<< "Before the Name Random\n";

for (int i =0; i <=9; i++)
{
cout << NameArray[i] << endl;
}
cout << endl;
cout <<"After the randomizing\n";

srand(time(0));

for (int j =0; j<=9; j++)
{
string Random;
Random = NameArray[rand()%10];
cout << Random << endl;
NameArray[j] = Random ;
}

system("PAUSE");
return 0;
}
Do you need to keep the array in order? If you don't then you could just loop through the array, each time picking a random name in the array and swapping it with the current name. If you do need to keep the array in order you could just create a new array for the randomized names.

what your for loop is doing now is for each iteration, it assigns a random name to Random, prints it, then writes that random name back into the array, overwriting whatever the current name was.

Assuming you don't need to keep the array in order you could...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int _tmain(int argc, _TCHAR* argv[])
{
	const int MAX = 10;	
	char *names[ MAX ] = { "budz" , "pain" , "konan","nagato", "itachi", "tobi", "madara", "naruto", "danzou", "kakashi" };
	int random;

	srand( time( 0 ) );


	cout << "The names in order: " << endl;

	for( int i = 0; i < MAX; i++ )
	{
		cout << names[ i ] << " ";
	}

	for( int i = 0; i < MAX; i++ )
	{
		random = rand() % 10;
		char *temp = names[ i ];
		names[ i ] = names[ random ];
		names[ random ] = temp;
	}

	cout << "\n\nThose names randomized.." << endl;
	for( int i = 0; i < MAX; i++ )
	{
		cout << names[ i ] << " ";
	}
		

	getch();
	return 0;
}

thanks you sir i solve my problem about that..

sir can i ask a question again??
sir how can i group the names of my program into three group and that is will randomize.. pls help me sir..according to my teacher we will use the two dimentional arrays,
i dont know how to use the 2D arrays.. tnx sir..

this is the sample of the out of the program..

the name in order:

budz
konan
nagato
itachi
tobi
madara
naruto
danzou
kakashi

those name randomize:

itachi
kakashi
madara

danzou
tobi
naruto

konan
budz
nagato



Your teacher probably wants you to use a 2D char array to store the names, not a string array.

So your 2D char array should look something like this.
 
char names[ 10 ][ 50 ] = { "budz" , "pain" , "konan","nagato", "itachi", "tobi", "madara", "naruto", "danzou", "kakashi" };


You can think of this 2D array as a set of 10 char arrays that can hold up to 50 chars, in other words it can hold up to 10 names that are less 50 characters long.

to print out the names from this array in a loop all you need to do is loop through the first subscript ( [ 10 ] )...
1
2
3
4
for( int i = 0; i < 10; i++ )
{
	cout << names[ i ] << " " << endl;
}


If you want to group your random names into threes, all you need is a counter that counts the number of iterations through the loop, when it gets to 3 output and extra "endl" and set the counter back to 0;
1
2
3
4
5
6
7
8
9
10
11
12
int counter = 0;
for( int i = 0; i < 10; i++ )
{
          ++counter;  
          cout << names[ i ] << " " << endl;

          if( counter == 3 )
          {
               cout << endl;
               counter = 0;
          } 
}


sir thank you so much.. i solve my problem.. tnx a lot.. :)))
Topic archived. No new replies allowed.