1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jul 9, 2005 #1
    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
     
  2. jcsd
  3. Jul 9, 2005 #2
    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.
     
  4. Jul 9, 2005 #3

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    Using only addition and subtraction? Interesting problem. I'll think it over but I can't promise anything.
     
  5. Jul 9, 2005 #4

    lurflurf

    User Avatar
    Homework Helper

    Just regular adds and subtracts, or bit level also? Like break numbers up into individual bits.
     
  6. Jul 9, 2005 #5

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    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;
    }
     
  7. Jul 10, 2005 #6

    es

    User Avatar

    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.
     
  8. Jul 10, 2005 #7
    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?
     
  9. Jul 11, 2005 #8
    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?
     
  10. Jul 12, 2005 #9

    siddharth

    User Avatar
    Homework Helper
    Gold Member

    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
     
  11. Jul 12, 2005 #10

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    tsk, tsk, you used multiplication. :smile: What about calling your multiplication routine instead?
     
  12. Jul 13, 2005 #11
    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
     
  13. Jul 14, 2005 #12
    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;
    }
     
  14. Jul 14, 2005 #13

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  15. Jul 15, 2005 #14
    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;
    }
     
  16. Jul 15, 2005 #15

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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: Jul 15, 2005
  17. Jul 15, 2005 #16
    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?
     
  18. Jul 16, 2005 #17
    This may not be the most efficient method, but try this:

    Code (Text):

    for (int i = 1; i <= 100; i++) {
        for (int j = 2; j < i; j++) {
            if (Modulus(i, j) == 0) {
                cout << i << endl;
                break;
            }
        }
    }
     
     
  19. Jul 16, 2005 #18
    umm what is the modulus for? isn't it undeclared?
     
  20. Jul 16, 2005 #19
    Whoops I meant this:

    Code (Text):

    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.
     
  21. Jul 16, 2005 #20
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Implementing Various Calculations ith only + and - in c++
Loading...