Finding the min and max
Mar 2, 2013 at 11:31pm UTC
Hello all. I wrote a program that gives me 100 random values from the range between -100 to +100. How am I supposed to find the minimum, maximum, and average of the values that will be listed when I run the program? I appreciate the help anyone can give me. Also, this is for a class and we are not allowed to use Arrays. Thanks again.
Here is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "stdafx.h"
#include <windows.h>
#include <iostream>
//#include <conio.h>
#include <ctime>
#include <cstdlib>
using namespace std;
float main() {
{
srand((unsigned )time(0));
float random_integer;
float lowest=-100.00, highest=100.00;
float range=(highest-lowest)+1;
for (float index=0; index<100; index++) {
random_integer = lowest+float (range*rand()/(RAND_MAX + 1.0));
cout << random_integer << endl; }
float sum;
float average;
float minimum, maximum;
minimum= (not sure what to put here...);
cout << ("Minimum:" )<<minimum << endl;
//_getch();
system("pause" );
}
}
Mar 3, 2013 at 1:15am UTC
If you can't use arrays then you just have to implement checks for the lowest/highest etc in the loop.
Here's a short example of the lowest. You can make the necessary changes to adapt it to your program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <ctime>
int main( int argc, char * argv[] )
{
// Seed PRNG
srand( time( NULL ) );
// Lowest number data
int lowest = 100, lowestIdx = -1;
int n;
// Number of iterations
const int MAX_NUMS = 100;
for ( int i=0; i < MAX_NUMS; i++ )
{
n = rand()%100 + 1;
if ( n < lowest )
{
lowest = n;
lowestIdx = i;
}
}
std::cout << "The lowest number was " << lowest\
<< " found at iteration " << lowestIdx << std::endl;
}
Finding the highest would be very similar. Finding the average would be keeping a running sum of the numbers then dividing them by 100 post loop.
Last edited on Mar 3, 2013 at 1:17am UTC
Topic archived. No new replies allowed.