max and min

the program below generates random numbers, tallys them, adds them, and displays min and max as well, im having one slight problem, if i put for it to only print 2 random numbers, from time to time the maximum will be incorrect, any idea why? thanks in advance.
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
#include<iostream>
#include<cstdlib>
using namespace std;

int main() {


    const int LENGTH = 10;
    int tallyArray[LENGTH] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // all elements 0
    int totalValues;
    int sum = 0;
    int min = 9;
    int max = 0;

    cout << "This program analyzes the c++ rand() function\n\n";
    cout << "Enter the number of random values: ";
    cin >> totalValues;

    srand(time(0));
    for ( int i = 0; i < totalValues; i++) {

        int randNumber = rand() % 10;

        tallyArray[randNumber]++;

        sum = sum + randNumber;

        if (min > randNumber or randNumber == 0) {
            min = randNumber;
        }else if (randNumber > max){
            max = randNumber;
        }

    }

    for (int i = 0; i < LENGTH; i++) {
        cout << i << ':' <<  tallyArray[i] << "\n";

    }
    cout << "summation: " << sum << endl;
    cout << "Maximum: " << max << endl;
    cout << "Minimum: " << min << endl;

    return 0;
}
I think the problem is here:
if (min > randNumber or randNumber == 0) {

First of all, 'or' is represented in c++ by '||'.
Second issue is the "randomNumber==0" part. It is not required and it excludes 0s from max.

Try if (min > randNumber) and tell me if it worked.
it seems to be having a similar problem, for example one of the runs i did , i got one 4 and one 7
and for some reason maxwas set to 0, although min was set correctly at 4
closed account (SECMoG1T)
hi there you are supposed to evaluate both conditions on every iteration.

1
2
3
4
5
6
7
8

 if (min > randNumber ) {
            min = randNumber;
        }

 if (randNumber > max){
            max = randNumber;
        }


try that.
generates random numbers, tallys them, adds them, and displays min and max as well

even with the C-style array you're using you could use some standard library algorithms to accomplish your tasks more efficiently:
(a) std::generate() to generate the random numbers in situ using more robust <random> library facilities
http://en.cppreference.com/w/cpp/algorithm/generate
(b) std::accumulate() to sum the elements of the array
http://en.cppreference.com/w/cpp/algorithm/accumulate
(c) std::minmax_element() to return a pair of forward iterators to a min and a max element of the array
http://en.cppreference.com/w/cpp/algorithm/minmax_element (requires C++11)
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
#include <iostream>
#include <algorithm>
#include <random>
#include <chrono>

constexpr auto SIZE = 10;
constexpr auto low_bound = 0;
constexpr auto up_bound = 10;

int main()

{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//engine
    std::uniform_int_distribution<int> di(low_bound,up_bound);//distribution

    int data[SIZE];
    std::generate(data, data + SIZE, [&]{ return di(dre);});

    std::cout << "Original array: \n";
    for (auto i = 0; i < SIZE; ++i)std::cout << data[i] << " ";

    auto sum = std::accumulate(data, data + SIZE, 0);
    std::cout << "\nSum: " << sum ;
    std::cout << "\nAverage: " << static_cast<double>(sum)/SIZE << "\n";

    auto minMax = std::minmax_element(data, data + SIZE);
    std::cout << "Minimum element: " << *(minMax.first) << "\n";
    std::cout << "Maximum element: " << *(minMax.second) << "\n";
}
gunnerfunner, unfortunately we have not yet covered the use of std:: cout and many of the other things that you mentioned even though it would make everything so much easier, we are not allowed to use them :( , Although i was able to fix it by evaluating both conditions on every iteration like Iwave mentioned above! :D it seems to be giving me the correct min and max every single time now , thanks for your input guys :D
Topic archived. No new replies allowed.