a queue to track the 10 most recent #'s

So you are picking #'s and this program needs to the ten last numbers. When you press 'enter' the program will check if the input is a #. So if the input 'a' then output wrong input. And if it is valid number then add to queue. When the user presses N, then the last ten #'s need to print out.

Any idea how to start this?
Start by getting one number correctly. To keep track of the last ten numbers you could use a queue.
Last edited on
You need the last 10 numbers?
Using a queue?

Umm.
Okay.

Use std::queue and when the user enters 'N', then call pop() until size() == 10.
Then the rest of the queue will contain the last 10 numbers the user entered.
(You can access the next one by calling front() and then pop it with pop())
whoovian11 didn't say they had to use a queue, that was what I suggested. From what I've seen in other posts though you seem to be more knowledgeable about C++ than me, so I'd go with whatever you come up with.
you can't use std queue...my teacher said you have to do it the long way
Start by getting input from the user until the queue contains 10 numbers
Any further input you get, simply pop from the queue first then push the new number into the queue.

If you don't need the 10 most recent and instead want to keep a randomly selected amount from the user inputs, then using a randomized queue will greatly help here. This is one I wrote sometime this year (In java):

https://dl.dropboxusercontent.com/u/69324346/RandomizedQueue.java
StdRandom and StdOut are not part of the java library, so to make use of them, you have to replace them with their corresponding java counterparts (Random, System.out)
Last edited on
If you can't use a queue, then I'd just use an array of ten numbers. Start at position 0 and keep track of where you are in the array. So the first number would go in position 0, the second in position 1, ... the 10th in position 9, and the 11th would loop back to the start and go in position 0. The get the numbers back, you would just have to print them out in reverse order from where you last put one in.
could you give me a pseudocode?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>


bool getNumber ( double numbers[], int & position );

void displayNumbers ( double numbers[], int position );


int main ( )
{
	double lastTenNumbers[10];

	int position = 0;

	do {
		getNumber ( lastTenNumbers, position );
	} while ( position < 10 );

	--position;

	displayNumbers ( lastTenNumbers, position );
}

bool getNumber ( double numbers[], int & position )
{
	std::cout << "Number: ";
	std::cin >> numbers[position];

	++position;

	if ( std::cin ) return true;

	return false;
}

void displayNumbers ( double numbers[], int position )
{
	int i = position;

	std::cout << numbers[i--] << ' ';

	while ( i != position )
	{
		std::cout << numbers[i] << ' ';

		if ( i == 0 ) i = 9;
		else --i;
	}

	std::cout << '\n';
}


better than pseudocode, but not good code so don't use it.
thank you soo much
but how do i do the 'When the user presses N, then the last ten #'s need to print out. '
You need to check somewhere for them to press N. If they do, call your displayNumbers equivalent function.
SOrry another question so if I write an 11th number how can i get the 1st number to be removed and the 11th number be the 10th number..
Replace the first number with the 11th number.
i am still confused on the Nth part...so I need to give then option to add more url or press N to display right?
And then what?
If they want to add more, replace the oldest one with the new one. If they want to display them, print them out in reverse order.
Topic archived. No new replies allowed.