Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:45:59+00:00 2026-06-07T03:45:59+00:00

First an overview of what I am trying to do: Every time a user

  • 0

First an overview of what I am trying to do:

Every time a user presses a key, I am recording the current time and once I have more than 5 records I need to calculate the average time between the key presses (whilst only retaining the last 5 – 10 key presses).

When I first sat down and looked at this problem I thought, easy, I will use a stack and simply keep it at less than 10 items.

Question:

  • How do I ensure that a stack only has a maximum of ten values in it?
    (date time type)
  • How do I find the average time between each of the date times stored in the stack?

Thanks

Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:46:00+00:00Added an answer on June 7, 2026 at 3:46 am

    Don’t use a Stack, but use a Queue with limited capacity. This can be achieve by something like :

    public class CappedQueue<T> : IEnumerable<T> {
    
        private readonly m_Capacity;
        private Queue<T> m_InnerQueue;
        public CappedQueue<T>(int capacity) {
            m_Capacity = capacity;
            m_InnerQueue = new Queue<T>(capacity);
        }
    
        // Wrap required methods 
        public void Enqueue(T item){
            if(m_InnerQueue.Count()) == capacity) {
                m_InnerQueue.Dequeue() ;                
            }
    
            m_InnerQueue.Enqueue(item);
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            return m_InnerQueue.GetEnumerator();
        }
    }
    
    
    public class Test {
        public void Foo()
        {
            var queue = new CappedQueue<DateTime>(5);
    
            queue.Enqueue(DateTime.Now); // each time the user press the key
            queue.Enqueue(DateTime.Now); // each time the user press the key
            queue.Enqueue(DateTime.Now); // each time the user press the key
    
    
            TimeSpan diff = (queue.Last() / queue.Fisrt())/queue.Count();
        }
        public void Foo2()
        {
            var queue = new CappedQueue<int>(5);
    
            queue.Enqueue(10);
            queue.Enqueue(20);
            queue.Enqueue(30);
    
            var average = queue.Average(x=>x);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand the python compiler/interpreter process more clearly. Unfortunately, I have not
First a short overview of my code, i hope its understandable: I have a
First I will provide a quick overview of what I am trying to accomplish.
Ok, here's some overview about what I'm trying to achieve. I want the user
( First time programming in PHP. Had some help. Need a bit more. )
I'm trying to use boost::asio for the first time to write a process that
first take a look on this picture from localScope app : i have 2
First you should know I have looked into many questions and none of them
The overview of what i'm trying to do is resend an activate account email
First of all, thanks for taking the time to read my question, I appreciate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.