void Ctofile(FILE* f_out, int* C, int C_size) { int i; fprintf(f_out, "["); for(i = 0; i < C_size -1; i++) {//printing all but the last number in C fprintf(f_out, "%i, ", C[i]); } fprintf(f_out, "%i]\n", C[i]);//printing out the last number in C fprintf(f_out, "%i\n", sumarray(C, C_size));//printing A }
int main() { int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) a[i][j] = ((j%2 == 0) ? 1 : -1); /* should sum to zero */ printf("sum=%d\n", sumarray(a)); exit(0); }
// address: 0x804846d int main(int argc, char *argv[], char *envp[]) { __size32 eax; // r24 unsigned int local0; // m[esp - 416] int local1; // m[esp - 404] unsigned int local2; // m[esp - 412] __isoc99_scanf(); local2 = 0; while ((int)local2 < (int)local0) { __isoc99_scanf(); local2++; } eax = sumarray(&local1, local0); printf("Sum = %ld\n", eax); return 0; }
//takes an array of values V, and returns the number for each of these to use to make A in corresponding array C int* changedp(int* V, int V_size, int A, int* DP_table[V_size]) { int i, j; int newA;//A with value in V subtracted int* newC = malloc(V_size*sizeof(int));//values calculated using newA + 1 coin int* minC = malloc(V_size*sizeof(int));//the C with the fewest number of coins if(sumarray(DP_table[A], V_size) != 0) {//if DP_table has already been set for this A, return it return DP_table[A]; } for(i = 0; i < V_size; i++) { if(V[i] == A) {//if A is one of the values in V, return minC with that as the only coin //setting all values in minC to 0, excluding minC[i] for(j = 0; j < V_size; j++) { minC[j] = 0; } minC[i] = 1; DP_table[A][i] = 1;//copying to DP table printf("Setting Base Table for %i \n", A); for(j = 0; j < V_size;j++) { printf("DP_table[%i][%i] = %i\n", A, j, DP_table[A][j]); } return minC; } } for(i = 0; i < V_size; i++) { if(V[i] > A) {//if the coin is larger than the amount we're checking, return minC memcpy(DP_table[A], minC, V_size*sizeof(int));//copying minC to DP_table printf("Setting Table for %i when V[i] > A\n", A); for(j = 0; j < V_size;j++) { printf("DP_table[%i][%i] = %i\n", A, j, DP_table[A][j]); } return minC; } newA = A - V[i]; memcpy(newC, changedp(V, V_size, newA, DP_table), V_size*sizeof(int));//getting change for sub amount new A newC[i] += 1;//adding coin to C if(i == 0) {//if this is the first newC found, it's automatically minC memcpy(minC, newC, V_size*sizeof(int)); } else {//comparing previous best C to new one if(sumarray(newC, V_size) < sumarray(minC, V_size)) {//if the new C has fewer coins than minC, it becomes minC memcpy(minC, newC, V_size*sizeof(int)); } } } memcpy(DP_table[A], minC, V_size*sizeof(int)); printf("nSetting Table for %i \n", A); for(j = 0; j < V_size;j++) { printf("DP_table[%i][%i] = %i\n", A, j, DP_table[A][j]); } return DP_table[A]; }