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


1, 2, gum, bread //transaction id, # of items purchased, item1, item2,...

2, 2, gum, napkin

3, 3, fruit, bread, fork

4, 7, milk, bread, napkin, fork, juice, soup, fruit

5, 4, juice, napkin, milk, bread

6, 2, bread, spoon

7, 1, juice

8, 3, juice, napkin, milk

9, 4, juice, napkin, milk, turkey

10, 1, pork

11, 10, milk, egg, bread, napkin, spoon, chicken, water, juice, soup, fork

12, 1, egg

13, 2, bread, milk,

14, 5, gum, salt, fruit, vegetable, bread

15, 1, bread

16, 2, bread, milk

17, 3, bread, salt, egg

18, 2, milk, napkin

19, 3, turkey, juice, milk

20, 3, turkey, chicken, soup

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>

#include <map>

#include <iterator>

#include <algorithm>

#include <string>

#include <cstdlib>

#include <fstream>

#include <sstream>

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<string, int> 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<string,int>::iterator itr = item_count.find(item);
                
                if (itr == item_count.end() ) {
                    
                    // means the element is not present
                    
                    item_count.insert(pair<string,int>(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<string,int>::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<string,int>::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.

Also, this looks like the same question you were asking already, please just ask once if that is the case.
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
{
bread,gum, salt, egg, ... max_itemz
};

and a vector or array

vector<int> itemcount(max_itemz);

itemcount[egg] = 3;
itemcount[bread] = 11;
etc

this saves a little dealing with a bunch of variables, and you can add more in the enum and it will grow on recompile naturally via the max field.
Last edited on
Topic archived. No new replies allowed.