code to Multiplication two numbers without using (+ , - , * )

i was have amistake in aquation >>> soory

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 12340 Views - Last Post: 10 January 2010 - 09:30 AM Rate Topic: -----

#1 ReMaS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 10

code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 03:57 AM

assalam 3laikom

peace

first i،m new here :D

and my english language is not so gooood

so please help me :wub:

i have an assignment

Write a code to Multiplication two numbers without using (+, - , * , ()) operators? ?

note : iknow this prog when useing add operator !!!

This post has been edited by ReMaS: 09 January 2010 - 12:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: code to Multiplication two numbers without using (+ , - , * )

#2 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 985
  • Joined: 30-July 08

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 06:04 AM

[rules][/rules]

Read this: http://www.dreaminco...topic140144.htm

And from the BIG yellow box you are suppose to read and not ignore when creating a thread: # We will not do your homework for you! Do not ask us to give you code!

This post has been edited by FrozenSnake: 09 January 2010 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 ReMaS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 10

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 06:09 AM

View PostFrozenSnake, on 9 Jan, 2010 - 05:04 AM, said:

[rules][/rules]

Read this: http://www.dreaminco...topic140144.htm

And from the BIG yellow box you are suppose to read and not ignore when creating a thread: # We will not do your homework for you! Do not ask us to give you code!


ok i see

make me some min to praint the code or my ideas ^^

thanks & sorry
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 06:43 AM

http://www.dreaminco...wtopic57137.htm
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 08:31 AM

I am confused by the condition:

"Do not use any mathematical operators"

What operators count as "mathematical"?

I am sure +, -, *, /, and % are on the list, but what about &, |, <<, >>, ~?

are boolean operators considered "mathematical"?

what about the assignment operator =, or +=, -=, *=, /=?

what about ++, --?

Exactly what operators are forbidden?
Was This Post Helpful? 0
  • +
  • -

#6 ReMaS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 10

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 11:12 AM

View PostNickDMax, on 9 Jan, 2010 - 07:31 AM, said:

I am confused by the condition:

"Do not use any mathematical operators"

What operators count as "mathematical"?



sorry

yes mr/mrs u r all rights

i was missen this type of condition


the quation is

Write a code to Multiplication two numbers without using (+, - , * , ()) operators?

just ^^


and sorry for confused


and i tryed and this is the result i used the div operator ^_!



#include<stido.h>
void main()
{
int x=3 , y=5 , sum;
sum = x / (1 / y);
printf("%d",sum);
}




if any body have another idea >>> i.m here ^^

and thaks for every body

Was This Post Helpful? 0
  • +
  • -

#7 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 294
  • Joined: 10-October 09

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 12:02 PM

there are actually alot of ways you could do this, but i would just create a function.

#include<stido.h>
void main()
{
int x=3 , y=5 , sum;
sum = x / (1 / y);
printf("%d",sum);
}




the "/" symbol is the mathematical operator for divide, so i think this would be wrong.
Was This Post Helpful? 0
  • +
  • -

#8 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 605
  • View blog
  • Posts: 1,868
  • Joined: 02-August 09

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 12:14 PM

This thread might help you.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 12:48 PM

WEll I was just having some fun and thought: Well if you can add, then you can multiply... so I used the info on this page here to make a binary adder... Though my adder seems to have an error if both numbers are negative... Anyway here is my adder (note that the code in main() is just to test the adder):
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

int binaryAdder(int a, int b) {
	int carry = 0;
	int value = 0;
	int place = 0;
	unsigned int A = (unsigned)a;
	unsigned int B = (unsigned)b;
	while(A!=0 || B!=0) {
		int bita = A % 2;
		int bitb = B % 2;
		int aXORb = bita ^ bitb;
		int aANDb = bita & bitb;  
		int carryANDaXORb = carry & aXORb;
		int temp = aXORb ^ carry;
		value |= temp << place++;
		carry = aANDb | carryANDaXORb;
		A >>= 1;
		B >>= 1;
	}
	value |= carry << place;
	return value;
}
		



int main() {
	srand(time(0));
	for (int i = 0; i < 100; i++) {
		int a = rand() % 1000;// * (rand()%2 ? -1: 1);
		int b = rand() % 1000;// * (rand()%2 ? -1: 1);
		int result = binaryAdder(a, b);
		cout << setw(5) << a 
			 << " + " 
			 << setw(5) << b 
			 << " = " 
			 << setw(5) << binaryAdder(a, b) 
			 << "\t| Error = " 
			 << result - (a+b) 
			 << endl;
	}
}

Was This Post Helpful? 0
  • +
  • -

#10 ReclusiveManiac  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 18-September 09

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 12:58 PM

ReMaS, I'm not sure why no one has commented on your code, but it is perfectly valid to multiply two numbers as long as you are allowed to use the / operator. Your code should work great.

x / (1 / y) = x * y
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 01:00 PM

Quote

ReMaS, I'm not sure why no one has commented on your code, but it is perfectly valid to multiply two numbers as long as you are allowed to use the / operator. Your code should work great.

x / (1 / y) = x * y


Note that in C/C++ using integers this is NOT a true statement!!!

1/y = 0

try 1.0/y
Was This Post Helpful? 0
  • +
  • -

#12 ReclusiveManiac  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 18-September 09

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 01:00 PM

Nevermind, typing the code I just realized you used parentheses which your assignment prohibited.

Edit: To NickDMax you are correct. You do need the 1.0 so the resulting number stays as a decimal. Good catch.

This post has been edited by ReclusiveManiac: 09 January 2010 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 01:02 PM

The restriction on () is pretty useless...

double temp = 1.0/y;
int result = x / temp;

Was This Post Helpful? 0
  • +
  • -

#14 ReclusiveManiac  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 98
  • Joined: 18-September 09

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 09 January 2010 - 01:06 PM

Yeah I knew there was a way you could code it without the parentheses, but I couldn't figure it out immediately. Nice job man!
Was This Post Helpful? 0
  • +
  • -

#15 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 294
  • Joined: 10-October 09

Re: code to Multiplication two numbers without using (+ , - , * )

Posted 10 January 2010 - 09:02 AM

well i don't see how this assignment would be possible without making something along the lines of a binary adder, like Nick did. But, if the division sign is allowed, then i think this assignment would be really easy, given that division is just the opposite of multiplication.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2