Spitting random output

I am having an issue with a program I am trying to write, which will take the product of numbers in an array. I'm getting random numbers from the array appended to my output, and can't figure why. Please help.

Link to code: https://jbox.gq/main.txt

I appreciate the help.

Thank you.
The code posted here, we can compile it, and refer to line numbers:

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
71
72
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int nums[20];
int maximum_value, minimum_value;
double product_of_all = 1;

void gen_rand_array_fill() {
    srand(time(NULL));
    for (int i = 0; i < 20; i++) {
        nums[i] = rand() % 101;
        if ((rand() % 2) == 1)
            nums[i] *= -1;
    }

}

void calc_product() {
    for (int i = 0; i < 20; i++) {
        product_of_all *= nums[i];
        cout << product_of_all << endl;
    }
}

int calc_min() {
    minimum_value = 101;
    for (int i = 1; i < 20; i++) {
        if (nums[i] < minimum_value) {
            minimum_value = nums[i];
            cout << nums[i];
        }
    }
    return minimum_value;
}

int main()
{
    int input_or_gen;
    cout << "PROJECT 1\nPART I\nInput numbers or generate randomly?\n\t1) Input numbers\n\t2) Generate randomly\n? ";
    cin >> input_or_gen;
    while ((input_or_gen != 1) && (input_or_gen != 2)) {
        cout << "Invalid input...\nInput numbers or generate randomly?\n\t1) Input numbers\n\t2) Generate randomly\n? ";
        cin >> input_or_gen;
    }
    if (input_or_gen == 1) {
        cout << "Input 20 numbers from -100 to 100";
        for (int i = 0; i < 20; i++) {
            cout << "\nInput number " << (i + 1) << ": ";
            cin >> nums[i];
            while (nums[i] > 100 || nums[i] < -100) {
                cout << "\nInvalid number...\nInput number " << (i + 1) << ": ";
                cin >> nums[i];
            }
        }
    } else if (input_or_gen == 2) {
        gen_rand_array_fill();
    }

    for (int i = 0; i < 20; i++) {
        cout << i << ", " << nums[i] << endl;
    }

    calc_product();
    cout << product_of_all;
    cout << "\n\n\nProduct of numbers: " << endl;
    cout << product_of_all;

    cout << "\n\n\nMinimum value inputted: " << calc_min();
}
Hi it does look weird. It seems the problem is on line 69. Try it in a debugger.

There are some other problems:

Line 30 misses the first value in the array.

The first time I ran it, the first number was zero, so the product was zero.

Try to avoid having global variables.
> I'm getting random numbers from the array appended to my output, and can't figure why.

When we multiply those twenty integers, the result may be too large to be held in an integer.
We have to avoid that (undefined behaviour).

Either check for integer overflow while computing the product,
or to get an approximate result, use a floating point type (double) to hold the product.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

const int MINV = -100 ;
const int MAXV = 100 ;
const int N = 20 ;
using array_type = int[N] ; // 'array_type' is an alias for 'array of N int'

void print( const array_type& array ) { // the array is passed by reference to const

    std::cout << "\n[ " ;

    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : array ) std::cout << v << ' ' ;

    std::cout << "]\n" ;
}

void gen_rand_array_fill( array_type& array ) { // the array is passed by reference

    for( int& v : array ) {

        v = rand() % (MAXV+1);
        if( rand()%2 == 1 ) v = -v ;
    }
}

void calc_and_print_product( const array_type& array ) {

    std::cout << "\nProduct of numbers: " ;

    // if there is a zero in the array, the product is zero
    for( int v : array ) if( v == 0 ) {

            std::cout << "0\n" ;
            return ;
    }

    // largest and smallest values that product can hold
    constexpr long long ubound = std::numeric_limits<long long>::max() ;
    constexpr long long lbound = std::numeric_limits<long long>::min() ;

    long long product = 1 ;

    for( int v : array ) {

            // check if there would be an integer overflow
            const double result = product * double(v) ;
            if( result > ubound || result < lbound ) {

                    std::cout << "integer overflow!\n" ;
                    return ;
            }

            else product *= v ; // multiply only if there is no overflow
    }

    std::cout << product << '\n' ;
}

int calc_min( const array_type& array ) {

    int minimum_value = MAXV ;

    for( int v : array ) if( v < minimum_value ) minimum_value = v ;

    return minimum_value;
}

int get_int( int min_value, int max_value ) {

    std::cout << "enter an integer in [" << min_value << ',' << max_value << "]: " ;
    int number ;

    if( std::cin >> number && number >= min_value && number <= max_value ) return number ;

    std::cout << "invalid input. try again.\n" ;

    // the stream will be in a failed state if the user entered a non-number eg. 'xyz'
    std::cin.clear() ; // clear the possible failed state
    std::cin.ignore( 1000, '\n' ) ; // and throw the bad input away

    return get_int( min_value, max_value ) ; // try again
}

void input( array_type& array ) {

    std::cout << "Input " << N << " numbers from " << MINV << " to " << MAXV << '\n' ;
    for( int& v : array ) v = get_int( MINV, MAXV ) ;
}

int main()
{
    std::srand( std::time(nullptr) ) ; // once, at the start of the program

    array_type array ;

    std::cout << "PROJECT 1\nPART I\nInput numbers or generate randomly?\n\t1) Input numbers\n\t2) Generate randomly\n? ";
    const int input_or_gen = get_int( 0, 1 ) ;

    if( input_or_gen == 0 ) input(array) ;
    else gen_rand_array_fill(array) ;

    print(array) ;
    calc_and_print_product(array) ;
    std::cout << "Minimum value: " << calc_min(array) << '\n' ;
}

http://coliru.stacked-crooked.com/a/065f8ebbe25f3d08
Topic archived. No new replies allowed.