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.
#include <iostream>
usingnamespace 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 = newint [n];
int *q = newint [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;
}