Part I can be found here

To extract each digit we need to divide the number by its base. The traditional numbers with which we work are base 10 or decimal. Each iteration we extract a digit by getting the remainder of the number divided by ten and then dividing the number by ten and assigning the number that value:

Recursively it would look like:

You can use this same principle in any base or to find the base equivalents.

In order to verify a number is prime it must be divisible only by one and itself. So, as we iterate to the square root of the number, if at any time it can be divided equally, we return false:

There are a few optimizations one could make, like immediately filtering out known composite numbers, or changing the incremental values, but for the purposes of quick illustration, the above function is satisfactory.

In short, an object has control over another. If the parent object destroys the child object when it is destroyed then the relationship is composition. If the child object continues to exist after the parent object is destroyed, it is aggregation.

One example is college. The college hires professors, but if the school shuts down, the professors continue on being professors, their lifetime (in the terms of a modeled program) does not end with the destruction of their "owner".

In code:

In composition Tim is tied to the college and dies with it

--

If you know of a topic that would be a good fit for the FAPQ, leave it the comments below or shoot me a PM.

Happy coding!

**4. Getting digits from a number****Purpose:**Validation, summation, etc... Excellent example of how math is applied in a programming context. Also, the concept of numerical bases.Quote

Given a number, write a function that sums its digits and returns that sum

To extract each digit we need to divide the number by its base. The traditional numbers with which we work are base 10 or decimal. Each iteration we extract a digit by getting the remainder of the number divided by ten and then dividing the number by ten and assigning the number that value:

int sumDigits(int num){ int total = 0; while(num > 0){ total += num % 10; num /= 10; } return total; }

Recursively it would look like:

int recurSumDigits(int num){ if (num <= 0) return 0; return ((num % 10) + recurSumDigits(num/10)); }

You can use this same principle in any base or to find the base equivalents.

**5. Prime Numbers****Purpose:**Quite a few problems require you to find prime numbers and use them in some fashion, for example, some public/private key cryptography is based off of large prime numbers.In order to verify a number is prime it must be divisible only by one and itself. So, as we iterate to the square root of the number, if at any time it can be divided equally, we return false:

bool isPrime(int n){ if (n < 2) return false; else if (n == 2) return true; for(int i = 2; i*i <= n; i++){ if (n % i == 0) return false; } return true; }

There are a few optimizations one could make, like immediately filtering out known composite numbers, or changing the incremental values, but for the purposes of quick illustration, the above function is satisfactory.

**6. Aggregation versus Composition****Purpose:**How to manage an object's lifetime. Object relationships.In short, an object has control over another. If the parent object destroys the child object when it is destroyed then the relationship is composition. If the child object continues to exist after the parent object is destroyed, it is aggregation.

One example is college. The college hires professors, but if the school shuts down, the professors continue on being professors, their lifetime (in the terms of a modeled program) does not end with the destruction of their "owner".

In code:

//composition class College{ private: Professor* tim; public: College(); ~College(); }; College::College(){ tim = new Professor(); } College::~College(){ delete tim; //no not tim! } //aggregation class College{ private: Professor* teachingPosition; public: College(); ~College(); }; College::College(Professor* tim){ teachingPosition = tim; // "hire" tim } College::~College(){ //nothing to delete Tim lives on! teachingPosition = 0; //get rid of dangling just in case }

In composition Tim is tied to the college and dies with it

*(apparently Tim is also a sea captain)*. In aggregation Tim is free to exist once the college shuts down.--

If you know of a topic that would be a good fit for the FAPQ, leave it the comments below or shoot me a PM.

Happy coding!

### 1 Comments On This Entry

Page 1 of 1

Page 1 of 1

### ← September 2016 →

S | M | T | W | T | F | S |
---|---|---|---|---|---|---|

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 |

### Tags

### My Blog Links

### Recent Entries

### Recent Comments

### Search My Blog

### 0 user(s) viewing

**0**Guests

**0**member(s)

**0**anonymous member(s)