I am writing a C++ program outside of my class work that takes a user entered maximum range, uses the value for range and generates that many random integers, then determines whether each integer being generated is even or odd. I want to do this using two functions, so I can understand how to write a multi-function program in C++. One function must determine whether the number is even and then main function which handles everything else.
Here is what I have so far:
/*
Name: Even/Odd Counter
Date: 28/09/11 13:08
Description: User enters a number range and the
program determines the number of odd and even
numbers randomly generated in that range.
*/
#include <iostream>
#include <time.h>
//#include <ctime>
//#include <cstdlib>
using namespace std;
int det_even(int value) {
bool isEven;
if((value % 2) == 0)
isEven = true;
else
isEven = false;
return isEven;
}
int main() {
int range = 0;
cout << "Enter the max range to consider: ";
cin >> range;
cin.get();
int even = 0, odd = 0;
srand(time(NULL));
int num = rand() % range;
for(int i = 0; i < range; i++) {
if (det_even(num))
even++;
else
odd++;
}
cout << endl << "Number of even integers: " << even;
cout << endl << "Number of odd integers: " << odd;
cin.get();
return 0;
}
In the for loop I was just testing to see if that would make sure that each integer in range was being tested. I'm quite unsure if it is necessary - though without even or odd would be 1 - or if I am implementing it correctly. What happens in this program right now is the number entered in range is being printed randomly in the variables even or odd. For example, if the user enters 100 for range, the values for even or odd would be 100. I'm thinking that this has something to do with only one integer being checked in the random integers generated for whether it is even or not. Overall, I'm almost positive that my main issue is how I am handling the random integers. Any direction would be appreciated.
Thanks,
Anday

New Topic/Question
Reply




MultiQuote





|