Hi!

I wrote this program to generate a keyboard determined random numbers.

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

int amount, aleatory_numbers;
int array[70];
int counter=0;

int generator ()
{
printf ("Introduce the amount of numbers that you wish: ");
scanf ("%d", &amount);
srand ( time(NULL) );
do
{
aleatory_numbers = rand() % 49 + 1;
counter++;
array[counter]=aleatory_numbers;
printf ("%d", array[counter]);
}
while (counter!=amount);
system("pause");

return array[counter],counter;
}

int main ()
{
generator();
}

It works, but now I need to display on screen the frequency of each numbers of the array. How can I do that? I've tried lots of things, but I can't do that.

Thanks.

Dani AI

Generated

This thread is asking for a frequency table of the generated integers (1..49). The safest approach is to tally counts into a container indexed by the generated value, then print absolute counts plus percentages. Two immediate fixes before tallying: validate the user’s requested amount against available storage, and avoid the comma-expression return and other undefined/unsafe indexing that pointed out. As suggested, compute percentages after you have the counts.

A concise, modern C++ pattern that is safe and clear:

#include <iostream>
#include <vector>
#include <random>
#include <iomanip>

int main() {
    const int MIN = 1, MAX = 49;
    std::size_t amount = 0;
    std::cin >> amount;

    std::vector<std::size_t> freq(MAX + 1, 0); // index 0 unused

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(MIN, MAX);

    for (std::size_t i = 0; i < amount; ++i) {
        int value = dist(gen);
        freq.at(static_cast<std::size_t>(value)) += 1;
    }

    std::cout << std::fixed << std::setprecision(2);
    for (int v = MIN; v <= MAX; ++v) {
        double pct = amount ? (100.0 * freq.at(v)) / amount : 0.0;
        std::cout << v << ": " << freq.at(v) << " (" << pct << "%)\n";
    }
}

Troubleshooting checklist: initialize count storage to zero; use .at() or explicit bounds checks to avoid overruns; prefer <random> over rand() for better reproducibility and seeding; do not rely on system("pause") (platform-dependent); if the value range is unknown or sparse use std::unordered_map<int,std::size_t> instead of a vector. This gives a safe, readable frequency report with both counts and percentages.

Recommended Answers

All 2 Replies

Well i guess now you could use another variable to count how many times a particular number is repeated in the data set and then could get the percentage of the occurence of that particular number .

You should however post your tries on doing this and we can help you out from there.

Just in passing:

First return array[counter],counter; is an awful way to say return counter .
The code does not return array[counter] in anyway.

Second

If I were to enter 100 as the number of numbers. then your code would break since the array is only 70 big.

Third

I think that you would prefer to enter a huge number of random and then obtain the number of 1s, 2s etc. You do not care about the order (yet) . So try collecting the statisics first. For example if your random number part only generated numbers between 1 and 10.
if you did this

int array[11];       // arrays are 0->10

// set array elements to zero here. 

while(counter<amount)
{
  N = rand() % 10 + 1;
  array[N]++;
  counter++;
}

// now print the array.

you have a nice set of distributions because array[1] is increased each time N becomes 1.

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.