Skip to content

Instantly share code, notes, and snippets.

@Madsy
Last active September 3, 2016 21:19
Show Gist options
  • Select an option

  • Save Madsy/db224dd2ff683e9bb3445f95b4a5266c to your computer and use it in GitHub Desktop.

Select an option

Save Madsy/db224dd2ff683e9bb3445f95b4a5266c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void sumSubArraysFast(int* arr, size_t size){
int i,j,k;
int matsize = size * size;
int matsizebytes = sizeof(int) * matsize;
int* mat = malloc(matsizebytes);
memset(mat, 0, matsizebytes);
memcpy(mat, arr, size*sizeof(int));
for(i = 1; i < size; i++){
int len = i+1;
for(j = 0; j < (size - (len - 1)); j++){
mat[j + i*size] = mat[j + (i-1)*size] + mat[i+j];
}
}
//print
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
printf("%4d ", mat[j + i*size]);
}
printf("\n");
}
}
int main()
{
int arr[] = { 11,23,36,43,56,63,71,89,93,10 };
printf("slow\n");
sumSubArrays(arr, 10);
printf("fast\n");
sumSubArraysFast(arr, 10);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment