Number Crunching

Forget the graphics. Forget the fancy stuff, which amazingly includes text. At the root of all programming, and the foundations upon which modern computer science is erected, lies the ability to quickly and accurately calculate vast seas of numbers. The traditional title for this activity is Number Crunching.

The first computers developed back in the 1940s were used to calculate missile trajectories. Even Babage’s engine (1820s) was designed to solve complex mathematical problems. This whole obsession with games and Facebook is child’s play next to what raw computing power can do.

Without getting overly complex — and by that, I mean writing code that could calculate π down to the millionth digit — I’ll present a rather simple example of number crunching.

To demonstrate number crunching, I’ve written code that simulates tossing two dice and reporting the total. That would be a random value between 2 and 12, inclusive:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROLLS 10

int main()
{
    int t,r1,r2;

    /* Seed randomizer */
    srand((unsigned int)(time(NULL)));

    /* Roll dice ROLLS times */
    for(t=0;t<ROLLS;t++)
    {
        r1 = (rand() % 6) + 1;
        r2 = (rand() % 6) + 1;
        printf("Roll %d = %d\n",t+1,r1+r2);
    }

    return(0);
}

Here’s sample output:

Roll 1 = 8
Roll 2 = 6
Roll 3 = 9
Roll 4 = 5
Roll 5 = 10
Roll 6 = 7
Roll 7 = 7
Roll 8 = 6
Roll 9 = 7
Roll 10 = 3

To tally the values, I’ll add an array. The array keeps track of the numbers 2 through 12 as they are rolled. The following code shows this modification from the original example (above):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROLLS 100

int main()
{
    int r,t,r1,r2;
    int results[13];

    /* Seed randomizer */
    srand((unsigned int)time(NULL));

    /* Initialize the array */
    for(r=0;r<13;r++)
        results[r] = 0;

    /* Roll dice ROLLS times */
    for(t=0;t<ROLLS;t++)
    {
        r1 = (rand() % 6) + 1;
        r2 = (rand() % 6) + 1;
        results[r1+r2]++;   /* count total */
    }

    /* Display results */
    for(r=2;r<=12;r++)
        printf("%2d: %d\n",r,results[r]);

    return(0);
}

First, I’ve increased the number of ROLLS to 100 in Line 5.

An integer array, results, with 13 elements is created at Line 10. Why 13? Because I’m going to use values 2 through 12 from the dice rolls to set the array’s elements. For example, a roll of 12 would be tallied at array element 13. Array elements 0 and 1 won’t be used as you can’t roll a zero or 1 with two dice.

Lines 16 and 17 initialize the results array, setting all the elements to zero.

At Line 24, the array element matching the number rolled is incremented. That effectively keeps track of the values rolled.

Line 29 displays the results, which are shown below for 100 rolls:

 2: 5
 3: 4
 4: 8
 5: 8
 6: 10
 7: 18
 8: 17
 9: 15
10: 10
11: 2
12: 3

That looks like a good distribution; the most popular rolls are around 7, which has the highest odds. The values 2 and 2 have the least amount of rolls.

To really crunch the numbers, I’ve adjusted the value of ROLLS to 100,000. Here’s that output:

 2: 2852
 3: 5538
 4: 8242
 5: 11212
 6: 13847
 7: 16614
 8: 13831
 9: 11094
10: 8451
11: 5562
12: 2757

Being a hard core nerd who can’t leave well enough alone, I made further modifications so that the output finally looks like this:

Results of 100000 rolls:
Roll	Count	Percent
 2	 2715	2.7%
 3	 5544	5.5%
 4	 8517	8.5%
 5	11135	11.1%
 6	14039	14.0%
 7	16547	16.5%
 8	13835	13.8%
 9	11145	11.1%
10	 8162	8.2%
11	 5624	5.6%
12	 2737	2.7%

Click here to view the code, although I’d encourage you to try and code this type of output on your own first. (Use this Lesson’s second example as a starting point.)

Now this is only a mild example of what I’d call “number crunching.” Obviously you’ll find more complex math problems that provide more of a challenge to the computer. I’ll cover some of those in a later Lesson.