0% found this document useful (0 votes)
135 views4 pages

Coin Row

This C++ program uses a dynamic programming approach to find the maximum value that can be obtained from a coin row problem using a given set of coin values. It takes in a user-input number of coins, stores the coin values in an array, calculates the maximum amounts for all subproblems and stores them in another array, and finally outputs the overall maximum value and the coins in the optimal set.

Uploaded by

api-373837960
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views4 pages

Coin Row

This C++ program uses a dynamic programming approach to find the maximum value that can be obtained from a coin row problem using a given set of coin values. It takes in a user-input number of coins, stores the coin values in an array, calculates the maximum amounts for all subproblems and stores them in another array, and finally outputs the overall maximum value and the coins in the optimal set.

Uploaded by

api-373837960
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

* Title: coin_row.cpp

* Abstract: This program conducts heap operations on a user input array.

* Author: Wayne Webster

* ID: 2191

* Date: 07/29/2017

* Code assistance: http://rashmikeralapura.blogspot.com/2013/05/coin-row-problem.html

*/

#include <iostream>

using namespace std;

int *coins;

int *amount;

int n=0;

int s=0;

void MaxCoins(int* amount, const int* coins) {

amount[0] = 0;

amount[1] = coins[1];

for(int i = 2; i < n; ++i) {

amount[i] = max(coins[i] + amount[i-2], amount[i-1]);

// cout << "Position ->>" << i << " Amount--> "<< amount[i] << " Coins + Amount -->" <<coins[i]
+amount[i-2] << " amount[i-1] -->" << amount[i-1] << endl;

// for (int i=0; i<n;i++){

// cout << "position " << i << " amount " << amount[i] << endl;

// }
if(amount[n-1] > amount[n-2]){

int i=0;

cout << "Best set: ";

for(i = n-1; i>2;i=i-2){

cout << i << ", ";

if(i==1){

cout << "1" << endl;

else if (i==2 && amount[n-(n-1)]==amount[n-(n-2)]){

cout << "1" << endl;

else if (amount[n-(n-1)]==amount[n-(n-2)] ){

cout << "1" << endl;

else if (amount[n-(n-1)]==amount[n-(n-2)] && n>2){

cout << "1" << endl;

else if(i<=2 && amount[n-(n-1)]==amount[n-(n-2)] ){

cout << "1" << endl;

else{

cout << "2" << endl;

else{

int i =0;

cout << "Best set: ";

for(i = n-2; i>2;i=i-2){


cout << i << ", ";

//cout << "what is i " << i << endl;

if (amount[n-(n-1)]==amount[n-(n-2)] && n>2){

cout << "1" << endl;

else if(i<=2 && amount[n-(n-1)]==amount[n-(n-2)]){

cout << "1" << endl;

else if (i==-1){

cout << "No Natural Sets" << endl;

else{

cout << "2" << endl;

cout << "Max Value: " << amount[n-1] << endl;

int main() {

cout << "Number of coin(s): ";

cin >> n;

n++;

coins=new int[n];

amount=new int[n];

cout << endl;

cout << "Value(s): ";

coins[0]=0;

for(int i=1; i<n;i++){

cin >> s;
coins[i]=s;

MaxCoins(amount, coins);

cout << endl;

return 0;

You might also like