Valarray modulus
Jan 1, 2012 at 4:39pm UTC
Do valarrays have the modulus operator come with them?
I am just doing question 6 from Project Euler, and I wanted to try out the valarrays.
At the moment, I am testing with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <valarray>
int main(){
int ARRAY[] = {10, 220};
std::valarray<int > P_Val(H);
if (P_Val % 10 == 0)
std::cout << "It Works!" << '\n' ;
else
std::cout << "It doesn't work." << '\n' ;
return 0;
}
It doesn't seem to be working at all. g++ complains about the functions that I am making.
Just wondering if any of you had experience with this sort of thing.
Haven't tried vectors yet, either.
I don't want to write a modulus operator, but you guys can. :P
Last edited on Jan 1, 2012 at 4:43pm UTC
Jan 1, 2012 at 5:02pm UTC
Yes, valarray<int> has % defined (see below), however, it will return another valarray<int> (or one of the intermediate types I've forgotten about), comparing it to zero doesn't make sense.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <valarray>
#include <iostream>
int main() {
std::valarray<int > v(10), q(10);
for (int i=0;i!=10;++i) v[i] = i;
q = v % 3;
for (int i=0;i!=10;++i) {
std::cout << v[i] << "," << q[i] << std::endl;
}
return 0;
}
Jan 1, 2012 at 5:25pm UTC
Thank you very much, kev, you're awesome!
Topic archived. No new replies allowed.