multiply using << several times

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

multiply using << several times

 
0
  #1
May 26th, 2009
the question reads as follows....

show how u can efficiently multiply an integer that is read from the keyboard by 100, without using the operator *. use the << operator several times....

ok i can do this using overload.... or thats the only way i can think of... or is it actually possible to use << to muliply the number.


my code so far... (just to get the number)


  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. int num;
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. cout << "Enter a number from: ";
  11. cin >> num;
  12.  
  13. system("PAUSE");
  14. return 0;
  15. }

oh and thanx
Last edited by phoenix911; May 26th, 2009 at 10:25 am. Reason: 4got to say thanx :/
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 29
Reputation: phoenix911 is an unknown quantity at this point 
Solved Threads: 0
phoenix911 phoenix911 is offline Offline
Light Poster

Re: multiply using << several times

 
1
  #2
May 26th, 2009
im not thinking at all.... all i can do is say

  1.  
  2. cout << num << "00";

or is there a way to multiply with <<??
Last edited by phoenix911; May 26th, 2009 at 10:27 am. Reason: was a bit slow
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: multiply using << several times

 
1
  #3
May 26th, 2009
Use the shift operators.

http://www.learncpp.com/cpp-tutorial...ise-operators/

Multiplying by a factor of 2 is much easier than multiplying by 100. Here's a basic example of how to use shift operators.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. int a = 12; // a = 12
  7. cout << a << endl;
  8. a = a >> 1; // a = 12 / 2 = 6
  9. cout << a << endl;
  10. a = a << 3; // a = 6 * 8 = 48
  11. cout << a << endl;
  12. cin.get ();
  13. return 0;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: multiply using << several times

 
0
  #4
May 26th, 2009
Originally Posted by phoenix911 View Post
im not thinking at all.... all i can do is say

  1.  
  2. cout << num << "00";

or is there a way to multiply with <<??
haha, that's thinking outside the box. Yeah, that'll work for "multiplying" by 100 but if you want to change the multiplier to 5 then you're boned. This made me laugh pretty hard.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,604
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: multiply using << several times

 
1
  #5
May 26th, 2009
use the additive property:

(a * m) + (a * n) = a * (m + n)

example:
  1. result = (a << 2) + (a << 4);
  2. // result now contains the sum, 4a + 16a = 20a
  3.  
  4. cout << a << " times 20 = " << result << endl;


.
Last edited by jephthah; May 26th, 2009 at 3:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,604
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: multiply using << several times

 
1
  #6
May 26th, 2009
er.. i mean "distributive property"

thanks Dave
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: jloundy has a little shameless behaviour in the past 
Solved Threads: 0
jloundy jloundy is offline Offline
Newbie Poster

Re: multiply using << several times

 
0
  #7
May 27th, 2009
are you allowed to just divide by .01

  1. int num;
  2. cin>>num;
  3. int answer / .01;
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: multiply using << several times

 
0
  #8
May 27th, 2009
Originally Posted by jloundy View Post
are you allowed to just divide by .01

int num;
cin>>num;
int answer / .01;  // huh?
Originally Posted by phoenix911 View Post
the question reads as follows....

show how u can efficiently multiply an integer that is read from the keyboard by 100, without using the operator *. use the << operator several times....
The << operator is required. What happened to the OP anyway?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: jloundy has a little shameless behaviour in the past 
Solved Threads: 0
jloundy jloundy is offline Offline
Newbie Poster

Re: multiply using << several times

 
0
  #9
May 27th, 2009
i meant int answer = num/.01;
same thing as multiplying by 100,
but i guess u have to use << so im wrong again
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,604
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: multiply using << several times

 
0
  #10
May 28th, 2009
Originally Posted by jloundy View Post
i meant int answer = num/.01;
same thing as multiplying by 100,
but i guess u have to use << so im wrong again
that's not the only reason why you're wrong.

mainly you're wrong because you can't divide an int by a float and expect to get a correct integer result.

go on try it. you might actually learn something.


.
Last edited by jephthah; May 28th, 2009 at 11:43 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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