954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

multiply using << several times

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)

#include <iostream>
#include <stdlib.h>

using namespace std;

int num;

int main(int argc, char *argv[])
{
  cout << "Enter a number from: ";
  cin >> num;
     
  system("PAUSE");	
  return 0;
}


oh and thanx

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

im not thinking at all.... all i can do is say

cout << num << "00";


or is there a way to multiply with <<??

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

Use the shift operators.

http://www.learncpp.com/cpp-tutorial/38-bitwise-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.

#include <iostream>
using namespace std;

int main ()
{
    int a = 12; // a = 12
    cout << a << endl;
    a = a >> 1; // a = 12 / 2 = 6
    cout << a << endl;
    a = a << 3; // a = 6 * 8 = 48
    cout << a << endl;
    cin.get ();
    return 0;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

im not thinking at all.... all i can do is say

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.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

use the additive property:

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

example:

result = (a << 2) + (a << 4);  
// result now contains the sum,  4a + 16a = 20a

cout << a << " times 20 = " << result << endl;

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

er.. i mean "distributive property"

thanks Dave :$

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

are you allowed to just divide by .01

int num;
cin>>num;
int answer / .01;
jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

are you allowed to just divide by .01

int num;
cin>>num;
int answer / .01;  // huh?

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?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

i meant int answer = num/.01;
same thing as multiplying by 100,
but i guess u have to use << so im wrong again

jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 
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.


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

@jephthan
>> go on try it. you might actually learn something.

COOOOOOOOOL, dont be dick dude, im a begineer.
btw sick avatar, what are you 12?

jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

If you want to multiply any number by 100
Try this :-

int result = 0;
   int x;
   cout << "Enter Any Number " << endl;
  cin>>x;
  for ( int i = 1 ; i <= (100 /2) ; i++)
 {
		result += x << 1;
		
}


if u want to multiply by n just replace 100 with n.
But BEWARE this works when n is even only.
Find by yourself how to overcome this when n is odd.

Ahmed_I
Light Poster
32 posts since Nov 2008
Reputation Points: 14
Solved Threads: 1
 
COOOOOOOOOL, dont be dick dude, im a begineer. btw sick avatar, what are you 12?

funny you didnt qualify your "solution" with the "hey i'm a beginner" disclaimer, why are you falling back on it now?

whatever, if you're a beginner, then don't use this site to give wrong advice to other beginners. a good intention is no substitute for correct code.

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

shu shu shu shut you mouth

jloundy
Newbie Poster
8 posts since May 2009
Reputation Points: 5
Solved Threads: 0
 

>shu shu shu shut you mouth
You can dish it out, but you can't take it? That's pretty sad.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

never mind. i'm done here. the correct solution has been given back on page 1.


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

For the record, Jephthah, I like your avatar, but I'm having trouble placing it. Is it Stupendousman from Calvin & Hobbes?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

yes :)

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You