Least possible money needed for buying grain

Hey guys, I need to write a program that would find the least possible money spent for buying grain, if you know the amount of providers, the grain you need and each provider's price and quantity. I have been trying to get this to work but I am not smart enough. Someone please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
using namespace std;

int main () {
    int m, n, i, bot=0, c, x=0;
    cout<<"Enter grain needed and amount of providers accordingly"<<endl;
    cin>>m>>n;
    int *p = new int [n];
    int *q = new int [n];
    for (i=0; i<n; i++) {
        cout<<"Enter price / quantity of producer "<<i+1<<" accordingly"<<endl;
        cin>>p[i]>>q[i];
    }
    while (m>0) {
        for (i=0; i<n; i++) if (bot>p[i]) {
            c = i;
            bot = p[i];
        }
        cout<<"Lowest price is "<<p<<" from producer "<<c+1<<endl;
        while (m>0 && q[c]>0) {
            m--;
            x+=p[c];
            q[c]--;
            cout<<"Buying from cheapest provider, grain still needed: "<<m<<endl;
            cout<<"Money spent so far... "<<x<<endl;
            cout<<"Grain still remaining in this provider: "<<q[c]<<endl;
        }
        p[c]=99999;
    }
    cout<<"Total money spent: "<<x<<endl;
}
Last edited on
You have:
1
2
3
bot = 0;
// assign price to p[i], assuming that 0 < price
if ( bot > p[i] ) // if ( 0 > price ) 
Topic archived. No new replies allowed.