944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1717
  • C++ RSS
Jun 13th, 2007
0

Program for Random numbers having a bit of trouble.

Expand Post »
HI,

I have been working on this program for a bit now and having a little bit of trouble, I have got most of it but still a few stumbles.

Here is the Directions:

Write a program that will generate an unknown (between 93 and 139) count of random numbers whose values fall between 147 and 206. Calculate the average to 2 decimal places and standard deviation to 3 decimals. On the monitor display only the count of numbers, the mean, and the standard deviation. Then after you have displayed alll of that information list them again in 5 columns in order from high to low and then have skipped two spaces.

I just chose lower numbers than required for now to see if I can figure this out any easier.

So far I can get everything to work but coming out with strange numbers for standard deviation and average.

std deviation is sqrt of ( 2 2
sum of num (sum of num)
_____________ ___ _____________
2
count count

this is what i have so far:

srand((unsigned int) time (NULL));
int first, second;
int high, low, high2, low2;
int number, number2;
int total, total2, overall;
double average, deviation;
high = 3;
low = 2;
high2 = 10;
low2 = 5;


for (first=0; first<1; first++)
{
number = rand()%(high-low+1)+low;
}

for (second=0; second < number; second++)
{
number2 = rand()%(high2-low2+1)+low2;
total = number2 * number2;
total2 = total2 + number2;
overall = overall + total;
cout << number2 << endl;


}
average = total2 / second;
deviation = sqrt((overall/second) - ((total2 * total2)/(second * second)));

cout << setprecision ( 2 ) << average << endl;
cout << setprecision ( 3 ) << deviation << endl;



Thank you all for any help you can give.

Radskate360
Similar Threads
Reputation Points: 34
Solved Threads: 0
Light Poster
radskate360 is offline Offline
37 posts
since May 2007
Jun 13th, 2007
0

Re: Program for Random numbers having a bit of trouble.

try writing down the s.d on paper for a few numbers.

Then walk through your program and place a few couts to see where it may be going wrong.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 13th, 2007
0

Re: Program for Random numbers having a bit of trouble.

Strange values usually means you didn't initialize a variable somewhere. For example, you don't initialize total2 when it should be set to 0 for an accurate sum.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 20th, 2009
0

Re: Program for Random numbers having a bit of trouble.

hi can anyone tell me how to generate random number within certain limit as in between 10 to 20 etc .please help me
Thanks
anuj
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anujsharma is offline Offline
24 posts
since Apr 2009
May 20th, 2009
0

Re: Program for Random numbers having a bit of trouble.

10..20 or 11..19 or what else?
[0,n) - 0, 1, ... n-1 :
C++ Syntax (Toggle Plain Text)
  1. rand() % n
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: visual c++ 6
Next Thread in C++ Forum Timeline: Guessing Game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC