I need help with an assignment I am working on. Here is the assignment:

count all the even numbers between X and Y, where X and Y are entered by the user.  Do not include X and Y.
EXAMPLE 1:
Please enter an integer: 0
Please enter another integer: 10
The number of even numbers between 0 and 10 is 4

EXAMPLE 2:
Please enter an integer: 10
Please enter another integer: 0
The number of even numbers between 10 and 0 is 4

Note that the numbers can be entered in any order (that is, the lower one does not have to come first).

I have it just about finished but it is not working properly. Each time I run it, I don't get 4, it keeps changing. I know it's gotta be something small that I am missing, but could you help me. Here is what I have:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int min, max, number = 0;
    unsigned seed = (unsigned long) (time(NULL));
    srand(seed);
    cout << "Please enter an integer: ";
    cin >> min;

    cin.ignore();
    cout << "Please enter another integer: ";
    cin >> max;
    int range = max - min + 1;
    for (int counter = 0; counter <= number; counter++)
    {
        int number = rand()/100%range + min;
        cout << "The number of even numbers between " << min << " and " << max
            << " is " << number;
            break;

    }
    cout << endl;

}

What is wrong with this program that the number printed keeps changing?

Dani AI

Generated

had the right instinct that something small was wrong; correctly pointed out that generating random numbers is unnecessary. The real bugs in the shown code are: 1) using rand() makes the output change each run, 2) declaring int number inside the loop shadows the outer number (so the counter/limit is wrong), and 3) the loop condition and range logic do not actually iterate over the integers strictly between the inputs. The clean fixes are to (a) normalize the endpoints (swap if needed), (b) iterate only over values strictly between them, and (c) test each for evenness or compute the answer arithmetically.

A straightforward loop solution (no randomness, no shadowing):

#include <iostream>
#include <algorithm>

int main() {
    int a, b;
    std::cin >> a >> b;
    if (a > b) std::swap(a, b);
    int count = 0;
    for (int i = a + 1; i < b; ++i) {
        if (i % 2 == 0) ++count;
    }
    std::cout << "The number of even numbers between " << a << " and " << b << " is " << count << '\n';
}

A faster O(1) method computes the first and last even strictly between the endpoints and derives the count:

long long low = a + 1, high = b - 1;
if (low > high) count = 0;
else {
    long long first_even = (low % 2 == 0) ? low : low + 1;
    long long last_even  = (high % 2 == 0) ? high : high - 1;
    count = (first_even > last_even) ? 0 : ((last_even - first_even) / 2 + 1);
}

Notes and cautions: adjacent or equal inputs produce zero. The % test i % 2 == 0 correctly identifies even integers (including negatives) in C++; see the C++ remainder rules for details (Remainder on cppreference). Use std::swap or std::min/std::max to normalize input order (std::swap).

Recommended Answers

All 2 Replies

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int min, max, number = 0;
	unsigned seed = (unsigned long) (time(NULL));
	srand(seed);
	cout << "Please enter an integer: ";
	cin >> min;

	cin.ignore();
	cout << "Please enter another integer: ";
	cin >> max;
	int range = max - min + 1;
	for (int counter = 0; counter <= number; counter++)
	{
		int number = rand()/100%range + min;
		cout << "The number of even numbers between " << min << " and " << max
			<< " is " << number;
		break;

	}
	cout << endl;

}

I don't understand what this problem has to do with random numbers and why you are generating them. You are getting different results each time because you are changing number each time through the loop. You are displaying number, which is a random number, so it's going to change. I don't see anywhere where you test to see whether any integer is even or odd, which is what you are supposed to be counting.

Get the random numbers out of the program since there is nothing random about the problem. If you want to do this by using a for-loop (not necessary in my view; this is a fairly uncomplicated calculation that doesn't need any loops), you'll need to set up a counter, initialize it to 0, then set up a for-loop based on min and max. Inside the for loop, test some integer for evenness. If that integer is even, increment your counter.

thanks!! I appreciate it : )!

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.