1

Salute..

Let's see this example:

int x,y,s;
cin>>x>>y;
s=x+y;

here we have three variables for adding two values..

Can we do this just with one variable?

thanks.

7
  • Why? This sounds like a homework question. Commented Mar 7, 2011 at 19:12
  • Or a (terrible) interview question. Commented Mar 7, 2011 at 19:13
  • oh no, neither. maybe the original, but i myself just found this question :) Commented Mar 7, 2011 at 19:17
  • @BlackBear: Let us see your way :) Commented Mar 7, 2011 at 19:27
  • 4
    Would this cut it: int x[ 2 ]; cin >> x[ 0 ] >> x[ 1 ]; x[ 0 ] += x[ 1 ];? Commented Mar 7, 2011 at 19:33

8 Answers 8

6

How about zero variables?

#include <numeric>
#include <iterator>
#include <iostream>

int main()
{
  std::cout <<
    std::accumulate(
      std::istream_iterator<int>(std::cin),
      std::istream_iterator<int>(),
      0) <<
    "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

4
struct Accumulator {
    int value;
    Accumulator(): value(0) {}
    friend std::istream& operator>>(std::istream& ss, Accumulator& acc)
    { int x; ss >> x; acc.value += x; return ss; }
};

int main() {
    Accumulator a;
    std::cin >> a >> a;
    std::cout << "Total is " << a.value << "\n";
    return 0;
}

See how useful abstraction is?

Comments

3

You can cut out one by using the extraction operator twice.

int x, s = 0;
cin >> x;
s += x;
cin >> x;
s += x;

You could cut that down even more by using a single variable that's twice the size of int. I can't believe I am typing this:

long long s;
assert(sizeof(int)*2 == sizeof(long long));
cin >> *(int*)(&s);
cin >> *((int*)(&s)+1);
s = (s & 0xffffffff) + ((s >> 32) & 0xffffffff);

You're only allowed to do things like this when you absolutely need to do something like store two 32-bit values in a 64-bit register for arcane performance reasons, or the gods will smite you. In such a case you are likely not using the iostream library anyway, but there you go. I'm going to go take a shower to wash the code smell off. I might need some lye.

Comments

1

It is possible.

Very important note, int is 16 bit. The code is very, very long. There is a lot of constants. Something like this.

int x;
cin >> x;

if (x == -32768) {
    cin >> x;
    x = x - 32768; 
} else if (x == -32767) {
    cin >> x;
    x = x - 32767; 
} else ...
...
} else if (x == -1) {
    cin >> x;
    x = x - 1;
} else if (x == 0) {
    cin >> x;
    x = x;
} else if (x == 1) {
    cin >> x;
    x = x + 1;
} else ...
...
} else if (x == 32766) {
    cin >> x;
    x = x + 32766;
} else {
    cin >> x;
    x = x + 32767;
}
cout << x << endl;
return 0;

1 Comment

Come on, at least use a switch statement.
0

I know you can cut it down to 2 variables by using x+=y

You're not allowed to do anything like cin<<x<<x+=y which would be the only way I would know how to cut it down to a single variable.

Comments

0

You can't as long as that variable is an int. But why do you want to anyway?

Of course, you can get rid of s by x += y.

Comments

0
int main() {
        typedef long long int64;

        int64 x;
        cin >> ((int*)(&x))[0] >> ((int*)(&x))[1];
        x = ((int*)(&x))[0] + ((int*)(&x))[1];
        cout << x << endl;
        return 0;
}

Input:

100 250

Output

350

See yourself at ideone : http://ideone.com/2AmK0

Note: I don't know how portable this solution is. But this works with gcc-4.3.4.

Comments

0

I think the below one would work.....

#include<stdio.h>
int x=0;
int getno()
   {
     scanf("%d",&x);
     return x;
   }
int main()
   {
     x=getno()+getno();
     printf("addition of the number is = %d",x);
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.