|
-
October 26th, 2010, 03:10 PM
#16
Re: Help wit C++
 Originally Posted by ade161
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.
-
October 26th, 2010, 03:10 PM
#17
Re: Help wit C++
 Originally Posted by Lindley
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?
-
October 26th, 2010, 03:11 PM
#18
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.
-
October 27th, 2010, 06:53 AM
#19
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);
 Originally Posted by Lindley
#10 is correct.
No, 10 is wrong. it doesn't calculate the required value, it only calculates part of the problem.
-
October 27th, 2010, 08:23 AM
#20
Re: Help wit C++
Yeah, I missed that part of it.
-
October 27th, 2010, 09:20 AM
#21
Re: Help wit C++
 Originally Posted by ade161
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.
-
October 27th, 2010, 02:17 PM
#22
Re: Help wit C++
 Originally Posted by Ankheg
#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;
-
October 27th, 2010, 02:19 PM
#23
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.
-
October 27th, 2010, 02:20 PM
#24
Re: Help wit C++
 Originally Posted by OReubens
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));
-
October 27th, 2010, 02:29 PM
#25
Re: Help wit C++
 Originally Posted by ade161
5. srand(static_casst<int>(time(0)));
50 + rand() % (100 – 50 + 1)
cout << rand () << endl;
Again, what's the point in (100 – 50 + 1)? Just write the number.
-
October 27th, 2010, 02:46 PM
#26
Re: Help wit C++
 Originally Posted by GCDEF
Again, what's the point in (100 – 50 + 1)? Just write the number.
the syntax states:
lowerbound + rand() % (upperbound - lowerbound +1)
-
October 27th, 2010, 02:57 PM
#27
Re: Help wit C++
 Originally Posted by ade161
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.
-
October 27th, 2010, 03:06 PM
#28
Re: Help wit C++
 Originally Posted by GCDEF
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
-
October 27th, 2010, 03:20 PM
#29
Re: Help wit C++
 Originally Posted by Ankheg
#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
-
October 27th, 2010, 03:28 PM
#30
Re: Help wit C++
 Originally Posted by ade161
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|