1. could be a trick question ...
std::cout << "numbers from 1 to 100 and 100 to 1";
2. assuming we are talking integers, use a macro like ...
// macro to swap two arguments of any type
#define SWAP(x, y) ((x) ^= (y) ^= (x) ^= (y))
Edit: Eating crow again. Sorry, change the "of any type" to "integers only"!
3. assuming we are talking integers, whittle the number down with a series of odd integers (1,3,5,7,...) until a zero match is reached ...
#include <iostream>
using namespace std;
void isPowerOf2(int x)
{
int y;
y = 1; // start of odd number sequence 1, 3, 5, 7 ...
while(x > -1)
{
cout << x << endl; // test
x = x - y; // subtracting a sequence of odd numbers
y = y + 2; // next odd number
if (x == 0)
{
cout << "Bingo, we have a power of 2 number" << endl;
break;
}
else if (x < 0)
cout << "Gee, this number fails the power of 2 test" << endl;
}
}
int main()
{
isPowerOf2(98);
isPowerOf2(81);
isPowerOf2(1);
isPowerOf2(-8); // test
cin.get(); // wait
return EXIT_SUCCESS;
}
Edit: put that in here because the tone went splenetic later in this thread.
4. Multiple addition will do ...
y = x+x+x+x+x+x+x;
... as long as you don't multiply by a float!
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Ouch, I have forgotten how meticulous C is. Sorry, change the "of any type" to "integers only".
May I kindly suggest you start learning Python, then a simple ...
a, b = b, a
... will do. Where a and b is any objet.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
Now you got me started, for swapping either integers or floats this will work too ...
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 7;
cout << "a = " << a << " b = " << b << endl;
// swap a with b
a = a + b;
b = a - b;
a = a - b;
cout << "a = " << a << " b = " << b << endl;
cin.get(); // wait
return EXIT_SUCCESS;
}
Edit: Just an interrogative alternate, this swap is actually about five times slower then the usual temp variable swap.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
Now you got me started, for swapping either integers or floats this will work too ...
Starting with your example, you could use a c++ template.
#include <iostream>
#include <limits.h>
using namespace std;
template<class T>
swapm(T &a,T &b)
{
a = a + b;
b = a - b;
a = a - b;
};
int main()
{
int a = 1;
int b = 7;
cout << "a = " << a << " b = " << b << endl;
swapm(a,b);
cout << "a = " << a << " b = " << b << endl;
float c = 1.23F;
float d = 2.34F;
cout << "c = " << c << " d = " << d << endl;
swapm(c,d);
cout << "c = " << c << " d = " << d << endl;
cin.get(); // wait
return 0;
}
Ancient Dragon
Retired & Loving It
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
if(!(numbername%2))
cout<<numbername<<" is a power of 2."<<endl;
Um, no. This only tells if the number is amultiple of 2. For power of 2, that'd be (!(x & (x - 1))).
As for #1:
First of all, "if" is not a loop. I don't know what got this idea into your head. But let's not use 'if' anyway.
#include <iostream>
void cout_nums_and_back(int low, int high) {
(high < low) || (
std::cout << low << std::endl,
cout_nums_and_back(low + 1, high),
std::cout << low << std::endl
);
return;
}
int main() {
cout_nums_and_back(1, 100);
return 0;
}
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
And as for #4:
y = x + (x << 1) + (x << 2);
also works.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
And as for #4:
y = x + (x << 1) + (x << 2);
also works.
That is most likely the reason 7 was picked as the multiplier.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
>Sorry, change the "of any type" to "integers only".
How about changing "macro to swap two arguments of any type" to "silly macro to break everything". It's still broken because you're modifying a variable more than once between sequence points. This isn't a difficult rule to figure out, I can't see why so many people have so much trouble with it.
I would word those questions differently in an interview:
>1.Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
Translation: Expain recursion.
>2.Exchange two numbers without using a temporary variable.
Translation: Explain why exchanging two numbers without a temporary variable is the height of stupidity.
>3.Find if the given number is a power of 2.
Translation: Find if the given number is a power of 2. ;) This is actually a useful exercise in bit twiddling and the result can be used in real code.
>4.Multiply x by 7 without using multiplication (*) operator.
Translation: How would you multiply without using the (*) operator, and why would I fire you if you did it in our source base?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>
>2.Exchange two numbers without using a temporary variable.
Translation: Explain why exchanging two numbers without a temporary variable is the height of stupidity.
And why do you think it is sooo stupid (unrelated to you comments about use of macros, which I agree with)? Did you read the template I posted (not my original code)? works with integers, floats and doubles. Won't work with c++ classes or C-style strings. I think the only way they can be swapped is via temp variable.
Ancient Dragon
Retired & Loving It
30,047 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
>>>2.Exchange two numbers without using a temporary variable.
>And why do you think it is sooo stupid
My answer might be that it attempts to teach how to avoid problems of the 70's while ignoring realities of present day -- and in the process teach bad coding practice.
[edit]Much the same goes for the question about multiplication without a * operator. I did my time with a Z80, but it too was long ago.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Thank you Junior Poster sara.rythm! Thank you for bringing up these question for the fun of it. As you can see, there are two kinds of people, the creative kind that get inspired by your puzzles, and the "go by the book apparatchiks" that find most of their pleasure in cutting you and others down. Unfortunately, this is Reality 101.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
>And why do you think it is sooo stupid
Because it's useless trivia. Questions like this are basically like tricky riddles: unless you already know it, you probably won't figure it out on your own. You also won't use it in the real world. Even the smallest embedded system will have enough spare memory for a temporary variable, and if it doesn't then there are far better ways to optimize space than to restrict the flexibility of your swap. It's also dreadfully easy to "improve" the trick and break everything, as vegaseat proved. Yes, it's good to know the trick, but it's even better to know why it shouldn't be used.
>the creative kind that get inspired by your puzzles, and the "go by the book apparatchiks"
There's a time and a place for creativity. Code should be written with clarity in mind, and ancient tricks that only serve to create bugs and obfuscate things have no place there. Be creative in your algorithms and you'll be a much better programmer for it. Be creative in "cool" micro-optimization code tricks and you'll just be shrugged off as an idiot who has never worked in the real world.
I've already been inspired by these puzzles, I've already solved them, and now I'm inspired by a higher quality of puzzle. You obviously take no pride in your work, or you would try harder to learn the "book" as well as think outside the box, and when to use the best one for the job. The two are not mutually exclusive as you seem to think.
>that find most of their pleasure in cutting you and others down
You still don't get it, do you? I take no pleasure in berating you, because when you do something stupid, it means I failed to properly teach you. Now, that could be because you're too arrogant to think I could teach you something, but that still doesn't change the fact that I have to correct the people that you incorrectly "inspire".
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401