arrays and random number sticking??

Hey guys,
so here's the problem,
I made a dowhile loop that randomly generates numbers and inputs them into 25 arrays.

after that I want to determine which ones odd and even.
but for some reason, before I can input a blank 0 for the totalodd or totaleven,
somehow the random number that was generated gets inputted into the totalodd.
how is that happening?
I need it to stay at 0 before entering the function but I just cant figure out how, its just dumping the random number there....


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
#include <iostream>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <ctime>

using namespace std;

//  Function Prototypes ****************************************************
void findMinmax (int & min, int &max);
int randNum (int min, int max);
void dispArr ();
void calcTot (int & totaleven, int & totalodd, int a[]);
//  main function **********************************************************
int main ()
{
srand(time(NULL));
int min, max, numone;
int totaleven = 0;
int odd = 0;
int counter = 0;
int arr [25];
findMinmax(min, max);
cout << "total even is " << totaleven << endl;   //doesnt change  (stays at 0)
cout << "total odd is: " << odd << endl;         //doesnt change  (stays at 0)
do
{
numone = randNum(min, max);
cout << "the random number is: " << numone << endl;
arr[counter] = numone;
cout << "the counter is: " << counter << endl;
counter++;
}while (counter<26);

cout << "total even is " << totaleven << endl; //doesnt change (stays at 0)
cout << "total odd is: " << odd << endl;       //CHANGES INTO RANDOM NUMBER???
calcTot(totaleven, odd, arr);

	cout << "Please press enter once or twice to continue...";
	cin.ignore().get();
	return EXIT_SUCCESS;           
}

void findMinmax(int & min, int &max)
{
do
{
cout << "Enter the Minimum (higher than 0)" << endl;
cin >> min;
cout << "Enter the Maximum (lower than 501)" << endl;
cin >> max;
}while ((min<=0)||(max>=501));
}

int randNum (int min, int max)
{
int numone;
numone = min + rand()%(max - min + 1);
return numone;
}

void calcTot(int & totaleven, int & totalodd, int a[])
{
int count = 0;
cout << "total even is " << totaleven << endl;
cout << "total odd is: " << totalodd << endl;
do
{
if(a[count]%2 == 0)
{
totaleven=+a[count];
cout << "the total of even number is:" << totaleven << endl;
count++;
}
else
{
totalodd+=a[count];
cout << "the total of odd number is:" << totalodd << endl;
count++;
}
}while (count<26);
cout << "total of even is: " << totaleven;
cout << "total of odd are: " << totalodd;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int arr [25];
    findMinmax(min, max);
    cout << "total even is " << totaleven << endl;   //doesnt change  (stays at 0)
    cout << "total odd is: " << odd << endl;         //doesnt change  (stays at 0)
    do
    {
        numone = randNum(min, max);
        cout << "the random number is: " << numone << endl;
        arr[counter] = numone;
        cout << "the counter is: " << counter << endl;
        counter++;
    }while (counter<26);

    cout << "total even is " << totaleven << endl; //doesnt change (stays at 0)
    cout << "total odd is: " << odd << endl;       //CHANGES INTO RANDOM NUMBER??? 


Valid indices for arr are 0 to 24. You're assigning values to arr[0] to arr[26]. Two of those indices are out of the bounds of arr. You're modifying memory you shouldn't, so don't be surprised if unrelated variables (such as totalodd) are affected by the undefined behavior you've got going on here.

Last edited on
Topic archived. No new replies allowed.