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

#define DIMENSION 4000

int main(int argc, char** argv) {

    double m[DIMENSION][DIMENSION];
    int i,k;
    double a1, a2;
    clock_t start, mid, end;

    for (i=0; i<DIMENSION; i++) 
	for (k=0; k<DIMENSION; k++)
	    m[i][k] = ((double) random()) / ((double) RAND_MAX);

    start = clock();
    a1=0;
    for (i=0; i<DIMENSION; i++) 
	for (k=0; k<DIMENSION; k++) 
	    a1 += m[i][k];

    mid = clock();
    a2=0;
    for (k=0; k<DIMENSION; k++) 
	for (i=0; i<DIMENSION; i++)
	    a2 += m[i][k];
    end = clock();

    printf("First sum of the %dx%d random values in the array: %f\n",
	   DIMENSION,DIMENSION,a1);
    printf("Second sum of the %dx%d random values in the array: %f\n",
	   DIMENSION,DIMENSION,a2);
    printf("The first calculation needed %d clock ticks\n",
	   (mid - start));
    printf("the second needed %d ticks, ", 
	   (end - mid));
    printf("this is roughly %d times the time needed for the first.\n", 
	   (end - mid) / (mid - start)); 

    return 0;
}

