-6

I got a task from my lecture to make association program from my data mining class, and i'm using c++ in microsoft visual studio 2017 since that is the only language i understand. I'm trying to get support result but all i got is 0. i use an algorithm i got from some sites, but i can't implement it to my code because the value is 0. I think the problem is in the input data reading, the one with for(int i=0;i<n;i++).

this is my code :

#include<iostream>
#include<string>
using namespace std;

int main()
{
    float n = 5, support1 = 0, support2 = 0, support3 = 0;
    string item1, item2;
    //dataset fixed
    string tra1[5] = { "milk", "beer" , "coffee" , "sugar" , "detergen" };
    string tra2[5] = { "egg", "flour" , "milk" , "sugar" };
    string tra3[5] = { "coffee", "butter" , "cigarette" , "sugar" };
    string tra4[5] = { "doritos", "tea" , "coconut oil" , "soap" };
    string tra5[5] = { "detergen", "milk" , "sugar" , "coca cola" };

    cout << "item 1 : "; cin >> item1;//for example coffee
    cout << "item 2 : "; cin >> item2;//for example sugar
    cout << endl << "------------------------------" << endl;

    //i think this is where the problem is
    for (int i = 0;i < n;i++)
    {
        //tra1
        if (item1 == tra1[5]) { support1 + 1; }
        if (item2 == tra1[5]) { support2 + 1; }
        if (item1 == tra1[5] && item2 == tra1[5]) { support3 + 1; }

        //tra2
        if (item1 == tra2[5]) { support1 + 1; }
        if (item2 == tra2[5]) { support2 + 1; }
        if (item1 == tra2[5] && item2 == tra2[5]) { support3 + 1; }

        //tra3
        if (item1 == tra3[5]) { support1 + 1; }
        if (item2 == tra3[5]) { support2 + 1; }
        if (item1 == tra3[5] && item2 == tra3[5]) { support3 + 1; }
        //tra4
        if (item1 == tra4[5]) { support1 + 1; }
        if (item2 == tra4[5]) { support2 + 1; }
        if (item1 == tra4[5] && item2 == tra4[5]) { support3 + 1; }
        //tra5
        if (item1 == tra5[5]) { support1 + 1; }
        if (item2 == tra5[5]) { support2 + 1; }
        else if (item1 == tra1[5] && item2 == tra5[5]) { support3 + 1; }
    }

    //print how many times are coffee and sugar purchased
    cout << "Transaction done " << item1 << " : " << support1 << endl;
    cout << "Transaction done " << item2 << " : " << support2 << endl;
    cout << "Transaction done " << item2 << " dan " << item2 << " : " << support3 << endl;
    cout << endl << "------------------------------" << endl;

    float result1,result2,result3;
    result1 = (support1 / n) * 100;
    result2 = (support2 / n) * 100;
    result3 = (support3 / n) * 100;

    cout << "Item 1 : " << item1 << "\t" << "Item 2 : " << item2 << endl;
    cout << "support " << item1 << " : " << result1 << endl;
    cout << "support " << item2 << " : " << result2 << endl;
    cout << "support " << item1 << " dan " << item2 << " : " << result3 << endl;

    return 0;
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 3
    You do know that array-indexes are *zero* based? I.e. an array of five elements will have valid indexes from `0` to `4` (inclusive). – Some programmer dude Dec 13 '17 at 19:08
  • How is your application running? It should crash with an index out of bounds exception in your for loop – CAMD_3441 Dec 13 '17 at 19:10
  • 1
    @CDVAProgrammer, C++ doesn't specify an exception for accessing an array out of bounds. – chris Dec 13 '17 at 19:11
  • @Someprogrammerdude yes, i do – Andreas Wg Dec 13 '17 at 19:12
  • @CDVAProgrammer it can run, but the output is all 0 value – Andreas Wg Dec 13 '17 at 19:13
  • 1
    Then what do you think e.g. `item1 == tra1[5]` will do? What will `item1` be compared to? Especially since your arrays are only initialized with *four* strings. It seems you could use [a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Dec 13 '17 at 19:16
  • It seems like you’re not actually incrementing the values of your support variables. I think what you’re trying to do is add to those variables, “supportx + 1” isn’t adding the values to a variable. You need to do “supportx+= 1. – CAMD_3441 Dec 13 '17 at 19:17
  • Are you compiling in debug mode? `item1 == tra1[5]` should throw. – lakeweb Dec 13 '17 at 19:19
  • Not to mention all of your support variables are initialized with value 0, which is fine, but since you’re not incrementing the support variables themselves that’s why the final result are 0. – CAMD_3441 Dec 13 '17 at 19:19

2 Answers2

0

in your code, inside the loop you keep referencing tra2[5] . I think you mean to use tra2[i] instead. The way you have it now you're only looking at one past the last item in your arrays (arrays are 0-based. Valid indices are [0-4])

Eyal Cinamon
  • 939
  • 5
  • 16
  • 2
    There is a bunch of stuff. Like what does `support1 + 1;` do? Andreas is writing blind. – lakeweb Dec 13 '17 at 19:14
  • @lakeweb what i mean is the value of support1 will be ++ in every tra1[5] that have equal values with item1 – Andreas Wg Dec 13 '17 at 19:23
  • First, there is no tra1[5]. As pointed out, you only have 0 to 4. And if you want to count, it is `++support1` .. `support1 + 1;` does nothing. Which compiler are you using? – lakeweb Dec 13 '17 at 19:27
  • @lakeweb i use visual studio. Thank you for your suggestion. It helps me a lot – Andreas Wg Dec 13 '17 at 19:57
0

2 things, you’re for loop is trying to access the 6th index of the array when it’s only initialized to have 5 indicis.

2nd the support1,2,3 variables are initialized to 0 but you never actually increment those variables, which are used as part of your final calculation.

Your for loop needs to change to have “support1 + 1” be “support1+=1”.

CAMD_3441
  • 2,514
  • 2
  • 23
  • 38
  • i already change the `tra1[5]` to `tra1[i]`, and also `support1 + 1` to `support1+=1`. but the value is still 0. But thank you for your help i really apreciate it. – Andreas Wg Dec 13 '17 at 19:32
  • i'm sorry i was wrong, it was a problem with my visual studio solution explorer. I closed it and make a new file and then I re do what you suggested, and it actually works ! thank you very much. I really appreciate your help – Andreas Wg Dec 13 '17 at 19:39