Beispiel #1
0
struct mincoins changeslow(int A, struct Arr* K)
{
	int i,k,j;
	int T;
	struct mincoins m,m1,m2;
	
	for (k = 0; k < K->size ; k++)
		m.coins[k] = 0;
	
	if (A == 0)
	{
		m.num = 0;
		for (k = 0; k<K->size ; k++)
			m.coins[k] = 0;
		
		return m;
	}
	for(i = 0; i < K->size; i++)
	{
		if(K->array[i] == A)
		{
			m.num = 1;
			m.coins[i] += 1;
			return m;
		}
			
	}
	
	m.num = BIG_NUMBER;
	
	for(i = 1; i < A; i++)
	{
		m1 = changeslow(i,K);
		m2 = changeslow(A-i,K);
		
		T = m1.num + m2.num;
		if (T < m.num)
		{
			m.num = T;
			
			for(j = 0; j < K->size; j++)
			{
				m.coins[j] = m1.coins[j] + m2.coins[j];
			}
		}	
	}
	
	return m;	
}
Beispiel #2
0
int* changeslow(int *V, int size, int A){
	int i, j;
	int num_coins;
	int minimum = INT_MAX; //infinity
	int *left = malloc(100 * sizeof(int));
	int *right = malloc(100 * sizeof(int));
	int *cnt = malloc(100 * sizeof(int)); //assume each line has less than 100 numbers
	for (i = 0; i < size; i++){
		left[i] = 0; //initiate to zero
		right[i] = 0;
		cnt[i] = 0;
	}
	if (A == 0){
		return cnt;
	}
	for (i = 0; i < size; i++){
		if (A == V[i]){ //if there is such a coin, then that one coin is the minimum
			cnt[i]++;
		return cnt;
		}
	}
	for (i = 0; i < size; i++){
		num_coins = 0;
		if (A > V[i]){
			left = changeslow(V, size, A - V[i]); //find the minimum number by divide and conquer
			right = changeslow(V, size, V[i]); //find the minimum number by divide and conquer
			for (j = 0; j < size; j++){
				left[j] += right[j];
				num_coins += left[j]; //sum left and right
			}
			if (minimum > num_coins){
				minimum = num_coins; //choose the minimum number of coins
				for (j = 0; j < size; j++){
					cnt[j] = left[j]; //memo count of each coin as array
				}
			}
		}
	}
	return cnt;
}
/*
 * Brute Force or Divede and Conquer Algorithm
 * Recursively find the correponding coin number of each denominations
 */
vector<int> changeslow(const vector<int>& V, int A){
	//Declare a vector to store the correponding coin number 
	//of each denominations. Initialize each element to 0.
	vector<int> C(V.size(), 0);
	//Base case
	for (int i = 0; i < V.size(); i++){
		if (A == V[i]){
			C[i] = 1;
			return C;
		}
	}

	//Define a variable to hold the minimum sum
	//Initialize it to max int
	int min_sum = INT_MAX;

	//Recursive case
	for (int i = A - 1; i >= A / 2; i--){
		vector<int> low, high;

		//Find the minimum number of coins needed to make A-i cents
		low = changeslow(V, A - i);

		//Find the minimum number of coins needed to make i cents
		high = changeslow(V, i);

		//Choose the i that minimizes this sum
		int sum = count(low) + count(high);
		if (min_sum > sum){
			min_sum = sum;
			C = merge(low, high);
		}

	}
	return C;
}
Beispiel #4
0
/**************************************************************
 * *  This main function creates a vector of randomly generated integers
 * *  for use in testing the implementations of Algorithms 1-4. The first part measures
 * *  the running time in ms for each Algorithm for display. The second part tests
 * *  input output by reading input from a specified text file, running each Algorithm
 * *  on this input, and writing the results to another specified text file.
 * ***************************************************************/
int main(int argc, char* argv[]){
	std::string filename;
	std::string outputfilename;
	/* The first argument (argc) is the number of elements in the array so we should have two elements the program name and file name 
	Credit: http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html
	*/
    if(argc != 2)
    {
        std::cout << "Please enter an input filename." << std::endl << std::endl;
        exit(1);
    }
	/* which is the second argument (argv). The second argument is always an array of char*, */
    else
    {      
        filename = std::string(argv[1]);
		std::string tempoutputfilename = std::string(argv[1]);
		//http://www.cplusplus.com/reference/string/string/length/
		int strsize = tempoutputfilename.length() - 4;
		/* http://www.cplusplus.com/reference/string/string/operator+/ 
		http://www.cplusplus.com/reference/string/string/substr/ */
		outputfilename = (tempoutputfilename.substr(0, strsize)) + "change.txt";
		std::cout << outputfilename << std::endl;
	}
	//cout << filename << endl;
	/* Stream class provided by C++ to read from files
	Credit: http://www.cplusplus.com/doc/tutorial/files/*/
    std::ifstream textfile;
	/* In order to open a file with a stream object we use its member function open */
    textfile.open(filename);
	/* To check if a file stream was successful opening a file, you can do it by calling to member is_open
	Credit: http://www.cplusplus.com/doc/tutorial/files/*/
    if(!textfile.is_open())
    {
        std::cout << "The file could not be opened." << std::endl;
        textfile.close();
        exit(1);
    }
	/* Call function to put first alternating lines as the coin set input and the second alternating lines as total change V */
    std::vector<std::vector<int>> coinsetinput;
    std::vector<int> changevalueV;
    getinput( textfile, coinsetinput, changevalueV );
    textfile.close();
	/* Stream class to write on files
	Credit: http://www.cplusplus.com/doc/tutorial/files/*/
	std::ofstream textfile2;
    textfile2.open(outputfilename);
    if(!textfile2.is_open())
    {
        std::cout << "Cannot open for writing. Check the permissions of the directory." << std::endl;
        textfile2.close();
        exit(1);
    }
	/* Display a babel for brute force algorithm time trial */
    std::cout << "Testing changeslow...." << std::endl;
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        /* Run brute force algorithm on input numbers from first to last element */
		std::vector<int> coinCount;
		runtimetrial( changeslow, coinsetinput.at(i), changevalueV.at(i), coinCount );
    }
	/* Display a babel for greedy algorithm time trial */
    std::cout << "Testing changegreedy...." << std::endl;
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        /* Run greedy algorithm on input numbers from first to last element */
		std::vector<int> coinCount;
        runtimetrial( changegreedy, coinsetinput.at(i), changevalueV.at(i), coinCount);
    }
    /* Display a babel for dynamic programming algorithm time trial */
    std::cout << "Testing changedp...." << std::endl;
    /*for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
         Run greedy algorithm on input numbers from first to last element 
		
    }*/
	/* Call function to output is to be written to text file */
    textfile2 << "Brute Force \n\n";
	 /* Run changeslow algorithm on input numbers */
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        std::vector<int> coinCount;
        int minCoins = changeslow( coinsetinput.at(i), changevalueV.at(i), coinCount );
        writeResults( textfile2, coinCount, minCoins );
    }
	/* Call function to output is to be written to text file */
    textfile2 << "Greedy\n\n";
    for( unsigned int i = 0; i < coinsetinput.size(); i++ )
    {
        std::vector<int> coinCount;
        int minCoins = changegreedy(coinsetinput.at(i), changevalueV.at(i), coinCount);
        writeResults( textfile2, coinCount, minCoins );
    }
	/* Call function to output is to be written to text file */
    textfile2 << "Dynamic Programming\n\n";
    //for( unsigned int i = 0; i < coinsetinput.size(); i++ )
   // {
      //  std::vector<int> coinCount;
       // int minCoins = changedp();
        //writeResults( textfile2, coinCount, minCoins );
	//}
    textfile2.close();	
}
Beispiel #5
0
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;
}