muliplying without using a * operator!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 21
Reputation: sara.rythm is an unknown quantity at this point 
Solved Threads: 0
sara.rythm's Avatar
sara.rythm sara.rythm is offline Offline
Newbie Poster

muliplying without using a * operator!

 
0
  #1
Aug 24th, 2005
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
Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: muliplying without using a * operator!

 
0
  #2
Aug 25th, 2005
1. could be a trick question ...
  1. std::cout << "numbers from 1 to 100 and 100 to 1";

2. assuming we are talking integers, use a macro like ...
  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 ...
  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 ...
  1. y = x+x+x+x+x+x+x;
... as long as you don't multiply by a float!
May 'the Google' be with you!
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: muliplying without using a * operator!

 
0
  #3
Aug 25th, 2005
Originally Posted by vegaseat
2. use a macro like ...
  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
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: muliplying without using a * operator!

 
0
  #4
Aug 25th, 2005
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 ...
  1. a, b = b, a
... will do. Where a and b is any objet.
May 'the Google' be with you!
Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: muliplying without using a * operator!

 
0
  #5
Aug 25th, 2005
the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
  1. if(!(numbername%2))
  2. cout<<numbername<<" is a power of 2."<<endl;
Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: sara.rythm is an unknown quantity at this point 
Solved Threads: 0
sara.rythm's Avatar
sara.rythm sara.rythm is offline Offline
Newbie Poster

Re: muliplying without using a * operator!

 
0
  #6
Aug 25th, 2005
Thanks a lot everyone. It was quite quick. This forum seems to be good. That helped a lot. Thanks !!
Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: muliplying without using a * operator!

 
0
  #7
Aug 25th, 2005
Now you got me started, for swapping either integers or floats this will work too ...
  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.
May 'the Google' be with you!
Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: muliplying without using a * operator!

 
0
  #8
Aug 25th, 2005
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.
  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. }
Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: muliplying without using a * operator!

 
0
  #9
Aug 25th, 2005
Originally Posted by Drowzee
the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
  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.

  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. }
Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: muliplying without using a * operator!

 
0
  #10
Aug 25th, 2005
And as for #4:

  1. y = x + (x << 1) + (x << 2);

also works.
Quick reply to this message  
Closed Thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC