Hello, new C++ user at University

I am trying to find what would be the best way to count and display the even and odd numbers in a sequence. For example, if the user inputted the sequence 2 - 6, then there would be 3 evens and 2 odds, this is the code I have

In bold is what I have now for determining this, however it doesn't always work out.

//prog2.cpp
//1 - 25 - 2010

//Includes the math and iostream libraries for use later
#include <iostream> 
#include <cmath>

// includes the std library
using namespace std;

//Function prototype that allows us to call the function in main
bool isPrime(int num);

int main()
{
    //define our low and high range variables as int
    int range_low;
    int range_high;
    int odd_numbers;
    int even_numbers;
    int count_for_series;
    
    //Starts with user input for what should be the low number in the sequence
    cout << "Please input a range of numbers you would like checked, starting with the low number first :";
    cin >> range_low;
    
    //If the number entered is 0 or less, the user can fix their submission
    while (range_low <= 0)
    {
        cout << "Error: Value entered is 0 or less, please enter a new value " << endl;
        cin >> range_low;
    }
    
    //Starts the same loop but for the high range number in the sequence 
    cout << "Please Enter The High Number In The Range :";
    cin >> range_high;
    
    //Error checks to see if the higher number is less than the lower one, if so, allows user to change input
    while (range_high < range_low)
    {
        cout << "The second value entered is lower value than the first, please enter a value that is lower :" << endl;
        cin >> range_high;
    }
    //Outputs the sequence the user provided
    cout << "The lowest number in the sequence is " << range_low << " and the highest number in the sequence is  " << range_high << endl;
    //Sees if the range_low value is prime or not, and indicates so to the user
    if (isPrime(range_low) == true)
    {
        cout << "Starting number is prime " << endl;
    }
    else if (isPrime(range_low) == false)
    {
        cout << "Starting number is not prime " << endl;
    }
    //Sees if the range_high value is prime or not, and indicates so to the user
    if (isPrime(range_high) == true)
    {
        cout << "Ending number is prime " << endl;
    }
    else if (isPrime(range_high) == false)
    {
        cout << "Ending number is not prime " << endl;
    }
   [B] // This is where we calculate how many even and odd numbers are in the series
    count_for_series = range_high - range_low;
    if (count_for_series % 2 == 0)
    {
        even_numbers = count_for_series / 2;
        odd_numbers = even_numbers - 1;
    }
    else if (count_for_series % 2 == 1)
    {
        odd_numbers = count_for_series / 2;
        even_numbers = odd_numbers - 1;[/B]
    }
    //Outputs the number of odd numbers followed by the even
    cout << "The number of odd numbers in the sequence is " << odd_numbers << " and the number of even numbers is " << even_numbers << endl;
    
    return(0);
}

//Function for determining if a number is prime or not
bool isPrime(int num)
{
    int i;
    if (num < 0)
    {     
        num = -num;
    }
    if (num == 0)
    {
        return(false);
    }
    else if (num % 2 == 0)
    {
        return(false);
    }
    for (i = 3; i <= sqrt(num); i += 2)
    {
        if (num % i == 0)
        {
            return(false);
        }
    }
    
    return(true);
}

Would you mind defining what "doesn't work" means? Don't know whether that means it doesn't compile, displays the wrong numbers, causes a citywide blackout.

Details. Exactly what does it do, and what were you expecting?

Your code doesn't do what you expect it to do. Consider some simple example ranges:

1,2,3 (odd range, two odds)
2,3,4 (odd range, two evens)
1,2 (even range, one each)
2,3 (even range, one each)

This suggests to me that for even length range, the algorithm is very simple. For odd length range, you need to find the odd/evenness of the first number, then calculate the counts for the remaining even-length range range. (hint: if the extremes of the range have the same oddness, they are odd-length)

But in fact merely calculating and displaying the counts does not meet the spec. Here is your description of the problem: count and display the even and odd numbers in a sequence I parse this requirement to differ from your intention in three ways:

  1. You are not displaying the even and odd numbers
  2. You did not count them, but calculated them
  3. A range is a (simple) subset of all possible sequences

In order to deal with a sequence, point two will be immediately fixed, since you cannot calculate the count of odds and evens in a general sequence.

P.S. Although 0 == number % 2 is entirely correct to discover evenness, I sometimes prefer to use bitwise and: 0 == (number & 1) because. Hmm. I suppose because I like it. Beware that equality operator binds tighter than bitwise and

Yes so what I was getting at is not counting the numbers themselves, as in I want the number values for the odds and evens between a range, but how many odds and evens added up (1,2,3) there are.

I do not know this 0 == (number & 1) even test
I am in a new CPSC 122 course that is the basics of C++
I am a freshman still, just got out of a semester of Python.

Hi
try this:
1. You need to declare two int variables one for odd and the other for the even
2. Use a for loop that start at the low range and continues to the the max range
3. Now you will deal with your counter in the loop ,Inside your loop you will need two if statements
a) if the( counter % 2==0) then increment the even counter and you can print it out if you needed to .
b) the other if statement check if the (counter % 2==1) then increment the odd and so on.

Perhaps if you write it in python (just to help you see the program flow) then translate that flow into C++. One of the things we all eventually learn is that a programming language is just a way to accurately describe to the computer what you want it to do. ... And eventually, we learn that if we can describe it at the appropriate level of detail in any language, we can much more easily translate that description into code than if we just kind of dive in and start coding without the first step of thinking / describing to ourselves clearly and completely what we want done.

So far, I'm confused by your English language descriptions of the problem: how many odds and evens added up (1,2,3) there are. for instance. It's ok if your English isn't clear and descriptive as long as your understanding of the problem is... but describing it to other people is a problem in that case.

So far, I'm confused by your English language descriptions of the problem:

My guess is we won't get a better description. I already asked for one and was completely ignored.

I think you can have an algorithm that does this in constant time. I'm not sure why you have so much code[ didn't bother to look ]. Try to formulate a formula for a given range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.