1

Here is my function. I am wondering why I cant perform simple math operations on the two int variables

int sockMerchant(int n, vector<int> ar) {
int pairs;

for (int i = 1; i <= 100 ; i++) { //for every number ("Color") between 1 and 100
    for (int j = 0; j < n; j++) { //iterate thru array to look for it
        if (ar[j] == i) { //once found,
            for (int k = j; k < n ; k++) { //count how many of that number is there
                if (ar[k] == i) {
                    int count;
                    count++;
                }
            }
        count = (count/2);
        pairs = (pairs+count);
        }
    }
}
return pairs;
}

Here is the error recieved:

solution.cc: In function ‘int sockMerchant(int, std::vector<int>)’:
solution.cc:20:27: error: invalid operands of types ‘<unresolved overloaded 
function type>’ and ‘int’ to binary ‘operator/’
         count = (count/2);
                  ~~~~~^~
solution.cc:21:27: error: invalid operands of types ‘int’ and ‘<unresolved 
overloaded function type>’ to binary ‘operator+’
         pairs = (pairs+count);
                  ~~~~~^~~~~~
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56

3 Answers3

1

In count = (count/2) and pairs = (pairs+count), you aren't referring to the int you declared because it isn't in scope. You're actually referring to the function std::count because you have using namespace std; somewhere in that compilation unit. This is one of the reasons you shouldn't do that.

To fix this you need to declare count in the appropriate scope:

int sockMerchant(int n, vector<int> ar) {
    int pairs;
    int count = 0;

    for (int i = 1; i <= 100 ; i++) { //for every number ("Color") between 1 and 100
        for (int j = 0; j < n; j++) { //iterate thru array to look for it
            if (ar[j] == i) { //once found,
                for (int k = j; k < n ; k++) { //count how many of that number is there
                    if (ar[k] == i) {
                        count++;
                    }
                }
            count = (count/2);
            pairs = (pairs+count);
            }
        }
    }
    return pairs;
}
Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
0

Place count outside of the for loops, that should solve it.

QUIET
  • 1
  • 1
  • Hello, welcome to Stackoverflow! While your answer is correct and is insightful, it would help if you gave an example on how the OP may solve their problem. Moreover, explaining why moving count outside the loops solves the problem would also improve the quality of your answer. – Nick Parsons Sep 23 '18 at 03:46
0

First off, count was defined in a scope without a value. You then proceed to use it in an expression: count++;

Not only is this undefined behaviour, but count doesn't even exist after the if(ar[k] == i) scope!

Then you decide to use count, and variable that isn't even in the current scope, in another expression: count = (count/2);

TIP: Don't use integers in division. The value gets truncated.

And finally, you use pairs, which has not been initialized in an expression using itself with the previously mentioned undeclared count: pairs = (pairs + count);

TL;DR: Neither count nor pairs are used correctly. Make sure to initialize both of them to some value, in this case 0, and set them at the appropriate scope.

absoluteAquarian
  • 510
  • 3
  • 12