### Struggling with forming a equation into a c++ equation that uses the values from my data file

The equation is Threshold x (number of transactions having both gum and bread)/(number of transactions having gum)

Which in numerical form is 0.30(12/3)=1.2

Threshold is = 0.30 How can I implement this equation to work for any number items in the data file ? I have provided my code,output and the data file below

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include #include #include #include #include #include #include #include using namespace std; string toLower(string s) { string data = s; std::transform(data.begin(), data.end(), data.begin(), ::tolower); return data; } string trim(string str){ stringstream trimmer; trimmer << str; str.clear(); trimmer >> str; return str; } int main() { map item_count; string line; // variable used to read a line from file int lines =0; // variable conatining the number of transactions ifstream myfile("/Users/TheKid/Library/Mobile Documents/com~apple~TextEdit/Documents/en.lproj/marketdata.txt"); if (myfile.is_open()) // checking if the file was opened { getline(myfile,line); // to ignore the first line which contains number of transactions while(getline(myfile,line)) //read the file line by line until end of file is reached { // now we are processign each line stringstream ss(line); int i; string item; int count; // ignore Transactions ID, #of Items getline(ss, item, ','); getline(ss, item, ','); count = atoi(item.c_str()); while (count-- && getline(ss, item, ',')) { item = trim(toLower(item)); // Now the item variable is containing the item name map::iterator itr = item_count.find(item); if (itr == item_count.end() ) { // means the element is not present item_count.insert(pair(item,1)); } else { // increment the count itr->second = 1 + itr->second; } } } // now traverse in the array and print entries which have count 1 cout << "unique items: " << endl; for( map::const_iterator it = item_count.begin(); it != item_count.end(); ++it ) { if(it->second == 1) { cout << it->first << endl; } } cout << endl << "Items frequencies: " << endl; for( map::const_iterator it = item_count.begin(); it != item_count.end(); ++it ) { cout << it->first << ":" << it->second << endl; } myfile.close(); //closing the file } else{ cout << "Unable to open input file." << endl; return 1; } return 0; }``````

 ```unique items: pork vegetable water Items frequencies: bread:11 chicken:2 egg:3 fork:3 fruit:3 gum:3 juice:7 milk:9 napkin:7 pork:1 salt:2 soup:3 spoon:2 turkey:3 vegetable:1 water:1 Program ended with exit code: 0```
Last edited on
what you have written shows that you can do this.

What you need to do is just create variables that can store the things you need.
create a variable to store the value for the gum/bread combo. Iterate over the data, if it has both, add something to this value. Then make a variable for has gum (hint, because gum is in both, a single loop can get both values...

for (all the items)
{
if has both, modify both total
if has gum modify gum total
}

and at the end of all that you can do the computation.

the code you used to get the frequency of each item is VERY similar to what you need to accomplish this task.

If you still can't do it, ask again, but hopefully these hints will get you there.

Last edited on
Could I take the output that shows how many times each item was bought and store that into a variable ? such as:
b= 11
c= 2
e= 3
Last edited on
yes!
one way to do this a little cleaner is an enum:

enum itemz
{
};

and a vector or array

vector<int> itemcount(max_itemz);

itemcount[egg] = 3;