void main(){ int C = 40; int size = 4; int V[] = {1,5,10,25,50}; int O[] = {0,0,0,0}; for (int C = 2000; C<=2300; C++){ changedp(O,V,C, size); } }
//for each block of input in the argv[1] file, calls the change function for the values listed. Results are printed in filename+change.txt int main(int argc, char* argv[]){ FILE* f_in;//the file read FILE* f_out;//the file written to char* fn_in = argv[1];//the name of the file to be read char* fn_out;//the name of the file to be written to char* f_text;//the text from the file long f_size;//size of file int V[1000];//array of coin values, max size 1000 int v_count = 0;//number of coins int A;//the amount that needs to be made into change char* token;//section of input file from strtok char* token2;//section of input file from strtok char* token_ptr;//used for strtok_r int** DP_table;//table to hold values for dpChange int i, j; clock_t begin, end; double run;//used to measure time //checking if user has provided filename if(argc != 2){ printf("Error: incorrect number of arguments\n"); printf("Usage: changedp [filename]\n\n"); exit(0); } //opening input file f_in = fopen(fn_in, "r"); if(f_in == NULL) { printf("Error: cannot open input file\n"); } //stripping .txt from input filename and adding change.txt, then open output file fn_out = argv[1]; strtok(fn_out, "."); strcat(fn_out, "change.txt"); f_out = fopen(fn_out, "w+"); if(f_out == NULL) { printf("Error: cannot open output file\n"); } //getting text from file fseek(f_in, 0, SEEK_END); f_size = ftell(f_in); rewind(f_in); f_text = malloc(f_size + 1); fread(f_text, f_size, 1, f_in); fclose(f_in); begin = clock();//starting timer fprintf(f_out, "Changedp:\n"); token = strtok_r(f_text, "\n\r", &token_ptr); //run while there are blocks to be read while( token != NULL ) { //getting V v_count = 0; token2 = strtok(token, ", []\n\r"); while(token2!= NULL) {//adding all values to V V[v_count] = atoi(token2); v_count++; token2 = strtok(NULL, ", []\n\r"); } token = strtok_r(NULL, "\n\r", &token_ptr);//getting A A = atoi(token); //malloc for DP table DP_table = (int**)malloc((1+A)*sizeof(int*)); for(i = 0; i < A+1; i++) { DP_table[i] = (int*)malloc(v_count*sizeof(int)); for(j = 0; j < v_count; j++) { DP_table[i][j] = 0; } } //printf("RESULT SUM: %i\n", sumarray(changedp(V, v_count, A, DP_table), v_count));//remove later LAUREN Ctofile(f_out, changedp(V, v_count, A, DP_table), v_count); //free DP_table for(i = 0; i < A+1; i++) { free(DP_table[i]); } free(DP_table); token = strtok_r(NULL, "\n\r", &token_ptr); } fclose(f_out); end = clock();//ending timer run = (double)(end - begin) / CLOCKS_PER_SEC; printf("Running Time: %.2f\n", run); 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]; }
int main(){ FILE *fp, *fp2; char discard; char str[100]; //assume each line has less than 100 characters int number_lines; int i, j; int *V = malloc(100 * sizeof(int)); //assume each line has less than 100 numbers int *C = malloc(100 * sizeof(int)); //assume each line has less than 100 numbers int A; int size; int minimum; fp = fopen ("Coin.txt", "r"); for (i = 0; i < 100; i++){ if (feof(fp)) //end of file break; fgets(str, 100, fp); } number_lines = i; rewind(fp); fp2 = fopen ("Coinchange.txt", "w"); for (i = 0; i < number_lines / 2; i++){ discard = fgetc(fp); //discard [ for (j = 0; j < 100; j++){ fscanf(fp, "%d%c ", &V[j], &discard); if (discard != ',') //end of line break; } size = j + 1; fscanf(fp, "%d", &A); discard = fgetc(fp); //discard newline character //start changeslow int* array = malloc(100 * sizeof(int)); array = changeslow(V, size, A); int summ = 0; for (j = 0; j < size; j++){ //printf("%d ", array[j]); summ += array[j]; } fprintf(fp2, "["); for (j = 0; j < size - 1; j++){ fprintf(fp2, "%d, ", array[j]); } fprintf(fp2, "%d]\n", array[size - 1]); fprintf(fp2, "%d\n", summ); //end changeslow //start changechangegreedy for (j = 0; j < 100; j++){ C[j] = 0; //initiate to zero } minimum = changegreedy(V, C, size, A); fprintf(fp2, "["); for (j = 0; j < size - 1; j++){ fprintf(fp2, "%d, ", C[j]); } fprintf(fp2, "%d]\n", C[size - 1]); fprintf(fp2, "%d\n", minimum); //end changechangegreedy //start changedp for (j = 0; j < 100; j++){ C[j] = 0; //initiate to zero } minimum = changedp(V, C, size, A); fprintf(fp2, "["); for (j = 0; j < size - 1; j++){ fprintf(fp2, "%d, ", C[j]); } fprintf(fp2, "%d]\n", C[size - 1]); fprintf(fp2, "%d\n", minimum); //end changedp fprintf(fp2, "\n"); } fclose(fp); fclose(fp2); printf("See Coinchange.txt for results.\n"); return 0; }