• Support PF! Buy your school textbooks, materials and every day products Here!

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

  • Thread starter ermines
  • Start date
  • #1
45
0
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
 

Answers and Replies

  • #2
45
0
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.
 
  • #3
HallsofIvy
Science Advisor
Homework Helper
41,833
955
Using only addition and subtraction? Interesting problem. I'll think it over but I can't promise anything.
 
  • #4
lurflurf
Homework Helper
2,432
132
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.
 
  • #5
HallsofIvy
Science Advisor
Homework Helper
41,833
955
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;
}
 
  • #6
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.
 
  • #7
45
0
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?
 
  • #8
45
0
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?
 
  • #9
siddharth
Homework Helper
Gold Member
1,127
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
 
  • #10
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
tsk, tsk, you used multiplication. :smile: What about calling your multiplication routine instead?
 
  • #11
45
0
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
 
  • #12
45
0
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;
}
 
  • #13
HallsofIvy
Science Advisor
Homework Helper
41,833
955
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.
 
  • #14
45
0
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;
}
 
  • #15
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,527
28
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:
  • #16
45
0
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?
 
  • #17
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;
        }
    }
}
 
  • #18
45
0
umm what is the modulus for? isn't it undeclared?
 
  • #19
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.
 
  • #20
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.
 
  • #21
45
0
ohhh i see. sorry for not noticing it quickly. modulus is the code for finding the remainder. wow, this is such a cool code! i didn't even thought of using such a method. thanks! :D
 
  • #22
45
0
umm guys, i just have this other question. How do i implement the "Press enter key to continue"? I researched that the ascii value of enter key is 13. so do i just do an if cin=='13' for the cin if it is equal to 13? But what would be its difference to the number 13???
 

Related Threads on Implementing Various Calculations ith only + and - in c++

Replies
2
Views
676
Replies
2
Views
1K
Replies
4
Views
2K
  • Last Post
2
Replies
33
Views
1K
Replies
1
Views
1K
  • Last Post
Replies
2
Views
2K
Top