Evaluating Expressions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeyke
    New Member
    • Feb 2010
    • 15

    Evaluating Expressions

    How do I go about writing a program that finds and prints the values of result for all even values of y from 2 to 16, using the following formula:
    result= (y-1) + (y+4)3
    y -
    1
    y

    * the 3 is power to 3
    *the denominator means y minus 1 over y, just couldn't get it to stay on the same line.
    I just don't know where to start. please help!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Code:
                            3
               (y-1) + (y+4)
    result=    --------------
                 (y-1) / y
    You start by working out how to write a loop that will iterate through the even integers 2 - 16. You look in math.h or a reference book for functions that are going to be useful (pow I suggest) and you code your equation probably not on a single line of code.

    Remember that for all y > 0 (y-1) / y in integer arithmetic is 0
    also remember that divide by (y-1) / y is the same as multiply by y / (y-1)
    also remember that for all y > 2 y / (y-1) in integer arithmetic is 1

    and finally remember that for multiply by y / (y-1) it is sometimes better to multiply by y then divide by (y-1) rather than calculating y / (y-1)

    Comment

    • joeyke
      New Member
      • Feb 2010
      • 15

      #3
      this is what i did so far but i know i'm totally off

      #include <cmath>
      #include <iostream>
      using namespace std;
      int main()
      {

      double y;

      cout<<" Enter an value for y: ";
      cin>>y;

      double check;
      check=(y>0)+((y-1) / y))+((y > 2 y / (y-1));
      cout<<" y= ";
      cin>>y;

      if (check>0)
      {
      y=(y........... .);


      system ("pause");
      return 0;
      //i left the y=(y.......); blank because i have no idea what to do there!

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Go back and reread Banfa`s post
        You need a for loop to iterate through the even numbers b/n 2 and 16. Also to use the pow() function from the <cmath>header .
        double a = double pow(double(y+4) ,double 3) or a=pow(y+4,3)
        Of course y+4 cubed is nothing more than (y+4)*(y+4)*(y+ 4).
        I would suggest you leave your checks out until later and use if statements to control y values like if(y=0)y=1;

        Comment

        Working...