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

Edited 3 Years Ago by happygeek: fixed formatting

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.

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.

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

This article has been dead for over six months. Start a new discussion instead.