4 Replies - 2358 Views - Last Post: 12 October 2011 - 03:05 PM Rate Topic: -----

#1 Anday  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 19-May 11

Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 11:06 AM

Hello world,

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

Is This A Good Question/Topic? 0
  • +

Replies To: Finding Even/Odd Using Random Integers

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 11:33 AM

your num doesn't change anywhere in your loop, so all you're doing is repeating exactly the same check for the same number at every iteration
Was This Post Helpful? 1
  • +
  • -

#3 Anday  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 19-May 11

Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 01:23 PM

Alright, so I defined num and seeded rand() inside my loop instead of outside and now it appears to work fine only when debugging. Otherwise even or odd will be the entire range value. Is the program not considering the det_even function when running, but it is while debugging? Or is there something else going on altogether?

This post has been edited by Anday: 12 October 2011 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,010
  • Joined: 08-August 08

Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 02:09 PM

Call srand only once, at the beginning.
Was This Post Helpful? 1
  • +
  • -

#5 Anday  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 19-May 11

Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 03:05 PM

Yes, that was the problem. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1