how to program a random names??

guys pls help me how to program a random names with groupings in C++
i have here a code.. my problem is i dont know how to random the names in once only, and i dont know how to group the names...
heres my code ;

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


using namespace std;

int main(int argc, char *argv[])
{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;
}
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
#include <iostream>
#include <cstdlib> // use C++ standard libraries.
using namespace std;

int main()  // Your not using cmd line args.
{
   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 ; // this won't work you need another array.
   }

   //system("PAUSE");  only if you don't know how to use your IDE settings properly.
   return 0;
}


please use [ code] code goes here [ /code ] tags in future.

The logic is almost right, you need to make some adjustments.
So you want it to be random, but you also want it so names are not repeated.
So, how about when you get a random name from the array you delete that array element?

How do we do this?
Well you could write a function that takes an address of an array(or a pointer), the array size, and the element to remove. This is a lot easier when you know about pointers.

And rather than Random = NameArray[rand()%10];. You could have a varible:
1
2
3
int leftOver = 10;
// decrement after each use.
Random = NameArray[rand()%leftOver];


An easier way to look at this would be to get the last/first element in the array, and keep incrementing:
1
2
3
4
5
Random = NameArray[leftOver];
leftOver--;

string temp[10];
temp[rand()%10] = Random;

and place this in a random place in the random array. You can use a sentinel loop to make sure the random spot you get does not already contain data.
Last edited on
sir thank you for the responced. sir according to my prof, im going to use the two dimentional array, i dont know how to use that, i am a bigener student.. so pls sir help me..
give me a source code that will result a random names and it will group into three groups.

sir pls help me..

for example sir the result of the program is;

madara
tobi
nagato

pain
kakashi
danzou

naruto
bubz
itachi
konan
No, it's your assignment so you WILL do the source code yourself.

http://cplusplus.com/forum/articles/17108/

.
Last edited on
sir im going to program the names of student;
pls tell me how to program that sir..
need badly .:(
#include <iostream>
#include <stdlib.h>


using namespace std;

int leftOver = 10;
// decrement after each use.
Random = NameArray[rand()%leftOver];
Random = NameArray[leftOver];
leftOver--;

string temp[10];
temp[rand()%10] = Random;

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 ;
}


return 0;
}



sir like that sir? pls guide me sir..
I don't see any code tags. --------------------------------> button over there that looks like " <> "

As for all the code I posted, it was simply logic. Your not supposed to copy paste it all in your assignment. Use your head.


Seriously....
1
2
3
4
5
6
int leftOver = 10;
// decrement after each use.
Random = NameArray[rand()%leftOver];
Random = NameArray[leftOver];
leftOver--;


Whats the point in that?

And why are you still using strings?
string NameArray[10]


I'm starting to think you simply copied the original code off the internet or off one of your friends at school. Good luck with your fail.
Last edited on
I see here that you use string. Are you allowed to do that?
Anyway, to be correct you should also #include <string>

Are you sure your teacher said that you need a two dimensional array for this?
I can't see how that would be useful but maybe I'm missing something...
Maybe he said that you need two arrays. Could we see the assignment?

Are you supposed to write the algorithm yourself or can you use the standard library algorithms?
If it's the latter, this could help -> http://cplusplus.com/reference/algorithm/random_shuffle/
If it's the former, you could use a two-level for loop to do random swaps of the elements of the array, like this:

1
2
3
4
5
6
7
for (int i=0; i<10; i++)
{
    for (int j=i; j<10; j++)
    {
        //do your swaps here
    }
}

Regarding the grouping, does it have to be done in your code or just in the program output?
If it's the latter you can just print a newline character after every three names when you print your array.
Else, just create three arrays do some copying. It's not that difficult, really.

Now, something else I want to ask. Why are there both "tobi" and "madara" in your array?
They are the same person, aren't they? And who's "budz"?
Last edited on
sir roshi tnx for guiding me..
that name is my only example of my program sir.. ^^
tnx again..
HAHAYZZZZZZZ i dont know how to program that ..huhuhu
jonjondavid wrote:
i dont know how to program that

What do you not know how to program? I believe you were given more than enough guidance from gcampton and me. I'm quite sure you have understood the logic behind the algorithm by now. If you have trouble implementing it in C++, it simply means that you haven't read/practiced enough, in which case this would be useful -> http://cplusplus.com/doc/tutorial/
Last edited on
tnx sir.. i am a beginer.. can you pls help me?
my problem is how to group a names of the students.. huhu
need help badLy.. that program use a two dimentional arrays..
but i dont know how to use it..
i have no time enough time study about the C++ b'coz i am a working student..
pls help me..
Topic archived. No new replies allowed.