C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28630
Number of posts: 94612

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Random Sequence Generator & Frequency Using Arrays Posted by breaktheground on 20 Dec 2008 at 5:10 PM
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!



Report
Re: Random Sequence Generator & Frequency Using Arrays Posted by zahirdhada on 21 Dec 2008 at 1:53 AM
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
:
:
:


Report
Re: Random Sequence Generator & Frequency Using Arrays Posted by vibin_mdcp on 23 Dec 2008 at 11:00 PM
: 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 <iostream>

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;
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.