Random Sequence Generator & Frequency Using Arrays - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Random Sequence Generator & Frequency Using Arrays

Hi all, I have one last problem for my C++ class and I can't figure this one out, please help in any way you can!!!

I need to return a random number from array n= {19, 50, 78, 161, 252}, loop it 1000 times and print the frequency of the 5 numbers obtained. Heres what I have so far.

const int n=5, ntimes=1000;
int freq[n]={0};
short int rndSeq[]={19,50,78,161,252};

printRand (freq[], rndSeq[], ntimes);

//main for loop to create random sequence
//this is what I can't seem to set up correctly!

//results

cout<<rndSeq[i]<<"occurred "<<
freq[i]<<" times."<<endl;


Sample output

19 occured 2055 times
50 occured 1986 times
78 occured 1962 times
161 occured 2079 times
252 occured 1919 times



I wish I could provide more code, but nothing I create makes an sense in the for loops that I am doing.

Please help!

Thanks!



Comments

  • zahirdhadazahirdhada Posts: 1Member
    You can use the following code (Using rand() func):
    --------------------------------------------------
    --------------------------------------------------


    :
    : const int n=5, ntimes=1000;
    : int freq[n]={0};
    : short int rndSeq[]={19,50,78,161,252};
    : int i,k;
    :
    : printRand (freq[], rndSeq[], ntimes);
    :
    : randomize();
    :
    : for(i=0;i<ntimes;i++)
    : {
    : k=rand()%n;
    : freq[k]++;
    : }
    :
    : //results
    : for(i=0;i<n;i++)
    : {
    : cout<<rndSeq[i]<<"occurred "<<
    : freq[i]<<" times."<<endl;
    : }
    :
    :
    : Sample output
    :
    : 19 occured 2055 times
    : 50 occured 1986 times
    : 78 occured 1962 times
    : 161 occured 2079 times
    : 252 occured 1919 times
    :
    :
    :


  • vibin_mdcpvibin_mdcp Posts: 60Member
    : Hi all, I have one last problem for my C++ class and I can't figure
    : this one out, please help in any way you can!!!
    :
    : I need to return a random number from array n= {19, 50, 78, 161,
    : 252}, loop it 1000 times and print the frequency of the 5 numbers
    : obtained. Heres what I have so far.
    :
    : const int n=5, ntimes=1000;
    : int freq[n]={0};
    : short int rndSeq[]={19,50,78,161,252};
    :
    : printRand (freq[], rndSeq[], ntimes);
    :
    : //main for loop to create random sequence
    : //this is what I can't seem to set up correctly!
    :
    : //results
    :
    : cout<<rndSeq[i]<<"occurred "<<
    : freq[i]<<" times."<<endl;
    :
    :
    : Sample output
    :
    : 19 occured 2055 times
    : 50 occured 1986 times
    : 78 occured 1962 times
    : 161 occured 2079 times
    : 252 occured 1919 times
    :
    :
    :
    : I wish I could provide more code, but nothing I create makes an
    : sense in the for loops that I am doing.
    :
    : Please help!
    :
    : Thanks!
    :
    :

    #include <stdlib.h>
    #include

    using namespace std;

    int main(){

    //Creating your array and a variable to store random number
    int rndSeq[]={19,50,78,161,252},rnd;

    //Creating array for counting purpose
    int Count[5]={0},i;

    srand(1); //initializes the random number generator.

    //Loop that repeat 1000 times.
    for(i=0;i<1000;i++){
    /*ran() returns a random number, you can use it to access
    elements of the your array. For example like 'rndSeq[rnd]'*/

    rnd=rand()%5;
    Count[rnd]++;
    }
    for(i=0;i<5;i++){
    cout<<rndSeq[i]<<" occured "<<Count[i]<<" times."<<endl;
    }
    system("PAUSE");
    return 0;
    }
Sign In or Register to comment.