any particular reason why r=p(3,1); cout<<r; spits out 1?
note ive tested z m and t only r gives one even though its the exact same code ?
what gives ?

#include <iostream>
using namespace std;
int p(int x,int y){  //supposed to be a power operation if anyone knows how to better represent powers please guide me :)
    int z; 
    z=x*y; // x times y =z
    return z;} //return z
int main(){
int r=0; //r=0;
r=p(3,1); //value of p(3,1); = R
cout<<p<<endl; //Expected 3 got 1
cout<<p(3,1); // Expected 3 got 3 ????????
}

EDIT: Nevermind i figured out i was using the variable p instead of r

Edited 6 Years Ago by avarionist: n/a

Your function does not raise to power instead multiplies x & y.

z=x*y

* is a Multiplication operator *here*.

Hint: To raise a power say 2^3, We multiply 2 thrice 2x2x2.

yeah i figured that out after i got some sleep lol but how would i go about a function for that as the ^ is bit wise operator so 2^3 returns 1 and 2^2 would return 0.

int power(int x,int y){
int z,l;
for(l=0;l<y;l++){
z=x*x;
}
return z;
}

something like that ?

Edited 6 Years Ago by avarionist: n/a

Yes something like this but this is wrong. And don't forget to return the value & refer to limits.h for the max value (INT_MAX) a signed integer can store.
For every loop count z is still same.

ok ive got the powered problem fixed its here for anyone that wants or needs it

#include <iostream>
using namespace std;


int power(int x, int y, int flip=0){
    int z,l;
    z=x;
    if(flip==0){
    for(l=0; l<y;l++){
        cout<<z<<'\n';
        z*=x;

    }
    }
    else{
        
    return z;
}

int main(){
    power(12,4);
    cin.get();
}
This question has already been answered. Start a new discussion instead.