sum of two integers

R

robin

Hi everybody
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.
 
V

Victor Bazarov

robin said:
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.

#include <iostream>
int main() {
std::cout << "Enter two integers " << std::flush;
int a, b;
std::cin >> a >> b;
std::cout << "Now enter their sum or I'll tell mom! " << std::flush;
int sum;
std::cin >> sum;
std::cout << "The sum of " << a << " and " << b << " is " << sum;
std::cout << std::endl;
}

V
 
D

Default User

robin said:
Hi everybody
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.

It puzzled you on comp.lang.c as well, earlier today. You got the
brushoff there, so you thought you'd try here?

1. Pick a language.

2. Do your own homework.





Brian
 
M

Mark P

robin said:
Hi everybody
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.

cout << "1 + 1 = 2\n";
 
S

Scott Gifford

robin said:
Hi everybody
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.

Here is one solution, which adds the two numbers given on the
command-line without directly using arithmetic or loops. The code
uses some extra variables to make it more clear what's going on.

#include <iostream>

int main(int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " a b" << std::endl;
return 1;
}
int a = atoi(argv[1]),
b = atoi(argv[2]);

char *ptr = (char *) a;
char *ptr2 = &ptr;
std::cout << a << " + " << b << " = " << (int) ptr2 << std::endl;
return 0;
}

----Scott.
 
V

Victor Bazarov

Scott said:
robin said:
Hi everybody
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.

Here is one solution, which adds the two numbers given on the
command-line without directly using arithmetic or loops. The code
uses some extra variables to make it more clear what's going on.

#include <iostream>

int main(int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " a b" << std::endl;
return 1;
}
int a = atoi(argv[1]),
b = atoi(argv[2]);

char *ptr = (char *) a;
char *ptr2 = &ptr;
std::cout << a << " + " << b << " = " << (int) ptr2 << std::endl;
return 0;
}


Bzzt. It uses what is known as "pointer *arithmetic*". Since the OP
wanted "without using ANY arithmetic", this ain't gonna fly. :)

V
 
V

Victor Bazarov

Gianni said:
There are only 10 types of people, those that understand binary and
those that don't.

I wonder if comparison should be allowed. It is a kind of subtraction,
is it not?

V
 
V

Victor Bazarov

Gianni said:
Are bitwise operators allowed ?

:)

It's a challenge, isn't it? He never mentioned that shifts weren't
allowed. But I don't think this is possible without comparing to 0
(which shouldn't be considered arithmetic).

V
 
A

Alf P. Steinbach

* Victor Bazarov:
:)

It's a challenge, isn't it? He never mentioned that shifts weren't
allowed. But I don't think this is possible without comparing to 0
(which shouldn't be considered arithmetic).

It's easy to do without comparing anything, but we shouldn't spoil the
homework.

Cheers,

- Alf
 
J

Jerry Coffin

Hi everybody
I have a problem which is puzzling me
it is the following:

Write a C++ program to find the sum of two integers without using any
arithmetic, loops, recursions nothing.

At the hardware level, everything is implemented as gates. If you want
to get creative about it, you can implement everything with only NAND
gates, but it's far easier if you give yourself the complete complement
(AND, OR, NOT, XOR). If you search for "half adder" and/or "full adder"
it should get you started.
 
O

osmium

Jerry Coffin said:
At the hardware level, everything is implemented as gates. If you want
to get creative about it, you can implement everything with only NAND
gates, but it's far easier if you give yourself the complete complement
(AND, OR, NOT, XOR). If you search for "half adder" and/or "full adder"
it should get you started.

Those are arithmetic operators in the binary number system, so it doesn't
meet the spec..
 
T

Tim Slattery

osmium said:
Those are arithmetic operators in the binary number system, so it doesn't
meet the spec..

I don't think anything does, for the general case. If you know that
one number is much bigger than the other, you can do a bitwise OR,
that would work in some cases, but definitely not all. I suspect that
whoever made this problem up has something in mind - some definition
of 'arithmetic" or something - that we don't know about.
 
M

Michael DOUBEZ

osmium a écrit :
Those are arithmetic operators in the binary number system, so it doesn't
meet the spec..

IMO Jerry Coffinis right: the point of the exercise is to re-implement
sum with bitwise operators. No need to do it with NAND, four bit
operations should be enough.

And those are not arithmetic operators in the binary number system
because it is convention that gives the meaning of a binary sequence (as
an example, bit operations cannot be applied to float because they are
meaningless or I could use Gray code ...).

Michael
 
J

Jerry Coffin

[ ... ]
Those are arithmetic operators in the binary number system, so it doesn't
meet the spec..

That's not really true -- you need to combine them to do arithmetic. If
you want to consider them arithmetic opertors, then I'd posit that the
question has no answer: if those are arithmetic, then anything that can
produce an aritmetic result is also an arithmetic operator. Since the
required result is arithmetic, you've basically said that anything can
can possibly produce the desired result is ruled out as a possibility.
 
O

osmium

Jerry Coffin said:
[ ... ]
Those are arithmetic operators in the binary number system, so it doesn't
meet the spec..

That's not really true -- you need to combine them to do arithmetic. If
you want to consider them arithmetic opertors, then I'd posit that the
question has no answer: if those are arithmetic, then anything that can
produce an aritmetic result is also an arithmetic operator. Since the
required result is arithmetic, you've basically said that anything can
can possibly produce the desired result is ruled out as a possibility.

That was my point, the question has no answer. The answer the instructor is
almost certainly looking for is the one you gave. The instructor should say
what he means, but I will note he has been paraphrased by the student.
After all, "nothing" is an awfully strong restraint.

You may recall seeing the phrase "sum, modulo two" to represent the
exclusive or operation, for just one example. .

But then, I am not a big fan of what are essentially riddles in an
introductory course, anyway. There is plenty to keep the mind occupied
without learning how to swap two variables without using a temporary
location. And so on and on and on, ad infinitum.
 
J

James Kanze

I don't think anything does, for the general case.

The question is: what is considered arithmetic? And do you
count arithmetic done in system functions? The problem is
easily solved without the use of any "arithmetic" operators, by
something like:

int
add( int i, int j )
{
std::string r( i, ' ' ) ;
r.append( j, ' ' ) ;
return r.size() ;
}

On the other hand, I'm willing to bet that the function
std::string::append() does use + somewhere in its
implementation, and almost certainly contains a loop as well, to
initialize the extra values. (I might add that I can't think of
a more inefficient way of adding two numbers.)
 
M

Michael DOUBEZ

James Kanze a écrit :
The question is: what is considered arithmetic? And do you
count arithmetic done in system functions?

If you go this way:
#include <functional>

int add( int i, int j )
{
return std::plus<int>()(i,j);
}
:)
The problem is
easily solved without the use of any "arithmetic" operators, by
something like:

int
add( int i, int j )
{
std::string r( i, ' ' ) ;
r.append( j, ' ' ) ;
return r.size() ;
}

which doesn't work for negative numbers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top