简体   繁体   English

用 0 到 10 的随机数填充 n 的数组

[英]fill an array of n with random numbers from 0 to 10

i need to write a program that will load a natural number n (n<20).我需要编写一个程序来加载自然数 n (n<20)。 It will then fill an array of n with random numbers from 0 to 10.然后它将用 0 到 10 之间的随机数填充一个 n 数组。

It will write out the elements of the array on the screen.它将在屏幕上写出数组的元素。

Next, it will load a natural number x.接下来,它将加载一个自然数 x。

It will check and write out on the screen how many times the number x occurs in the array.它将检查并在屏幕上写出数字 x 在数组中出现的次数。

int n;
int array[n]={0};
int values[10];

cin >> n;

for(int i = 0; i < n; i++) {
    values[i] = i;
int left = 10;
for(int i = 0; i < 10; i++)
{
    int n = rand() % left;
    array[i] = values[n];
    left--;
    values[n] = values[left]; 
    cout << array[i] << endl;
}

}
```

Your code doesn't look like what I'd expect from current C++. This is not unsurprising since most code you find online, in books or C++ courses mostly show code with raw loops and "C" style arrays.您的代码看起来不像我对当前 C++ 的期望。这并不奇怪,因为您在网上、书籍或 C++ 课程中找到的大多数代码大多显示带有原始循环和“C”样式的代码 arrays。

In any case array[n] is NOT standard C++ (in standard C++ n should be a constant), standard C++ doesn't know about variable length arrays. For that there is std::vector.在任何情况下, array[n]都不是标准 C++(在标准 C++ 中,n 应该是一个常量),标准 C++ 不知道可变长度 arrays。为此,有 std::vector。

Another thing to learn early is never to trust your users input, and always check it.另一件要尽早学习的事情是永远不要相信用户的输入,并始终检查它。

For most things that are needed frequently the standard library header <algorithm> has someting for you like counting.对于大多数经常需要的东西,标准库 header <algorithm>有一些适合你的东西,比如计数。

And instead of index based for loops, have a look at range based for.而不是基于索引的 for 循环,看看基于范围的 for。 Or use algorithms to visit each element in your collection.或者使用算法来访问您集合中的每个元素。

#include <algorithm>
#include <random>
#include <vector>
#include <iostream>
#include <format>

int main()
{
    std::mt19937 generator(std::random_device{}());
    std::uniform_int_distribution<int> distribution(1, 10);

    std::size_t number_of_numbers;

    do
    {
        std::cout << "Enter a number between 1-20 : ";
        std::cin >> number_of_numbers;
    } while ((number_of_numbers < 1) || (number_of_numbers > 20));

    std::vector<int> values(number_of_numbers);
    std::generate(values.begin(), values.end(), [&]
    {
        return distribution(generator);
    });

    for (const auto value : values)
    {
        std::cout << value << " ";
    }
    std::cout << "\n";

    int expected_value;

    do
    {
        std::cout << "Enter a number between 1-10 to count : ";
        std::cin >> expected_value;
    } while ((expected_value < 1) || (expected_value > 10));

    auto count = std::count(values.begin(), values.end(), expected_value);
    std::cout << std::format("That number occurs {} times\n", count);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM