I have this problem i had to do for school and im a little confused on what i should put in my first while loop for my program.

Thanks for any help pointing me in the right direction!

Heres the problem.

and heres the code i have so far.

``````#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double boardArea;
double boardLength;
double boardWidth = 0.001;
//double bVolume, bHt;

double maxVolBoxBoardLn;
double maxVolBoxBoardWd;
double maxVolBoxHt;
double maxVolBox = 0;

double boxVolume;
double sqrSide;
double SqrSide = 0.001;
double minSide;
double maxvolume = 0.0;
double maxSqrSide;

cout << "Enter the area of the box" << endl;
cin >> boardArea;

while (boardWidth <= boardArea)
{
boardWidth;
}

double minside=0;
if (boardLength < boardWidth)
minside = boardWidth;
if (boardLength > boardWidth)
minside = boardLength;

while ( 2 * SqrSide <= minSide)
{
if (maxvolume > boxVolume)
{
maxvolume = boxVolume;
maxSqrSide = SqrSide;
}
else
SqrSide += 0.001;

}

boxVolume = (boardLength - 2 * sqrSide) * (boardWidth - 2 * sqrSide) * sqrSide;

cout<<"Length: "<<boardLength<<"Width: "<<boardWidth<<endl;
cout<<"The side of the square cutout is: "<<SqrSide<<endl;

return 0;
}``````

## All 8 Replies

Try to first think out the logic of the problem without worring about coding it

template<> is right, you should think about the mathematics of the solution first. I think that you can use calculus to find the solution to this problem and just use the user's numbers in the resulting formula to generate the answer. It might be that the equations are complex and cannot be solved analytically, in which case you will have to numerically solve the equations to get the answer.

I worked on it a bit but i think theres something wrong with the syntax now.

Thanks

``````#include "stdafx.h"
#include <iostream>
using namespace std;

double boardArea;
double boardLength;
double boardWidth = 0.001;
double maxVolBoxBoardLn;
double maxVolBoxBoardWd;
double maxVolBoxHt;
double maxVolBox = 0;
double sqrSide;
double SqrSide = 0.001;
double minSide;
double maxVolume;
double maxSqrSide;
double boxVolume;

void bVolume(double boardLength, double boardwidth, double& maxVolume, double &SqrSide)
{
double minside=0;
if (boardLength < boardWidth)
minside = boardWidth;
if (boardLength > boardWidth)
minside = boardLength;
maxVolume = 0.0;
while ( 2 * SqrSide <= minSide)
{
boxVolume =  (boardLength - 2 * sqrSide) * (boardWidth - 2 * sqrSide) * sqrSide;
if (maxVolume < boxVolume)
{
maxVolume = boxVolume;
maxSqrSide = SqrSide;
}
else
SqrSide += 0.001;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Enter the area of the box" << endl;
cin >> boardArea;

while (boardWidth <= boardArea)
{
boardLength = boardArea/boardWidth;
bVolume(boardLength,boardWidth, maxVolume, SqrSide);

if(maxVolume < boxVolume)
{
boardLength = maxVolBoxBoardLn;
boardWidth = maxVolBoxBoardWd;
SqrSide = maxVolBoxHt;
}
else
boardWidth+= .001;
}
cout<<"Length: "<<boardLength<<"Width: "<<boardWidth<<endl;
cout<<"The side of the square cutout is: "<<SqrSide<<endl;
return 0;
}``````

When passing something to a function as a reference you have to reference it. You are passing a normal double to your bVolume function, but double is not double&

When passing something to a function as a reference you have to reference it. You are passing a normal double to your bVolume function, but double is not double&

What do you mean?

Hello mxrider,
You are using call by value method in bvolume function. So the values you give using boardLength,boardWidth, maxVolume, SqrSide will not be changed by bvolume though you declared those variables globally. Use call by reference(&) method

So like this?

``````#include "stdafx.h"
#include <iostream>
using namespace std;

void bVolume(double l, double w, double& v, double ss,double bv, double mv)
{

double maxVolBox = 0;
double minSide =0;

double sqrSide = 0.001;

double minside=0;
double maxSqrSide;

if (l < w)
minside = w;
if (l > w)
minside = l;
mv = 0.0;
while ( 2 * sqrSide <= minSide)
{
bv =  (l - 2 * sqrSide) * (w - 2 * sqrSide) * sqrSide;
if (mv < bv)
{
mv = bv;
maxSqrSide = sqrSide;
}
else
sqrSide += 0.001;
}
}

int _tmain(int argc, _TCHAR* argv[])
{

double boardArea;
double boardLength;
double boardWidth = 0.001;
double maxVolBoxBoardLn = 0;
double maxVolBoxBoardWd =0;
double maxVolBoxHt = 0;
double maxVolume =0;
double sqrSide = 0.001;
double boxVolume = 0;

cout << "Enter the area of the box" << endl;
cin >> boardArea;

while (boardWidth <= boardArea)
{
boardLength = boardArea/boardWidth;
bVolume(boardLength,boardWidth, maxVolume, sqrSide, boxVolume, maxVolume);

if(maxVolume < boxVolume)
{
boardLength = maxVolBoxBoardLn;
boardWidth = maxVolBoxBoardWd;
sqrSide = maxVolBoxHt;
}

else
boardWidth+= .001;
}
cout<<"Length: "<<boardLength<<"Width: "<<boardWidth<<endl;
cout<<"The side of the square cutout is: "<<sqrSide<<endl;
return 0;
}``````

When passing something to a function as a reference you have to reference it. You are passing a normal double to your bVolume function, but double is not double&

This is fine in C++. Try this:

``````#include <iostream>

void f(int &x){    x = 10;    }

int main(){
int a = 0;
std::cout << "a = " << a << std::endl;
f(a);
std::cout << "a = " << a << std::endl;
return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.