Jump to content
  • Advertisement
Sign in to follow this  
Kjansen92

Simple C++ Design Question for Numerology App

This topic is 2137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

Very simple question on how best to go about this with c++.

 

I'd like to make an app where a user enters their year, month, and date of birth, and then return 3 numbers:

    -Entire birth day condensed down to one digit.

    -Date of birth condensed to one digit.

    -Month and date of birth condensed to one digit.

I am completely unsure which containers, classes, and functions could be used to accomplish this, as I am a complete noobie.

 

If anyone could make any suggestions, they would be greatly appreciated! :)

 

Thanks,

Kev

Share this post


Link to post
Share on other sites
Advertisement

-Entire birth day condensed down to one digit.

-Date of birth condensed to one digit.

-Month and date of birth condensed to one digit.

...
What does this even mean?

Perhaps a few examples might clear things up...

Share this post


Link to post
Share on other sites

Absolutely!

 

If I was born on August 4, 2097, then I would like those numbers (ie. 8/4/2097) all condensed to one digit. 

Condensing is basically saying that each digit is added up, until the result is a single digit.

In this example. that would mean:

    8+4+2+0+9+7, which would give you

    30

Then, since I only want one digit, it would condense to the number 3.

 

This is what I want my program to accomplish, given the entire birthday, as well as for just the date, and then again for the month and the day together.

 

It's very basic, I just don't know how to go about it.

Share this post


Link to post
Share on other sites

Basically, what I'd like to know is, if I have something like this:

 

int reduceAllToOneDigit(int x, int y, int z)

{

int use;

use = x+y+z;

if(use>9)

//What to put here?

else

cout << "Life Path #:\t" << use << ".";

return use;

}

 

What could I use (maybe an iterator?) to iterate from the beginning digit to the end digit, to add them all up, one by one, until they are finally condensed down to one digit? A while loop would make more sense there, as well, I just realized, instead of an if loop.

Share this post


Link to post
Share on other sites

To get each digit, look at the modulus division operator %.

 

For example:

int lastDigit = x % 10;

 

if x = 1234, lastDigit will be 4.

 

so for your loop, perhaps something like this:

 

while( x != 0)

{

  sum += x % 10;

  x /= 10; 

}

 

You will need to repeat this on the sum if it is over 10, and repeat that again if the next sum is over 10.

Share this post


Link to post
Share on other sites

How so? 26 % 10 = 6, puts your sum at 6, divide x by 10, x is now 2(integer division), 2 % 10 = 2, sum is now 6 + 2 = 8, dividing x by 10 again gives you x = 0 and the loop breaks.

Share this post


Link to post
Share on other sites
int condense_positive_number(int n) {
  return 1 + (n - 1) % 9;
}

Awesome, how did you come up with that?

Share this post


Link to post
Share on other sites

All the powers of 10 are congruent to 1 modulo 9, which means that a number and the sum of its digits are congruent modulo 9. If you iterate this enough times, you'll end up with a single digit that depends only on the congruence class modulo 9 of the original number. The subtracting 1 and adding 1 part are there to return `9' instead of `0' for multiples of 9.

 

I haven't watched the whole thing, but this seems like a reasonable explanation, if the above didn't make sense:

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!