Below are two possibilities to instantiate and initialize a vector:
andCode:int pint[1234567] = {0};
Why are those two possibilities equivalent? Or they are not equivalent?Code:int pint[1234567];
pint[0] = 0;
Printable View
Below are two possibilities to instantiate and initialize a vector:
andCode:int pint[1234567] = {0};
Why are those two possibilities equivalent? Or they are not equivalent?Code:int pint[1234567];
pint[0] = 0;
Not quite. In the first example you are initializing an array (vector is the name of an STL container, so you shouldn't use that word to refer to arrays). In the second example you are declaring an array and assigning, not initializing, its first element to 0.Quote:
Originally Posted by aljodav
> How did the interview go?
Thanks for asking arjay. I think it went well. We shall see soon enough I guess...I have another one coming up soon as well.
This question is about XOR operation.
Suppose you have two functions:
And you want to compute :Code:bool FooA(/*some parameter(s)*/)
{
//
// some code, including vectors (sorry Hermit, I mean array)
//
return some_boolean_value;
}
bool FooB(/*some parameter(s)*/)
{
//
// some code, including vectors (OMG, sorry again Hermit, I mean array)
//
return some_boolean_value;
}
The problem is that your computer keyboard IS NOT keying in the '^' symbol and, for some real reason, you cannot use any other keyboard. Which of the defines below would you use to implement the logical xor:Code:bool b = FooA(/**/) ^ FooB(/**/);
Please, after answering this, buy a new keyboard.Code:#define xor1 &&1!=
#define xor2 !=0==!
#define xor3 ==1==!
#define xor4 ==0!=
#define xor5 !=0&&
bool b1 = FooA(/**/) xor1 FooB(/**/);
bool b2 = FooA(/**/) xor2 FooB(/**/);
bool b3 = FooA(/**/) xor3 FooB(/**/);
bool b4 = FooA(/**/) xor4 FooB(/**/);
bool b5 = FooA(/**/) xor5 FooB(/**/);
heh, this is a funny one:
Ok, without cheating...an xor is the same as an inverter (electronics), or a half-adder without a carry....
So I would put:
bool x = FooA();
bool y = FooB();
bool b = (x || y) && (!(x && y));
...perhaps optimizable, but clear enough....fun! hey, some of those xor's look awefully equivalent at first glance....but a first glance is all they'll get :P
Actually "!" (not) is an inverter (eg 7404 TTL chip). You can implement an inverter by using an XOR, and fixing one input at "1"/"true"/"high".Quote:
Originally Posted by andersod2
( I actually did this on a design in 1978-1979. Inverters came 6 to a chip, and XOR's 4 to a chip. I needs (6n+1) inverters, and (4n-1) XOR gates. By using the above technique I was able to eliminate one chip, which reduced the space requirements by approx 0.3 sq inches and also reduced the power draw.
[Sometimes I still miss the old days of TTL design. I remember well when it was "new technology" and was just making inroads against RTL and DTL]...
Yes! You got it right but, try this in a real program:Quote:
Originally Posted by andersod2
#define xor2 !=0==!
bool b2 = FooA(/**/) xor2 FooB(/**/);
and tell me which one is simpler. This one I got some time ago in Code Project's forum. :)
All the following functions will compile successfully... or not?
Code:int FooA(/**/)
{
int n;
// n is used here...
float& f = (float)n;
// f and n are used here...
return n;
}
int FooB(/**/)
{
int n;
// n is used here...
float& f = n;
// f and n are used here...
return n;
}
int FooC(/**/)
{
int n;
// n is used here...
float& f = *(new float(n));
// f and n are used here...
delete &f;
return n;
}
int FooD(/**/)
{
int n;
// n is used here...
float& f = float(n);
// f and n are used here...
return n;
}
As you are returning booleans, why not use the logical boolean xor operator which C++ provides:Quote:
Originally Posted by aljodav
:)Code:if(fooA() != fooB())
{
}
I wonder what direction the interview would take if you answered with either of these:Quote:
Originally Posted by aljodav
1. Use a trigraph sequence: ??'
2. ALT + 094
Do you think you'd get the job for being a smart ***? :rolleyes:
A question about templates is a valid one? If so, please, consider the following declaration:
Also this function declaration:Code:template <typename T, int depth> class FunnyOne // depth > 0, always.
{
FunnyOne<T, depth - 1> vec;
T val;
public:
FunnyOne() : val(T(10)) {}
T Get() {
return vec.Get();
}
void Set(T val) {
vec.Set(this->val + val);
}
};
template <typename T> class FunnyOne<T, 1>
{
T val;
public:
FunnyOne() : val(T(10)) {}
T Get() {
return this->val;
}
void Set(T val) {
this->val += val;
}
};
What will be the correct answer to this?Code:int GetFunnyValue(int n)
{
FunnyOne<int, n> funny;
funny.Set(0);
return funny.Get();
}
Code:int answer = GetFunnyValue(10);
Only compile-time constants may be used as template parameters, so this won't compile.Quote:
Originally Posted by aljodav
Change that to:
... and I guess the answer would be 100.Code:template <int n>
int GetFunnyValue()
{
FunnyOne<int, n> funny;
funny.Set(0);
return funny.Get();
}
// ...
int answer = GetFunnyValue<10>();
So simple that I'm speechless, but before that, let me say that I intend that one of the defines be used. ;)Quote:
Originally Posted by Zaccheus
My deer :rolleyes: , I'd rather let such a job be taken by a smart *** as you :rolleyes: . Maybe, you don't like me but **** **** ;) .Quote:
Originally Posted by Plasmator
Correct! But you may notice that I haven't used any vector (OMG! sorry again, I mean array!)Quote:
Originally Posted by Hermit
You've just got a job! ;)Quote:
Originally Posted by Hermit