-8

get x,y from keyboard integers then do the following x=y^x , y=x where x is the initial value print the result without using another variable (only x and y allowed)

i used LOG to do it and it worked but the teacher wants a simpler way to do it(simple math).

Example Input:

X=3 Y=4

Example Output:

64 3
Community
  • 1
  • 1
Ayman IBreak
  • 3
  • 1
  • 5
  • 2
    Do You mean `printf( "%d %d\n", pow( (int) x, (int) y ), x );`? – Jacajack Sep 05 '15 at 14:20
  • 4
    How about punctuation and correct capitalisation to help people make more sense out of this question? – Christian Hackl Sep 05 '15 at 14:21
  • 2
    @Jacajack: I hope not. The question is tagged as C++, not C. – Christian Hackl Sep 05 '15 at 14:22
  • @ChristianHackl You mean using `printf` instead of `std::cout`? – Jacajack Sep 05 '15 at 14:24
  • 1
    @Jacajack: Yes, but especially the use of C-style casts. Even if you use `printf`, `static_cast` would be better. – Christian Hackl Sep 05 '15 at 14:25
  • @ChristianHackl Oh, sorry, I didn't see your edit, it's ok now :) – Jacajack Sep 05 '15 at 14:30
  • Q : Write a program that receive the value of x and y from keyboard (x and y both integer) such as : _______________ int main(){ int x ,y; _______________ then the program should calculate the following : x=y^x , y=x where x is the initial value and print the result without using another variable (only x and y allowed) – Ayman IBreak Sep 05 '15 at 14:41
  • @Jacajack look at this – Ayman IBreak Sep 05 '15 at 14:55
  • 1
    If I understand correctly, the task is to read two numbers, x and y; calculate the x:th power of y; and then print both that power and the initial value of x, all the while utilising only two variables. Is that correct? – molbdnilo Sep 05 '15 at 15:08
  • 2
    Your teacher's skills in formulating a clear problem statement seem a bit... lacking. – molbdnilo Sep 05 '15 at 15:10
  • yes thats how it must be read x/y then print x=y^x (must be stored in x) then print y=x (save the original x in y) – Ayman IBreak Sep 05 '15 at 15:11
  • @molbdnilo i hear ya :/ – Ayman IBreak Sep 05 '15 at 15:13
  • 4
    in C and C-like languages [`^` is the xor operator, **not power**](http://stackoverflow.com/q/4843304/995714) – phuclv Sep 05 '15 at 15:23
  • finished editing answer ... the problem is stated clearly enough: use integers and no more then `x,y` variables so `log,pow` are out of question ... After some thinking I approach with simple `O(n)` multiplication loop and bit manipulation to store the needed variables inside `x,y` ... – Spektre Sep 06 '15 at 08:53

2 Answers2

0

You can try the following program for your requirement.

#include <cstdio>
#include <cmath>

int main(){
    double x = 3;
    double y = 4;

    printf("y power x is = %lf\n", pow(y,x));
    y=x;
    printf("%lf", y);
}

Which will output:

y power x is = 64.000000
3.000000
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Maruthi Srinivas
  • 562
  • 2
  • 10
  • 22
  • 2
    1) indent the code. 2) In C++ it's `#include `, not math.h. 3) `return` is not a function, do not use it like a function – phuclv Sep 05 '15 at 15:24
  • 1
    it's giving me an error and i guess it must be stored in x not only print it as i said i used log and it worked but the teacher wants it to be simpler – Ayman IBreak Sep 05 '15 at 15:28
  • @AymanIBreak It's because Maruthi Srinivas used `printf` with `int` instead of `const char *`. Try changing it to `printf( "%d", y );` – Jacajack Sep 05 '15 at 15:30
  • 1
    Sorry for the error. It should be printf("%d", y) as said by AymanIBreak. – Maruthi Srinivas Sep 05 '15 at 15:33
  • i did so but it didn't work ||=== Build: Debug in ayman (compiler: GNU GCC Compiler) ===| C:\Users\ayman\Desktop\ayman\main.cpp||In function 'int main()':| C:\Users\ayman\Desktop\ayman\main.cpp|9|warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat]| obj\Debug\main.o||In function `main':| C:\Users\ayman\Desktop\ayman\main.cpp|3|multiple definition of `main'| obj\Debug\main.o:C:\Users\ayman\Desktop\ayman\main.cpp|3|first defined here| ||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===| – Ayman IBreak Sep 05 '15 at 15:40
  • `y` is a double. The correct format specifier for a double is `%f` or `%lf`. – Bill Lynch Sep 05 '15 at 15:46
0

I assume

  1. x,y are small enough not to overflow the integer result
  2. x,y is non negative (if negative you need to handle that too)
  3. int is 32 bit integer

I would use for loop ...

  • for is easy but you need to have iteration and result variables
  • as you have forbidden adding variables you need to encode them into unused space of x,y
  • so let limit x,y to 16 bits ... <0,65535>
  • 0x0000FFFF*0x0000FFFF=0xFFFE0001
  • so you need to take care only the cases x<=2 or |y|<=1 all the other cases are OK
  • let lower part be the x,y and higher i,z

in source it would looks something like this (do not read if you want to code it yourself):

// for (z=1,i=0      ;    i     <    x            ;i++          ) z*=y;
   for (y|=0x00010000;int(x>>16)<int(x&0x0000FFFF);x+=0x00010000) y=((int(y>>16)*int(y&0x0000FFFF))<<16)|int(y&0x0000FFFF);
// y=x;
   y=int(y&0xFFFF0000)|int(x&0x0000FFFF);
// x=z; free i
   x=int(y>>16);
// free z
   y&=0x0000FFFF;

[notes]

  • if you need also negative values then you need to copy sign bit ...
  • during conversion from 16->32 bit for example if (int(y&0x00008000)) y|=0xFFFF0000;
  • and also during multiplication (xor the result sign bit by y sign bit)...
  • or ignore it completely and compute the result sign only from y sign and x zero bit (odd/even)
  • as result z is integer then exponent x should not be negative ...
Spektre
  • 49,595
  • 11
  • 110
  • 380