guide me about my program..

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...
according to my teacher im going to use the two dimentional arrays the problem is i dont know how to use the 2D arrays..
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;
}
Last edited on
pls guide me guys,how to program that will result a random names and it will group into three groups.

this is the sample output of the program

for example the result of the program is;

madara
tobi
nagato

pain
kakashi
danzou

naruto
bubz
itachi
konan
closed account (D80DSL3A)
If you are using strings you will need to include this:
#include <string> EDIT: corrected from #include <string.h> which is wrong.
But...you mention that your teacher wants you to use 2D arrays.
Would that be a 2D array of characters, not a 1D array of strings?

ie.char Names[10][50] = {"budz", "pain",...};
instead of string NameArray[10] = {"budz" , "pain"...} ?

Either way you go though, there are logical problems with your for loop:
1
2
3
4
5
6
7
for (int j =0; j<=9; j++)
{
string Random;
Random = NameArray[rand()%10];
cout << Random << endl;
NameArray[j] = Random ;
}


One problem: Suppose on the 1st iteration (j=0) that rand()%10 returns 3.
Then this line (if the code is valid) NameArray[j] = Random ; would overwrite NameArray[0] with NameArray[3] before NameArray[0] was ever printed, and so it would be lost. Is it your intent here to prevent printing out the same name twice? That would be a 2nd problem since rand()%10 may return the same value 2 or more times and some values not even once. This problem may be the "core" issue of the exercise.
Last edited on
sir tnx for the reply.. sir im beginer students about C++ programing.. i dont know how use the 2D arrays.. can u pls check may program what is the error?
sir can pls tell me how to program it correctly.. pls sir.. need help badly..
sir heres my code..

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

using namespace std;
int main(int argc, char *argv[])
{char Name[10][50] = {"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]return 3;
cout << Random << endl;
NameArray[j] = Random ;
}

system("PAUSE");
return 0;
}
closed account (D80DSL3A)
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.
hello tnx sir for the reply..
sir can you pls give me a sample source code using the two dimentional arrays..? and the output of the program is the random of names and the names will be group into three groups.. please sir.. so that i have a guide how to use the 2D arrays. pls sir give me a sample program.. pls sir..
Topic archived. No new replies allowed.