M
M
Maxiiiiiiiim2020-02-17 13:38:02
C++ / C#
Maxiiiiiiiim, 2020-02-17 13:38:02

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 :)


Wrote code:
#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;   //Вывод распаковки
    }
    
}


How and where is it better to prescribe a condition so that gifts are not repeated, neither in different, nor in the same box?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2020-02-17
@vt4a2h

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;

Here, of course, we need to add an interface part and input error handling ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question