pseudo-random numbers; printing the number of even numbers between 0 and 10

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 21
Reputation: WonderWomen204 is an unknown quantity at this point 
Solved Threads: 0
WonderWomen204 WonderWomen204 is offline Offline
Newbie Poster

pseudo-random numbers; printing the number of even numbers between 0 and 10

 
0
  #1
Mar 31st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: pseudo-random numbers; printing the number of even numbers between 0 and 10

 
0
  #2
Mar 31st, 2008
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int min, max, number = 0;
  11. unsigned seed = (unsigned long) (time(NULL));
  12. srand(seed);
  13. cout << "Please enter an integer: ";
  14. cin >> min;
  15.  
  16. cin.ignore();
  17. cout << "Please enter another integer: ";
  18. cin >> max;
  19. int range = max - min + 1;
  20. for (int counter = 0; counter <= number; counter++)
  21. {
  22. int number = rand()/100%range + min;
  23. cout << "The number of even numbers between " << min << " and " << max
  24. << " is " << number;
  25. break;
  26.  
  27. }
  28. cout << endl;
  29.  
  30. }

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 21
Reputation: WonderWomen204 is an unknown quantity at this point 
Solved Threads: 0
WonderWomen204 WonderWomen204 is offline Offline
Newbie Poster

Re: pseudo-random numbers; printing the number of even numbers between 0 and 10

 
0
  #3
Mar 31st, 2008
thanks!! I appreciate it : )!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC