944,185 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 12764
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 24th, 2005
0

muliplying without using a * operator!

Expand Post »
Hello Everyone,

This is my first day here. I've not browsed the site yet. But I wanted to know the answer of these questions. I'd be glad if anyone of you can help me.

1.Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
2.Exchange two numbers without using a temporary variable.
3.Find if the given number is a power of 2.
4.Multiply x by 7 without using multiplication (*) operator.
Last edited by sara.rythm; Aug 24th, 2005 at 10:32 pm. Reason: spelling mistake
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sara.rythm is offline Offline
21 posts
since Aug 2005
Aug 25th, 2005
0

Re: muliplying without using a * operator!

1. could be a trick question ...
C++ Syntax (Toggle Plain Text)
  1. std::cout << "numbers from 1 to 100 and 100 to 1";

2. assuming we are talking integers, use a macro like ...
C++ Syntax (Toggle Plain Text)
  1. // macro to swap two arguments of any type
  2. #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 ...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void isPowerOf2(int x)
  6. {
  7. int y;
  8.  
  9. y = 1; // start of odd number sequence 1, 3, 5, 7 ...
  10. while(x > -1)
  11. {
  12. cout << x << endl; // test
  13. x = x - y; // subtracting a sequence of odd numbers
  14. y = y + 2; // next odd number
  15. if (x == 0)
  16. {
  17. cout << "Bingo, we have a power of 2 number" << endl;
  18. break;
  19. }
  20. else if (x < 0)
  21. cout << "Gee, this number fails the power of 2 test" << endl;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. isPowerOf2(98);
  28. isPowerOf2(81);
  29. isPowerOf2(1);
  30. isPowerOf2(-8); // test
  31.  
  32. cin.get(); // wait
  33. return EXIT_SUCCESS;
  34. }
Edit: put that in here because the tone went splenetic later in this thread.

4. Multiple addition will do ...
C++ Syntax (Toggle Plain Text)
  1. y = x+x+x+x+x+x+x;
... as long as you don't multiply by a float!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 25th, 2005
0

Re: muliplying without using a * operator!

Quote originally posted by vegaseat ...
2. use a macro like ...
C++ Syntax (Toggle Plain Text)
  1. // macro to swap two arguments of any type
  2. #define SWAP(x, y) ((x) ^= (y) ^= (x) ^= (y))
That won't work for any type.
http://www.eskimo.com/~scs/C-faq/q10.3.html
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 25th, 2005
0

Re: muliplying without using a * operator!

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 ...
C++ Syntax (Toggle Plain Text)
  1. a, b = b, a
... will do. Where a and b is any objet.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 25th, 2005
-1

Re: muliplying without using a * operator!

the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
C++ Syntax (Toggle Plain Text)
  1. if(!(numbername%2))
  2. cout<<numbername<<" is a power of 2."<<endl;
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 25th, 2005
0

Re: muliplying without using a * operator!

Thanks a lot everyone. It was quite quick. This forum seems to be good. That helped a lot. Thanks !!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sara.rythm is offline Offline
21 posts
since Aug 2005
Aug 25th, 2005
1

Re: muliplying without using a * operator!

Now you got me started, for swapping either integers or floats this will work too ...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int a = 1;
  8. int b = 7;
  9.  
  10. cout << "a = " << a << " b = " << b << endl;
  11. // swap a with b
  12. a = a + b;
  13. b = a - b;
  14. a = a - b;
  15.  
  16. cout << "a = " << a << " b = " << b << endl;
  17.  
  18. cin.get(); // wait
  19. return EXIT_SUCCESS;
  20. }
Edit: Just an interrogative alternate, this swap is actually about five times slower then the usual temp variable swap.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 25th, 2005
0

Re: muliplying without using a * operator!

Quote originally posted by vegaseat ...
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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <limits.h>
  3. using namespace std;
  4.  
  5. template<class T>
  6. swapm(T &a,T &b)
  7. {
  8. a = a + b;
  9. b = a - b;
  10. a = a - b;
  11. };
  12.  
  13. int main()
  14. {
  15. int a = 1;
  16. int b = 7;
  17.  
  18. cout << "a = " << a << " b = " << b << endl;
  19. swapm(a,b);
  20. cout << "a = " << a << " b = " << b << endl;
  21.  
  22.  
  23. float c = 1.23F;
  24. float d = 2.34F;
  25.  
  26. cout << "c = " << c << " d = " << d << endl;
  27. swapm(c,d);
  28. cout << "c = " << c << " d = " << d << endl;
  29.  
  30. cin.get(); // wait
  31. return 0;
  32. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Aug 25th, 2005
0

Re: muliplying without using a * operator!

Quote originally posted by Drowzee ...
the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
C++ Syntax (Toggle Plain Text)
  1. if(!(numbername%2))
  2. cout<<numbername<<" is a power of 2."<<endl;
Um, no. This only tells if the number is a multiple 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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. void cout_nums_and_back(int low, int high) {
  4. (high < low) || (
  5. std::cout << low << std::endl,
  6. cout_nums_and_back(low + 1, high),
  7. std::cout << low << std::endl
  8. );
  9. return;
  10. }
  11.  
  12. int main() {
  13. cout_nums_and_back(1, 100);
  14. return 0;
  15. }
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Aug 25th, 2005
0

Re: muliplying without using a * operator!

And as for #4:

C++ Syntax (Toggle Plain Text)
  1. y = x + (x << 1) + (x << 2);

also works.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: ifstream help?
Next Thread in C++ Forum Timeline: Taking address of constructors??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC