N
N
Nikita M.2021-09-08 00:34:35
C++ / C#
Nikita M., 2021-09-08 00:34:35

How to create an array from strings and fill it with values ​​at random indices?

Goodnight.
How in C++ to create an array of strings (maximum 1000 values) and fill it with some values ​​at random indexes? i.e. working indexes of all 1000 values ​​can be 100 and 500, the remaining values ​​can be null. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Kulakov, 2021-09-08
@unknowndeal

#include <stdlib.h>
#include <string>
#include <ctime>
using namespace std;

// получаем рандомный индекс
int getIndex() {
    srand((unsigned) time(0));
    return ((rand() % 1000));
}

int main() {
    string arr[1000] = { };

    int index = getIndex();
    arr[index] = "value";

    return 0;
}

D
Developer, 2021-09-08
@samodum

The task is elementary or with a catch.
At what stage is the problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question