So im doing an assignment for class and im not sure what/how to proceed, im struggling with storing randomly generated numbers in an array and then calling on them later to display how many times the random number has been called them. as you might notice i already have my first array and is used to display 100 random numbers between 0 through 9. been stuck for a while now and am finally at the point where i need some help on understanding where/what to do from here.

just a heads up i am still very new to c++ so forgive my unorganization, i have yet to set up my void function above the int main.

#include <iostream>
using namespace std;


int main()
{
    int randomnumber[100], storenumber[10] = {0,0,0,0,0,0,0,0,0,0};

    for (int i = 0; i < 100; i++ )
        {
            randomnumber[i] = rand()%10;

        }


            system("PAUSE");
    return 0;
}

 void count (int &x, int &n)
{ 
        x = [0];
        storenumber[1] = ;
        storenumber[2] = ;
        storenumber[3] = ;
        storenumber[4] = ;
        storenumber[5] = ;
        storenumber[6] = ;
        storenumber[7] = ;
        storenumber[8] = ;
        storenumber[9] = ;
}

Dani AI

Generated

— good start. The main pieces missing are (1) seeding the RNG so you don't get the same sequence every run, (2) a clear counting step that walks the generated values and increments a counter for each observed digit, and (3) either a prototype above main or a full function definition placed before main. is on the right track suggesting a dedicated counting function; below is a compact, modern C++ pattern that keeps the logic clear and avoids the syntax problems in the original count attempt.

A simple, safe counting function (uses std::vector/std::array and checks bounds):

#include <vector>
#include <array>

void countFrequencies(const std::vector<int>& values, std::array<int,10>& freq)
{
    freq.fill(0);
    for (int v : values)
        if (v >= 0 && v < (int)freq.size())
            ++freq[v];
}

Generate 100 random digits and call the counter (prefer <random> for classwork if allowed; otherwise seed rand):

#include <random>
#include <ctime>

std::mt19937 rng(static_cast<unsigned>(std::time(nullptr)));
std::uniform_int_distribution<int> dist(0, 9);

std::vector<int> values;
values.reserve(100);
for (int i = 0; i < 100; ++i) values.push_back(dist(rng));

std::array<int,10> freq;
countFrequencies(values, freq);

Print results with a simple loop. Troubleshooting tips: avoid system("PAUSE") (Windows-specific); always check index bounds before incrementing a counter; if the digit range may change, use std::unordered_map<int,int> instead of a fixed-size array; and if the instructor requires C-style arrays, pass the array length into the counting function so you don’t read/write out of bounds.

Recommended Answers

All 2 Replies

I think that you need another for loop inside the count function, which will go over the array of random numbers and count each value. You should also use more descriptive names for the arguments to the count function; so x and n should be something more like values and frequencies, or something like that. That would make the signature to your count function look more like:

void count( const int* values, int valueCount, int* frequencies );

Notice that I have included a valueCount variable in the signature, since you're going to want to know how many values are in the values array :)

Thank you for the help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.