C++ Project #3 Need Help With Formula

Posts 115 of 18 · Page 1 of 2
C++ Project #3 Need Help With Formula
Programming Project #3
Open Box
An open box is made from a piece of rectangular cardboard by cutting out squares of equal size from
the four corners and bending up the sides.
What size should the squares be to obtain a box with largest possible volume? The length and width
of the cardboard are given by user.
Write a C++ program that can solve the problem. Allow your program run again and again as often as
the user wishes.
For example,
 For the entered sides 16 and 30 of the cardboard, the size of the cut is 3.33 and the maximum
volume of the box is 725.93
 For the entered sides 20 and 16 of the cardboard, the size of the cut is 2.95 and the maximum
volume of the box is 420.11
Note:
Validate your input values to detect characters and negative values, which should be discarded.
Must create at least three functions:
1. To read in values entered at the keyboard,
2. To process the input values,
3. To output the required values.


i need some serious help...what formula would i have to use?
On my phone but here we go:
limitations of the cut z are 0 < z < (width<length? width/2 : length/2)
So:
float mincut = 0.01f;
float maxcut = {width<length ? width/2 -0.01f : length/2 - 0.01f );

Content of the box:
(width-2•Cut) • (length-2•cut) • cut

calc if cur content is bigger then prev, as soon as it's not it means you hit the turning point
Something u learn in middle school
You can use some nifty calculus:
Code:
V = (D)(W - (2D))(L - (2D)) ...expand
  = 4(D^3) - 2W(D^2) - 2L(D^2) + WLD   ...refactorize a section:
  = 4(D^3) - 2(W + L)(D^2) + WLD
now, W and L are constant, so derive V with respect to D

Code:
dV/dD = 12(D^2) - 4(W + L)D + WL

turning points when dV/dD = 0, solve for D.
Code:
    0 = 12(D^2) - 4(W + L)D + W
    
      = 12(D^2) - (4L + 4W)D + WL
which is a quadratic ( 0 = ax^2 + bx + c ) so you can solve using
Code:
    x = (-b +- sqrt((b^2) - 4(ac))) / 2a
so in our case:
Code:
    a = 12
    b = -(4L + 4W)
    c = WL
therefore:

    D = ((4L + 4W) + sqrt((-(4L + 4W)^2) - 4(12WL)))) / 24

or  D = ((4L + 4W) - sqrt((-(4L + 4W)^2) - 4(12WL)))) / 24
Calculate both values of D, substitute them into the original equation and find which results in a bigger vol.

In code this would be:

Code:
#include <math.h>

#define quadA(a, b, c) (float)((-(b) + sqrt(((b) * (b)) - (4 *((a) * (c))))) / ( 2 * a)) //quadratic equation (addition)
#define quadS(a, b, c) (float)((-(b) - sqrt(((b) * (b)) - (4 *((a) * (c))))) / ( 2 * a)) //quadratic equation (subtraction)
#define vol(d,w,l) (float)((d) * (w) * (l)) //simple volume macro

float OptimizeDepth(float length, float width)
{
    float L = length; //convenient shorthand
    float W = width; //...

    float DA, DS; //hold the depths
    float VA, VS; //hold the volumes

    DA = quadA(12.0f, -(4*L + 4*W), W*L);
    DS = quadS(12.0f, -(4*L + 4*W), W*L);

    VA = vol(DA, W - (2*DA), L - (2*DA));
    VS = vol(DS, W - (2*DS), L - (2*DS));

    return ( VA > VS ? DA : DS );
}
Enjoy.
Jason to the rescue. Great Job homie ;]

PS: I hate calculus ;'(
damn thank you guys i havent even taken cal yet so it was pretty hard to figure out...you guys are awesome
Y U SPOONFEED HOMEWORK D:
Quote Originally Posted by Hell_Demon View Post
Y U SPOONFEED HOMEWORK D:
I gave a whole breakdown on how I arrived to that formula n00b.
Quote Originally Posted by Jason View Post


I gave a whole breakdown on how I arrived to that formula n00b.
And Someone who isn't able to think of a way to solve it at all is going to understand it? how is he going to explain his teacher what he did?

And don't worry I knew of this method, just decided to post the somewhat more calc noob friendly version. But nonetheless I'm proud of you, you payed attention in school <3
well im trying to get some help from hassan so i can understand it better...i know im a noob at it but at least im not just trying to turn in work without understanding it...i think its pretty cool how programming works which is why im taking it...even if it takes hours to learn.
i swear computer geeks r so damn smart i work every day if it wasnt for yall then people like me wouldnt know what to do about game cheats or anything i dont mean to offend yall. yall got my props thanks for everything yall have do and did so far im trin to fing a free NX Generator for a 32 bit machine comp. im sure yall can help me out wit that if yall wouldnt mind helpin me thanks for everything so far
Quote Originally Posted by junebug22 View Post
i swear computer geeks r so damn smart i work every day if it wasnt for yall then people like me wouldnt know what to do about game cheats or anything i dont mean to offend yall. yall got my props thanks for everything yall have do and did so far im trin to fing a free NX Generator for a 32 bit machine comp. im sure yall can help me out wit that if yall wouldnt mind helpin me thanks for everything so far
Holy Shit. I lol'd so hard ! Ahahaha...

On Topic: Yes, we will help you.
NX can be generated by sending packets
of course you will go to jail and have a hefty fine
hey well i showed my proffesor and apparently using the calculus formula is too easy he wants me to find a different way >.< fml!!!
Posts 115 of 18 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?