CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 40

Thread: Help wit C++

  1. #16
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    the syntax for the pow function is:

    pow(x,y)




    9. double num1 = 0;
    double num2 = 0;
    double answer = 0;
    cout<<”Enter num 1 “;
    cin>>num1;
    cout<<”Enter num 2 “;
    cin>>num2;
    answer = pow(num1,3) + sqrt(num2);
    Better. He doesn't ask you to input the numbers, but I don't imagine he'll take any points off if you do.

  2. #17
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by Lindley View Post
    My reading of the problem is that you only need the last line of that (but move the declaration of answer onto that line for completeness). However, the professor probably will accept that entire thing as the answer.
    and you are referring to question 9?

  3. #18
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    Yes.

    One thing to be careful about with pow() is that it doesn't have a form which takes pow(int,int). Therefore, at least one of the arguments must be a float or double. In this case, the first argument is a double, so you're fine; for your future reference, however, it might be slightly safer to write "pow(num1,3.0)" rather than "pow(num,3)". This makes the second argument a double as well rather than an int.
    Last edited by Lindley; October 26th, 2010 at 03:14 PM.

  4. #19
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Help wit C++

    10. Write a C++ statement that assigns to the answer variable the square root of the following expression: x^2 * y^3. The x,y and answer variable have the double data type.
    10. answer = pow(x,2) * pow(y,3);
    Quote Originally Posted by Lindley View Post
    #10 is correct.
    No, 10 is wrong. it doesn't calculate the required value, it only calculates part of the problem.

  5. #20
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    Yeah, I missed that part of it.

  6. #21
    Join Date
    Aug 2010
    Posts
    47

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    5. 50 + rand() % (100 – 50 + 1)
    #5 is looking better, so far. You've generated your random number, now you just have to display it.

  7. #22
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by Ankheg View Post
    #5 is looking better, so far. You've generated your random number, now you just have to display it.
    5. srand(static_casst<int>(time(0)));
    50 + rand() % (100 – 50 + 1)
    cout << rand () << endl;

  8. #23
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help wit C++

    No, you're supposed to display the number you just came up with, not a new random number.
    Last edited by Lindley; October 27th, 2010 at 02:25 PM.

  9. #24
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by OReubens View Post
    No, 10 is wrong. it doesn't calculate the required value, it only calculates part of the problem.
    im glad that you caught that mistake

    heres what i got

    10. answer = sqrt(pow(x,2) * pow(y,3));

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    5. srand(static_casst<int>(time(0)));
    50 + rand() &#37; (100 – 50 + 1)
    cout << rand () << endl;
    Again, what's the point in (100 – 50 + 1)? Just write the number.

  11. #26
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by GCDEF View Post
    Again, what's the point in (100 – 50 + 1)? Just write the number.
    the syntax states:

    lowerbound + rand() % (upperbound - lowerbound +1)

  12. #27
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    the syntax states:

    lowerbound + rand() % (upperbound - lowerbound +1)
    I know what you're doing. You should either #define constants for upper and lower bound, or just write 51. There's no point in writing a little arithmetic expression in there.

  13. #28
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by GCDEF View Post
    I know what you're doing. You should either #define constants for upper and lower bound, or just write 51. There's no point in writing a little arithmetic expression in there.
    except for when i take a test the prof wants it that way

  14. #29
    Join Date
    Aug 2010
    Posts
    51

    Re: Help wit C++

    Quote Originally Posted by Ankheg View Post
    #5: google for "random numbers in c", I glanced at the top 3 results and 2 of them have enough information to answer this.
    #6: reread the question... you correctly figured out the square root of something in #9, just do the same sort of thing and assign it to the variable they want
    #7: yeah... start this one over... it doesn't bear anything but passing resemblance to the question
    #8: Here, I'll get you started. "a function that receives four double numbers" might look like:
    Code:
    void question_eight(double numberA, double numberB, double numberC, double numberD)
    {
     // This is a void function so doesn't need a return statement.
    }
    Now, the next thing it says is that it's going to have to return the average of the supplied numbers. So you'd pick a type of value to return (let's say a double), and replace the 'void' type with that type. There, you now have an empty function set up to take four doubles (but of course it needs to return something... and they have naming requirements that I didn't cover... and of course there's some math... and so forth).
    #9: You used the 'pow' function correctly in #10, but not here.
    #10: I don't see any square rooting being done here.
    8. double calcAverage (double, double, double, double); // prototype
    double calcAverage (double num1, double num2, double num3, double num4)
    {
    double quotient = 0.0;
    quotient = (num1 + num2 + num3 + num4) / 4;
    return quotient;
    } // end of calcAverage function

  15. #30
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help wit C++

    Quote Originally Posted by ade161 View Post
    8. double calcAverage (double, double, double, double); // prototype
    double calcAverage (double num1, double num2, double num3, double num4)
    {
    double quotient = 0.0;
    quotient = (num1 + num2 + num3 + num4) / 4;
    return quotient;
    } // end of calcAverage function
    That'll work. Don't forget the second part of the problem.

    In addition, write a statement that invokes the calcAverage function and assigns its return value to a double variable named quotient. Use the following numbers as the actual arguments: 45.67, 8.35, 125.78, 99.56.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured