Implementing Various Calculations ith only + and - in c++

Guys, i'm starting to learn c++ and wanted to know hot to implement various operations using only + and -.

I'm trying to implement *, /, %, powers, and square root.

I've been experimenting with the for loop but can't seem to make it work. Any tips on how to do these?

Thanks :D
 
hmmm i've just finished the codes for *, /, %, and cube but they're all in int forms. is there a way to do this in float or double???

right now, i'm having a hard time implementing the cube, raised to any exponent and the square root.
 

HallsofIvy

Science Advisor
41,626
821
Using only addition and subtraction? Interesting problem. I'll think it over but I can't promise anything.
 

lurflurf

Homework Helper
2,410
116
ermines said:
hmmm i've just finished the codes for *, /, %, and cube but they're all in int forms. is there a way to do this in float or double???

right now, i'm having a hard time implementing the cube, raised to any exponent and the square root.
Just regular adds and subtracts, or bit level also? Like break numbers up into individual bits.
 

HallsofIvy

Science Advisor
41,626
821
I don't see any good way to do floating point arithemetic with only adding and subtracting. Square roots are going to be very difficult also since the square root of an integer is not, in general, an integer. Would the "integer part" (i.e. the largest integer less than or equal to the square root) be sufficient?

For powers such as Cube, here's what I would do

int Multiply(int x,int y)
{//returns x times y
int z= 0;
for (int i=0;i<x;i++)
z= z+ y;
return z;
}

int Square(int x)
{//returns x squared
return Multiply(x,x);
}

int Cube(int x)
{ //returns x cubed
int z= Square(x);
return Multiply(x,z);
}

int Power(int x, int n)
{ //returns x to the n power
int z= 1;
for (int i=0;i< n;i++)
z= Multiply(x,z);
return z;
}
 

es

70
0
ermines said:
I'm trying to implement *, /, %, powers, and square root.
For what type of numbers? int,float,double,...

Do you also have to handle sign,overflow,divide by zero,...
I had this for a homework assignment once as well. It's all the special cases that make it hard.
 
hmmm, thanks for the tips guys. i've finished everything, from * to exponentials, although they're in int form but handles positive and negative numbers.

My only problem now is the square root function of type int, not float. So how do i do it?
 
yes! finally finished the code for square root thanks to the help of a ertain someone.

int sqRoot (int input)
{
int b=0;

while ((b*b)<=input)
{
b+=1;
}
b-=1;

return b;
}

now, if only i can find a way to implement some codes to float. any ideas, guys?
 

siddharth

What's this?
Homework Helper
Gold Member
1,107
0
How about
double sqRoot(double input)
{
double b=0;
while((b*b)<=input)
{
b=b+.0001;
}
return b;
}

Because you are looking at an approximate value, it won't make a big difference if you subtract by .0001 after the loop
 

Hurkyl

Staff Emeritus
Science Advisor
Gold Member
14,829
14
tsk, tsk, you used multiplication. :smile: What about calling your multiplication routine instead?
 
ooooppppppsssss, you're right! i thought i had already edited it out. well, here's the revised edition. :D

int sqRoot (int input)
{
int b=0;

while (square (b)<=input)
{
b+=1;
}
b-=1;

return b;
}

umm now guys, i'm faced with a new problem. how do i implement the inverse factorial program and also a program that produces all non-prime numbers from 1-100?. of course, * and / are already allowed. i'm still thinking of it and have a rough idea of how to do it like probrably the same with the division code...Still, i need some tips and help on how to carry this out. :D
thanks again. :D
 
i've already gotten the factorial code (below) but how do i do the inverse? do i just divide the return function to one? oh, wait! is inverse factorial first getting the factorial of a # then dividing it by one or taking the inverse of every factor?

int factorial (int x)

{
int base=1;
for (int i=1; i<=x; i++)
{
base*=i;
}
return base;
}
 

HallsofIvy

Science Advisor
41,626
821
What do you mean by "the inverse factorial"? Exactly what does the problem you were given say? I suspect you mean not the reciprocal but the function that, given n!, returns n. A problem with such a "function" is that it is not defined for most numbers.
 
ohh yes, you are right! how could i have misunderstood what the teacher meant by inverse factorial. so if the input is 24, i should get 4.

here's my NON-WORKING code. how do i fix it. im a beginner, remember. Basically i wan the input to be continuously divided until it reaches 1 and the i count the iterations to give me the base of the factorial.

int inv (int input)

{
int a=0;
while (input>=1)
{
a++;
input=input/(a+1);
}
a--;
return a;
}
 

Math Is Hard

Staff Emeritus
Science Advisor
Gold Member
4,435
22
my code is pretty clunky, but here's something I was playing around with:

int main()
{
int number = 24; //the input
int div = 1;
int count = 0;

while(number > 1)
{
number = number/div;
div ++;
count ++;
}

cout << count;

return 0;
}
 
Last edited:
i already got it, i think. here's my revised code. pls check if there is more needed to be added.

int inv (int n)

{
int a=1;
if (n>=0)
{
while (n>=1)
{
a++;
n=n/a;
}
a--;
}
return a;
}

now all i need is the code for listing all non-prime numbers from 1-100 including 100. any tips?
 
35
0
ermines said:
i already got it, i think. here's my revised code. pls check if there is more needed to be added.

int inv (int n)

{
int a=1;
if (n>=0)
{
while (n>=1)
{
a++;
n=n/a;
}
a--;
}
return a;
}

now all i need is the code for listing all non-prime numbers from 1-100 including 100. any tips?
This may not be the most efficient method, but try this:

Code:
for (int i = 1; i <= 100; i++) {
    for (int j = 2; j < i; j++) {
        if (Modulus(i, j) == 0) {
            cout << i << endl;
            break;
        }
    }
}
 
umm what is the modulus for? isn't it undeclared?
 
35
0
Whoops I meant this:

Code:
for (int i = 1; i <= 100; i++) {
    for (int j = 2; j < i; j++) {
        if (i % j == 0) {
            cout << i << endl;
            break;
        }
    }
}
Basically once it finds any factor other than 1 and the number itself, then it is not a prime number and therefore stops the inner loop and moves on to the next number.
 
35
0
Also, it is apparently obvious that 1 is not prime nor composite so that is never listed. And 2 is obviously prime since there are no numbers between 1 and 2 to check for factors.
 

The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top