简体   繁体   中英

C++ Check how many times there are numbers in the resulting array

I'am beginner in C++ Somebody could help me with it. INT array.Random. fill an array of 1000 integers with random numbers from 10 to 77. Check how many times in the resulting array there are numbers 10, 20, 30, 40, 50, 60, 70. Print the received statistics.

I see it like this but i dont know how make count of numbers in array.

int main()
{
    int number[1000];
    int countUnits[7];
    int count = 0;
    srand(time(NULL));
    for (int i = 0; i < 1000; i++) {
        number[i] = rand() % 68 + 10;

    }
    for (int i = 0; i < 1000; i++) {
        if (number[i] % 10 == 0) {

        }
    }
}

Start the same way as if you were counting only one number, except you have seven checks and seven counters:

for (int i = 0; i < 1000; i++)
{
    if (number[i] == 10)
        countUnits[0] += 1;
    else if (number[i] == 20)
        countUnits[1] += 1;
    else if ...
...
}

Then you notice that there is a pattern to the testing and indexing, and simplify:

for (int i = 0; i < 1000; i++)
{
    if (number[i] % 10 == 0)
        countUnits[number[i]/10-1] += 1;
}

Or, if you want to confuse people with your m4d sk1llz:

for (int i = 0; i < 1000; i++)
{
    countUnits[number[i]/10-1] += number[i] % 10 == 0;
}

You are not far. But you should first initialize countUnits :

int countUnits[7] = {0};    // enough to zero initialize the whole array

You can then count:

for (int i = 0; i < 1000; i++) {
    if (number[i] % 10 == 0) {
        countUnits[number[i] / 10 - 1] += 1;
    }
}

That way you scan the array only once.

The index for countUnits must be based on the value of number[i] .

And not i as others have suggested. With that (eg) if index i was 934 and that was evenly divisible by 10, the resultant index would be 92

Here's the code (please forgive the printf ):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(void)
{
    int number[1000];
    int countUnits[7] = { 0 };

    srand(time(NULL));

    for (int i = 0; i < 1000; i++) {
        number[i] = rand() % 68 + 10;
    }

    for (int i = 0; i < 1000; i++) {
        int value = number[i];
        if (value % 10 == 0)
            countUnits[value / 10 - 1] += 1;
    }

    for (int i = 0; i < 7; i++)
        printf("%d: %d\n",(i + 1) * 10,countUnits[i]);

    return 0;
}

Since this is C++, it's supposed to be way, way easier. It will read more like Python than like C. That's a good thing in this case!

Here's how I'd write it:

#include <array>
#include <iostream>
#include <map>
#include <random>

int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> distrib(10, 77);

    std::array<int, 1000> numbers;
    for (auto &number : numbers)
        number = distrib(gen);

    std::map<int, int> counts;
    for (auto const &number : numbers)
        if ((number % 10) == 0)
            counts[number] ++;

    for (auto const &number_count : counts)
    {
        auto number = number_count.first;
        auto count = number_count.second;
        std::cout << number << " found " << count
            << (count == 1 ? " time" : " times") << '\n';
    }
}

Sample output:

10 found 13 times
20 found 15 times
30 found 13 times
40 found 7 times
50 found 15 times
60 found 9 times
70 found 17 times

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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