In: Computer Science
I need to be able to store 5000 names into an array. I currently have a code that generates one random name. I just need to generate an array of 5000 the same way.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
string RandomFirstGen(int namelength);
int main(){
srand(time(NULL));
int namelength = rand()%(16-8+1)+8;
cout<<"name is:
"<<RandomFirstGen(namelength)<<endl;
return 0;
}
string RandomFirstGen(int namelength){
for (int i=0; i<=5000 ; i++){
const int MAX = 26;
char alphabet[MAX] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z'};
string name = "";
for (int i = 0; i < namelength; i++)
name = name + alphabet[rand() % MAX];
return name;
}
}
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
string RandomFirstGen(int namelength);
int main(){
srand(time(NULL));
int namelength = rand()%(16-8+1)+8;
//created array with 5000 to store names
string arr[5000];
//iterating 5000 times to generate names
for(int i=0;i<5000;i++){
namelength = rand()%(16-8+1)+8;
//getting the name and putting in the array
arr[i]=RandomFirstGen(namelength);
}
//printing the 5000 names
for(int i=0;i<5000;i++)
cout<<"name "<<i+1<<" is: "<<RandomFirstGen(namelength)<<endl;
return 0;
}
string RandomFirstGen(int namelength){
for (int i=0; i<=5000 ; i++){
const int MAX = 26;
char alphabet[MAX] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z'};
string name = "";
for (int i = 0; i < namelength; i++)
name = name + alphabet[rand() % MAX];
return name;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot