473,508 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help Fast w/ Counting Consecutives

1 New Member
Hi, I need this fast 10pm today. I need help with some code. I am working on this program...a user inputs an integer (For example, 50) between 1 and 100. I have a for loop that generates random numbers between 1 and 100 and iterates according to user input. Now i need to figure out the number of consecutive times that a random numbers occurs below or above the user input.
For example...
cout<<"What's your number?";
cin>>50
cout<<"How many random numbers would you like?";
cin>>10

Output: 24 34 54 67 98 12 34 44 99 10 (random numbers)

This is what I need to figure out...how many times the random numbers occurs consecutively...
24 and 34 are 2 consecutives below 50
54, 67, and 98 a 3 consecutives above 50
12, 34, 44 are 3 consecutives below 50

Thank you!
Jul 3 '06 #1
1 1481
D_C
293 Contributor
Here's what I did with STL. Hopefully that's off limits, so you will have to change it around a little. The basic logic should still be the same. That's what you get since memory management owns me. I'm sure there is an easier way, especially by reusing the code at the end, but that's what you can have.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4.  
  5. int main()
  6. {
  7.   const unsigned int MAX = 100;
  8.  
  9.   unsigned int number = 0;
  10.   unsigned int limit = 0;
  11.  
  12.   bool prev;
  13.   bool next;
  14.   vector<unsigned int> v;
  15.  
  16.   do
  17.   {
  18.     cout << "What's your number (1-" << MAX-1 << ")?" << endl;
  19.     cin >> number;
  20.   } while(!((0 < number) && (number < MAX)));
  21.  
  22.   do
  23.   {
  24.     cout << "How many numbers (at least one required)?" << endl;
  25.     cin >> limit;
  26.   } while(limit == 0);
  27.   cout << endl;
  28.  
  29.   v = vector<unsigned int>();
  30.   v.reserve(limit);
  31.  
  32.   int i = 0;
  33.   unsigned int temp;
  34.   while(i < limit)
  35.   {
  36.     do
  37.     {
  38.       temp = rand()%MAX;
  39.     }
  40.     while(temp == number);
  41.     v.push_back(temp);
  42.     i++;
  43.   }
  44.  
  45.   vector<unsigned int>::iterator it;
  46.   for(it = v.begin(); it != v.end(); it++)
  47.     cout << *it << " ";
  48.   cout << endl << endl;
  49.  
  50.  
  51.   // variables temp, i and it are reused. It's
  52.   // a bad practice, that's why I'm warning.
  53.   i = 0;
  54.   it = v.begin();
  55.   temp = *it; // not really necessary
  56.   prev = (temp > number);
  57.  
  58.   cout << temp;
  59.   it++;
  60.   i++;
  61.  
  62.   do
  63.   {
  64.     temp = *it; // not really necessary
  65.  
  66.     next = (temp > number);
  67.     if(prev == next)
  68.     {
  69.       if(i++ == 0)
  70.         cout << temp;
  71.       else
  72.         cout << ", " << temp;
  73.     }
  74.     else
  75.     {
  76.       if(i < 2)
  77.         cout << " is ";
  78.       else
  79.         cout << " are ";
  80.  
  81.       if(prev)
  82.         cout << "above ";
  83.       else
  84.         cout << "below ";
  85.  
  86.       cout << number << endl;
  87.  
  88.       i = 1;
  89.       prev = next;
  90.       cout << temp;
  91.     }
  92.     it++;
  93.   } while(it != v.end());
  94.  
  95.     if(i < 2)
  96.       cout << " is ";
  97.     else
  98.       cout << " are ";
  99.  
  100.     if(prev)
  101.       cout << "above ";
  102.     else
  103.       cout << "below ";
  104.  
  105.     cout << number << endl << endl;
  106.  
  107.   system("PAUSE");
  108.   return EXIT_SUCCESS;
  109. }
  110.  
What's your number (1-99)?
0
What's your number (1-99)?
100
What's your number (1-99)?
50
How many numbers (at least one required)?
0
How many numbers (at least one required)?
8

41 67 34 0 69 24 78 58

41 is below 50
67 is above 50
34, 0 are below 50
69 is above 50
24 is below 50
78, 58 are above 50

Press any key to continue . . .
Jul 3 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1523
by: pmatos | last post by:
Hi all, I've been having questions about strings, references, initializations... I've created code (which will not compile due to a reference problem) and I'd like comments on why this won't...
9
2293
by: terry | last post by:
I am a programmer (cobol, peoplesoft, sqr, etc.) so I am familiar with programming logic, etc. but not very familiar with C. I need a C program in a study I'm doing. The program is fairly simple,...
4
4177
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
4
2178
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
2110
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
19
5379
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and...
0
2354
by: Jorhajnes | last post by:
Hi there. Im very new to making games in flash, although I have used flash for quite some time now in web-based situations so I know my way around. Anyways, I thought it would be fun to try...
27
2066
by: Mark | last post by:
Hi all, I have a scenario where I have a list like this: User Score 1 0 1 1 1 5 2 3 2 1
1
1469
by: cubabellaro | last post by:
I need to obtain a numeric value from a dropp down menu to validade the information due to i'm trying to define dates from the user. I need to avoid conflict because the user can't select a date...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.