Answer the question
In order to leave comments, you need to log in
How to make a condition for the program?
Solved the problem:
On Gosha's birthday, Sasha came up with a very funny prank:
he offered his friend three gift boxes: blue,
white and black. The day before his birthday, Sasha got ready and
put a random item from the list into each box: a
spider, a sock, an iPhone 11 Pro, a C++ book, a soccer ball, a flash drive,
a movie ticket, a wristwatch, kefir. Sasha congratulated Gosha
and invited him to choose a gift box on his own.
Write a program that puts a
random item from an array of gifts into each box (make sure there are no
duplicate items!) and prompt Gaucher to choose a gift.
Display the result of unpacking on the screen :)
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
int d = 0 + rand() % 10; //Рандомное число
string korbka1[4]; //Инициализирую коробки
string korbka2[4]; //Инициализирую коробки
string korbka3[4]; //Инициализирую коробки
string podarki[9] = { "паук," "носок", "iPhone 11 Pro", "книга по С++", "футбольный мяч", "флешка", "билет в кино", "наручные часы", "кефир"}; //Массив с возможными подарками
for (int i = 0; i < 4; i++) {
korbka1[i] = podarki[d]; //Рандомизирую подарки в коробках
d = 0 + rand() % 10; //Рандомизирую подарки в коробках
korbka2[i] = podarki[d]; //Рандомизирую подарки в коробках
d = 0 + rand() % 10; //Рандомизирую подарки в коробках
korbka3[i] = podarki[d]; //Рандомизирую подарки в коробках
d = 0 + rand() % 10; //Рандомизирую подарки в коробках
}
cout << endl << endl << "1 коробка" << endl; //Вывод распоковки
for (int i = 0; i < 4; i++) { //Вывод распоковки
cout << korbka1[i] << endl; //Вывод распоковки
}
cout << endl << endl << "2 коробка" << endl; //Вывод распоковки
for (int i = 0; i < 4; i++) { //Вывод распоковки
cout << korbka2[i] << endl; //Вывод распоковки
}
cout << endl << endl << "3 коробка" << endl; //Вывод распоковки
for (int i = 0; i < 4; i++) { //Вывод распаковки
cout << korbka3[i] << endl; //Вывод распаковки
}
}
Answer the question
In order to leave comments, you need to log in
From the condition, I see that there can only be one item from the list in the box. Therefore, you need to select 4 pseudo-random elements from the sequence. This can be done in many ways. For example, you can just randomly shuffle an array of all available gifts and ask the user to select an index (box number) from 0 to 3. Well, display the name of the gift under the specified number. Something like this:
std::vector<std::string> presents {"паук", "носок", "iPhone 11 Pro", "книга по С++", "футбольный мяч", "флешка", "билет в кино", "наручные часы", "кефир"};
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(presents.begin(), presents.end(), g);
std::size_t boxNumber = 0;
std::cin >> boxNumber;
std::cout << presents[boxNumber] << std::endl;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question