-1

I need to give the minimum amount of change possible.I input number of cases,each case had a number of coins(1 is not necessarily a part of them) and the number of number I want to test.Then I enter the different coins and the different number to test.

I dont know why my program isnt working.Since 1 isnt necessarily part of the change,I had to tweak the program a little.

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<functional>
#include<numeric>
#include<algorithm>
#include<vector>

using namespace std;



int main()
{
    int n,i;
    cin>>n;
    int f=n,c,m;
    int flag=0;
    int m1;
    int coins[100];
    vector <int>storage(100,0);
    vector <int> testcases(1000,0);
    vector <int> answers(1000,-1);

    while(n>0)
    {
        cin>>c;
        cin>>m;
        for(i=1;i<=c;i++)
        {
            cin>>coins[i];
        }
        for(i=1;i<=c;i++)
        {
            cin>>testcases[i];
        }
        m1=*max_element(testcases.begin(),testcases.end());
        for(i=0;i<1000;i++)
        {
            answers[i]=-1;
        }


            i=0;
            while(m1>=i)
            {
                i++;
                flag=0;

            for(int j=1;j<=c;j++)
            {
                if(i-coins[j]>=0)
                {
                    storage[j]=answers[i-coins[j]];
                    flag=1;
                }
                else
                storage[j]=-2;

            }
            if(flag==1)
            {answers[i]=*min_element(begin(storage), end(storage),
    [](int t1, int t2) {return t1 > 0 && (t2 <= 0 || t1 < t2);});

            flag=0;
            }
            else
                answers[i]=0;


            }

            if(m1==i)
            {
                for(int y=1;y<=m;y++)
                {
                    cout<<answers[testcases[y]]<<endl;
                }
            }

    }


return 0;
}

EDIT: By "not working" I mean its actually doesnt do anything.Its takes input and does nothing.I think it goes into an infinite loop.

LoveMeow
  • 3,858
  • 9
  • 44
  • 66
  • 5
    "Isn't working" is not a good description. Please specify your problem. – Appleshell Oct 06 '13 at 15:13
  • To elaborate: does the program fail to compile? (if so, what error do you get from the compiler?) Does it crash (if so, how do you compile it and run it?) Does it compile and run, but produce unexpected output? If so, what is the output you're getting -- and what did you expect to get? – jalf Oct 06 '13 at 15:44
  • well it gives me nothing,it runs.but thats it.takes input and does nothing.the debiugging thing doesnt work inmy compiler. – LoveMeow Oct 06 '13 at 16:03
  • If debugging doesn't work for your compiler then get one where it does! There's various free compilers with perfectly good debuggers, Visual Studio Express for instance. – john Oct 06 '13 at 17:04
  • What is variable `c` for? You input 3 numbers without telling the user what the variables are for. What is variable `m` for? Try renaming your variables to something meaningful, such as "initial_monetary_value" and "number_of_coins". – Thomas Matthews Oct 06 '13 at 18:34
  • Why are you asking the number of coins, when that is the purpose of the assignment? The assignment: Given a monetary value, print the minimum number of coins and the coinage. – Thomas Matthews Oct 06 '13 at 18:38

2 Answers2

3

Universal solution: Run your app under debugger. Step into the code, then watch values of variables. Compare with values that you expect. Try to edit code, re-compile, and debug again. Place a breakpoints in problem places to quickly jump through the code.

I see #include "stdafx.h", that probably means that you using Visual Studio. Here is a guide:

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
2

There could be lots of things wrong with this code (I didn't test it) but one simple problem which would cause an infinite loop is this

while (n > 0)
{
    // lots of code which never changes n
}

You have an infinite loop because nowhere inside the while (n > 0) loop do you modify the value of n.

I would guess you want this

while (n > 0)
{
    // lots of code which never changes n
    --n;
}
john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you.That was definitely a mistake,but for some reason,my program behaves as if its still in an infinite loop,i rebuilt it. – LoveMeow Oct 06 '13 at 15:32