hi i am doing a program that requires me to input a random seed (already chosen) and input a maximum of 5 friends to text, randomly choose one and output the chosen name and the names that did not get chosen. i am trying to hard code the sites preset input, but i dont know how to add in the seed generator, so the program could work without hard coding it. please help me!
the first set of inputs is:
21(seed)
adam
bill
cat
damion
edward.
//and cat would be the chosen friend.
first one is my attempt to hard code the site, second is without the seed generator.
Wait, I don't understand. You tell the user "Enter seed" but then store the input in a variable that limits the number of friends that can be entered. Is the for condition wrong, or are you missing an input variable?
this is the question: Write a program that randomly determines whom the user should send a text message to. Allow the user to enter five friends that will be used for the selection.. . .
this is the givin input that my program will be tested with: Program Input
Standard Input
21
Adam
Bill
Cat
Damion
Edward
Program Output
== Who Should I Text? ==
Enter seed
Enter friend 0
Enter friend 1
Enter friend 2
Enter friend 3
Enter friend 4
You should text: Cat
These other friends didn't make the cut:
Adam
Bill
Damion
Edward
My assumption is that you would like to manually control the seed of rand().
So when you prompt for the seed and a value of 21 is entered, it would produce Cat as the chosen seed.
Based on the shorten code:
Declare an integer named seed.
Move srand below your prompt for the seed. And modify it to be: srand(seed);
Then clean up your remaining code; you should be okay.
ok this is what i have come up with. this code allows me to input friends but it gets a runtime error after the 5th name inputted. what can i do so it can work properly?
ok it worked, but it scrambles up the last two names, or all the names, in the outputted names. why would that happened, only 1 of the 3 test cases is correct...
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
string get_random_name(string arr[], int& nums)
{
int num = rand() % nums;
string name = arr[num];
arr[num] = arr[nums - 1];
--nums;
return name;
}
int main()
{
//srand(time(NULL));//Commented out for other intended uses below
string num_names[6];
int num_of_names=5;//Modified num_of_names to be 5
int seed = 0;//Added seed integer
//int N = 6;//Commented out useless/unused variable
cout << "== Who Should I Text? ==" << endl;
cout << "Enter seed" << endl;
cin >> seed;//Modified data entry to reflect actual seed
for (int counter = 0; counter < num_of_names; counter++)
{
cout << "Enter friend " << counter << endl;
cin >> num_names[counter];
}
srand(seed);//call the srand function with seed instead of time, and after seed was declared.
cout << "You should text: " << get_random_name(num_names, num_of_names) << endl;
cout << "These other friends didn't make the cut:" << endl;
for (int i = 0; i < num_of_names; ++i)
{
cout << num_names[i] << endl;
}
return 0;
}
If the names that did not get pick are incorrect, then maybe we're approaching the rebuilding of the non selected names incorrectly.
Possibly something in the function (get_random_name).
arr[num] = arr[nums - 1];
The above code swaps:
-The person at the index of num(our random) with the person at the index of nums-1(your maximum number of people minus one: the last person)
Maybe instead of swapping the missing with the last, we should implement a design to shuffle all of the remaining folks down a notch?
Do you need help with that too? or do you've got it?