Subscribe to Stuck in an Infiniteloop        RSS Feed

Frequently Asked Programming Questions - Part II

Icon 1 Comments
Part I can be found here

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.


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:

class College{
     Professor* tim;

    tim = new Professor();

    delete tim; //no not tim!

class College{
     Professor* teachingPosition;

College::College(Professor* tim){
    teachingPosition = tim; // "hire" tim

  //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

taylorc8 Icon

30 August 2010 - 09:42 AM
More inheritance and polymorphism, go go go!
Page 1 of 1

February 2017

19 202122232425


    Recent Entries

    Search My Blog

    4 user(s) viewing

    4 Guests
    0 member(s)
    0 anonymous member(s)