beginner tpoic?

What are the errors? Copy and paste them for us next time.

Your binaryK function returns a vector<int>, but you never use this.
If you need to treat a vector as a C-style array, you can do:
1
2
    vector<int> vec = binaryK(k);
    modExpo(a, vec.data(), n);



Or, better, would be to pass it as a vector to modExpo:
1
2
    vector<int> vec = binaryK(k);
    modExpo(a, vec, n);



int modExpo(int a, const vector<int>& K, int n)

And remove line 28.

Line 37:
1
2
3
    if (K[0]=1){
        b=a;
    }
You meant to use ==, not = in the if-statement condition (equality check, not assignment)
Last edited on
And the errors are? You need to tell us what the errors you get so we don't have to guess.

Single character variable names are not helping you. k and K are entirely different variables, it is easy to confuse the two.
c++, case matters. K is not k. That is one of the issues.
k is not a pointer nor an array in main. It is just an int. You can take its address and make it an array with 1 location in it if you need to, but ... what are you trying to do??

in the function, you make a new variable with same name (K again) as the parameter passed in.
There are probably more, but again, what is it you are trying to do would help... and I read it, I know its modulo exponent ... the question is what K is supposed to be, a value or an array...

my modulo math is a little rusty but watch that line 44. I forget if a*a%n is the same as (a*a)%n and lack the energy to doodle it out to see.

for the new issues, post the updated code.

-- vectors are an OBJECT. (its a template class that forms a normal class of the provided type that then becomes a variable: an object). Vectors have no relationship to pointers. C style arrays and pointers are very, very similar (they are both just the starting point of a solid block of memory, and share much due to this). You can sorta kinda convert arrays to pointers and pointers act a lot like arrays. Vector is a whole new idea, and no can do**.
If you are using a pointer, you should justify that in words why. Otherwise, just use containers exclusively.

**ok, I lied. you can take an UNSAFE pointer to the address of the first element of a vector and then proceed. If the vector reallocates its memory, the pointer goes bad, though, so this is only useful if the vector is not allowed to change size while the pointer is active. It is not usually a good idea to do this, but if you are mixing c and c++ or older code you may have to do so.
Last edited on
main.cpp:70:24: error: invalid initialization of reference of type ‘const std::vector&’ from expression of type ‘int*’
70 | modExpo(a, vec.data(), n);

I was giving two different alternatives at once. Don't do vec.data() if it's expecting a vector as the parameter.

If your function looks like:
int modExpo(int a, const vector<int>& K, int n)

Then pass it in as:
1
2
    vector<int> vec = binaryK(k);
    modExpo(a, vec, n);
Last edited on
... integers are already in binary. making an array of this is redundant, but if you insist, have you considered a <bitset>?

regardless, post that updated code and lets get it working.
to get bits from an int..
unsigned int indices[] ={1,2,4,8,16,32,64,128,... big};
unsigned int foo;
foo & indices[0];//the 1's or least bit
foo &indices [4]; //the 5th bit, from zero... like an array

I can't recommend enough NOT using k and K. In many fonts these are very hard to tell apart, and that is a recipe for aggravation.
Last edited on
what is your value for K the vector look like in main after you get it from the binary function? Is it the binary for the number (write a loop, print it out).
examples: 7 is 111 (4+2+1)
10 is 1010 (8+0*4 + 2 + 0)

you also still have if = ... compare with ==
Last edited on
... in main, it be called 'vec'.
also, on top of the above, you have k being used with no value in the modulo function. HEED your compiler warnings!

I redid binary for you, and by the way, please start using code tags...

1
2
3
4
5
6
7
8
9
10
11
vector<int> binaryK(unsigned int k)
{
  std::vector<int> K(8*sizeof(int));
  for(auto &a:K)
  {
    a = k&1;
    k = k>>1;	
  }
  return K;
}


what you believe is irrelevant. Test it, print it, see if it is right or not. Then if not, fix it.
you should probably use unsigned ints everywhere. Some compilers do stupid stuff with signed bitwise activity.
Last edited on
stop guessing. debug it. if you have a debugger, use it. If not, print the variables in key locations to see if they are what you think they should be at that point.

I don't know what a random k added to make it work should be. But it needs a value, or it needs to not be there. Look at your equations/formula for what you are doing, and match it up, or remove it.

While I want you to figure it out, and learn, I am not holding back. I don't know what all is wrong with it ... I don't know the math here, and don't care to research it at this point. But a methodical debugging will get it going pretty fast; its not really that big.
Last edited on
It doesn't matter now, someone gave the OP "The Boot..."

jonnin wrote:
I forget if a*a%n is the same as (a*a)%n and lack the energy to doodle it out to see.

I am supremely lazy myself, but on occasion something like ^^ this ^^ gets me revved up to make the effort to go look.

According to cppreference's page on C++ Operator Precedence multiplication (*), division (/), and remainder (%) have the same level of precedence with an associativity of left-to-right.

Weirdly enough the C++ standard has ZIP to say about operator precedence levels, it is derived from the grammar of the language.

https://en.cppreference.com/w/cpp/language/operator_precedence
Topic archived. No new replies allowed.