### 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....

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include #include #include #include #include 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
 ``123456789101112131415`` `````` 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.