Findin the smallest number and largest number from a list of random numbers generated

Hi all,

I'm new here and this forum has been a great help! Unfortunately, I'm not able to find the answer to my issue here or anywhere else on the web. I was hoping some of you can give me some help or tips on how to go about this.

The program will generate random numbers based on the max limit and the amount of random numbers that will generate.

I'm also required to find the smallest, largest number, as well as the average from all the numbers generated in the loop. The average I can find using the sum/MAX_COUNT_NUM. Unfortunately, I am stuck finding the smallest and largest number. Been at this for the past 6 hours. Please help anyway you can. Thank you.

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
37
38
39
40
41

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(){

int UP_MAX, MAX_COUNT_NUM, RAND_NUM, MIN_COUNT_NUM;


cout << "This program creates random numbers" << "\n" << "\n";

cout << "Enter the upper limit of all generated random numbers: ";
cin >> UP_MAX;
cout << "\n" << "\n";
cout << "Enter the count of random numbers: ";
cin >> MAX_COUNT_NUM;
cout << "\n" << "\n";
cout << "Creating " << MAX_COUNT_NUM << " random numbers from 1 to " << UP_MAX << ": " << "\n" << "\n";

MIN_COUNT_NUM = 1;
int LARGE = 0;
int SMALL = 0;

for (; MAX_COUNT_NUM >= MIN_COUNT_NUM; MIN_COUNT_NUM++)
{
    RAND_NUM = rand() % UP_MAX + 1;
    cout << setw(8) << RAND_NUM;

    if (RAND_NUM < SMALL)
    {
        SMALL = RAND_NUM;
    }
    if (RAND_NUM > LARGE)
    {
        LARGE = RAND_NUM;
    }
}
Place the randomly generated numbers into an array, or I guess vector, since you don't know how many you will have to start with. Then do something like this:

1
2
3
4
5
6
7
int myArray[10];         //Array containing all the randomly generated numbers
int smallestNumber = UP_MAX + 1;   //Holds the smallest number

for(int i = 0; i < 10; i++)
{
      if(myArray[i] < smallestNumber) { smallestNumber = myArray[i] }
}


Or, if you're bold, vectors. I don't know how much about c++ you know, so if this is too far just say so.

1
2
3
4
5
6
7
8
9
std::vector<int> myVector;				//vector containing RNG numbers
myVector.push_back(some integer here);	//How to put items in the vector
int smallestNumber = UP_MAX + 1;		        //the smallest number found

for(std::vector<int>::iterator i = myVector.begin();
	i != myVector.end(); i++)
{
	if((*i) < smallestNumber) { smallestNumber = (*i); }
}


That all looks right, though I'm prone to making stupid mistakes.


WAIT A SECOND, I am prone to stupid mistakes. What you did should have worked, but set SMALL = UP_MAX + 1.
Last edited on
Thanks you for the solution!

Unfortunately, I need to do this without arrays and vectors. In my head, I'm thinking it should work as well but it doesn't. The largest number comes out fine, but the smallest comes out as 0 which makes me scratch my head.

I'm taking a beginners course and this got me stumped, so my way of thinking may be off beat. If there are any other tips you can provide, I definitely appreciate it.
Try
 
int SMALL = 2147483647;


and look for numbers smaller

@Brian2705

You were super close. I think you forgot to initialize your test values and that was causing unexpected output.

I went a set further and initialized your test values to the max int value.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <iomanip>
#include <limits>

// max vaild int values //
const int MAXINTVAL = std::numeric_limits<int>::max();
const int MININTVAL = std::numeric_limits<int>::min();

using namespace std;

int main() {

    int MIN_COUNT_NUM = 0;
    int RAND_NUM = 0;
    int MAX_COUNT_NUM = 0;
    int UP_MAX = 0;


    cout << "This program creates random numbers" << "\n" << "\n";

    cout << "Enter the upper limit of all generated random numbers: ";
    cin >> UP_MAX;
    cout << "\n" << "\n";
    cout << "Enter the count of random numbers: ";
    cin >> MAX_COUNT_NUM;
    cout << "\n" << "\n";
    cout << "Creating " << MAX_COUNT_NUM << " random numbers from 1 to " << UP_MAX << ": " << "\n" << "\n";

    MIN_COUNT_NUM = 1;

    // init these values with opposites to
    // keep the below "test" in the domian of int values on this machine

    // if randnum is larger than the smallest number possible //
    int LARGE = MININTVAL;
    // if randnum is smaller than the largest number possible //
    int SMALL = MAXINTVAL;

    int average_denom = 0;
    double sum_of_input = 0;

    for (; MAX_COUNT_NUM >= MIN_COUNT_NUM; MIN_COUNT_NUM++) {
        RAND_NUM = rand() % UP_MAX + 1;
        cout << setw(8) << RAND_NUM;


        if (RAND_NUM < SMALL) {
            SMALL = RAND_NUM;
        }
        if (RAND_NUM > LARGE) {
            LARGE = RAND_NUM;
        }

        average_denom++;
        sum_of_input+= RAND_NUM;
    }

    // extra new lines //
    cout << endl << endl;

    cout << "Largest number is: " << LARGE << endl;
    cout << "Smallest number is: " << SMALL << endl;
    cout << std::fixed;
    cout << std::setprecision(2);
    cout << "Average number is: " << sum_of_input/average_denom << endl;

    return 0;
}
Last edited on
Thank you so much @Bdanielz! You solved it!

I was completely lost to what I was suppose to do. Thank you so much with the detailed notes as well.

I now see what I did wrong. This will help me understand it for my next assignment as well.

Thanks again!
Topic archived. No new replies allowed.