| | |
pseudo-random numbers; printing the number of even numbers between 0 and 10
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 21
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Complex Number Class
- Next Thread: compare function versus == when using strings
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






