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 7764325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:52:55+00:00 2026-06-01T14:52:55+00:00

I am looking for a simple but efficient Objective-C algorithm which, starting from a

  • 0

I am looking for a simple but efficient Objective-C algorithm which, starting from a given quarter of a year (3 months), adds or subtracts a specific number of quarters.

For example, adding 10 quarters to Q1 of 2012, I get the year: 2012 + Floor(10 / 4) = 2012 + 2 = 2014, then the quarter: 1 + Mod (10, 4) = 1 + 2 = Q3. Subtracting 2 quarters from Q3 of 2011, I want to end up with Q1 of 2011.

What would be the most elegant code to accomplish this?

  • 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-01T14:52:56+00:00Added an answer on June 1, 2026 at 2:52 pm
    - (void)addQuarters:(NSInteger)quarters toYear:(NSInteger *)currentYear andQuarter:(NSInteger *)currentQuarter {
        if (*currentYear < 0 ||
            *currentQuarter < 1 ||
            *currentQuarter > 4) {
            return;
        }
    
        *currentYear += quarters / 4;
        *currentQuarter += quarters % 4;
    
        if (*currentQuarter  > 4) {
            *currentQuarter -= 4;
            *currentYear += 1;
        } else if (*currentQuarter < 1) {
            *currentQuarter += 4;
            *currentYear -= 1;
        }
    }
    
    // Example of use:
    NSInteger year = 2012;
    NSInteger quarter = 2;
    
    [self addQuarters:-7 toYear:&year andQuarter:&quarter];
    NSLog(@"Quarter %i of %i", quarter, year);
    // Output: Quarter 3 of 2010
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question I suspect, but nonetheless: I'm looking for an efficient way to grab
Ok, this is an embarassingly simple-looking problem, but is driving me crazy. I'm learning
I'm looking for a simple to integrate date picker object, which would be triggered
I am looking for some simple and efficient parameter container that would act like
I am looking for the most efficient/direct way to do this simple C/C++ operation:
I'm looking for a simple, efficient way to convert strings in CamelCase to underscore
I have a simple CMS running PHP/MySQL and I'm looking for the most efficient
This is probably a real simple question but I'm looking for the most memory
I am looking for a simple and efficient way to give a special tint
I'm looking for the most efficient/simple way to run a script (we'll call it

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.