8 Replies - 417 Views - Last Post: 07 June 2012 - 06:41 PM Rate Topic: -----

#1 VTMEC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 12

Error in calculating my cost

Posted 07 June 2012 - 08:01 AM

My code is working and giving the correct values for height, length, and width but, my cost is coming out wrong for everyone. Can anyone see what I did wrong. Here is the problem statement:

A containing tank is to be constructed that will hold some given
amount of oil when filled. The shape of the tank is to be a rectangular prism (including a base)
surmounted by a rectangular pyramid. The pyramid's height is always equal to half the length of
the prism. The cost to construct the prism portion of the tank is $300 per square meter, less a
10% discount for prism surface area in excess of 300 square meters and a 15% discount for
prism surface area in excess of 500 square meters, while the cost for the pyramid portion is $400
per square meter, less a 20% discount for pyramid surface area in excess of 150 square meters
and a 40% discount for pyramid surface area in excess of 300 square meters.Write a program that, for a given tank volume and a series of prism heights, finds the optimal prism length and width (to the closest 0.1 meters) that minimizes cost for that prism height.

Here is the sample input/output:

Enter the minimum prism height (decimal number, meters) >>> 11.0
Enter the maximum prism height (decimal number, meters) >>> 11.5
Enter the tank volume (integer, cubic meters) >>> 4000
Prism Height (m) Prism Length (m) Prism Width (m) Minimum Cost ($)
---------------------------------------------------------------------------
11.0 17.0 17.0 415156.35
11.1 17.0 16.9 415142.54
11.2 16.9 16.9 415135.85
11.3 16.8 16.9 415138.10
11.4 16.8 16.8 415146.58
11.5 16.7 16.8 415162.07

The minimum cost is $415135.85.
It is achieved with a prism height of 11.2, a length of 16.9 and a width of
16.9.

Here is my code:

class Cost
{
public:
	int Cost_prism_initial;
	int Cost_pyr_initial;
	Cost()
	{
		Cost_prism_initial = 300;// Initialize the class objects.
		Cost_pyr_initial = 400;
	}
};

//  Class for storing the optimal values.
class OptimalValues
{
public:
	double optLength;
	double optWidth;
	double Min;
	OptimalValues()//  Initialize class objects.
	{
		optLength = 999999999;
		optWidth = 99999999999;
		Min = 99999999999;
	}
};

//  Prototype the function.
OptimalValues computeOptimalValues(double);

//  Declare the objects.
double SA_prism,SA_pyramid,Cost_prism,Cost_pyramid,Cost_pyr_discount1,
       Cost_pyr_discount2,Cost_prism_discount1,Cost_prism_discount2,Width,
	   Length,Height=0.0,minHeight,maxHeight,BestoptMinLength,BestHeight,BestoptMinWidth;
int Volume;
double optMin=99999999999999;

// Start the main function.
int main()
{
		//  Enter the max and min heights.
		cout << "Enter the minimum prism height (decimal number, meters) >>> ";
		cin >> minHeight;

		cout << "Enter the maximum prism height (decimal number, meters) >>> ";
		cin >> maxHeight;
	 
	    //  Enter the volume.
	    cout << "Enter the tank volume (integer, cubic meters) >>> ";
	    cin >> Volume;
		
		//  Print the header of the table.
		cout << "   Prism Height (m)   Prism Length (m)   Prism Width (m)   Minimum Cost ($)" << endl;
		cout << "---------------------------------------------------------------------------" << endl;

		//  For loop to compute the values at each height.
		for(Height = minHeight; Height<=maxHeight; Height+=0.1)
		{
			//  Instantiate the class in the main fuction.
			OptimalValues InMain;
			InMain = computeOptimalValues(Height);
			double MinLength = InMain.optLength;
			double MinWidth = InMain.optWidth;
			double Min = InMain.Min;

		 //  Print height, length, width, and cost in a table.
		cout << setiosflags(ios::fixed) << setprecision(1) << "         " << Height << "               " 
			<< setiosflags(ios::fixed) << setprecision(1) << MinLength << "              " 
			<< setiosflags(ios::fixed) << setprecision(1) << MinWidth << "          " << setiosflags(ios::fixed) 
			<< setprecision(2) << Min << endl;
		cout << endl;

		
		//  Compute the absolute min price.
		if(Min < optMin)
		{
			optMin = Min;
			BestoptMinLength = MinLength;
			BestoptMinWidth = MinWidth;
			BestHeight = Height;
		}
		}
		//  Print the absolute min cost and the size at which it occurs.
		cout << "The minimum cost is $" << setiosflags(ios::fixed) << setprecision(2) << optMin << "." << endl;
		cout << "It is achieved with a prism height of " << setiosflags(ios::fixed) << setprecision(1) << BestHeight
			<< ", a length of " << setiosflags(ios::fixed) << setprecision(1) << BestoptMinLength << " and a width of " <<endl;
		cout << setiosflags(ios::fixed) << setprecision(1) << BestoptMinWidth << "." << endl;
		return 0;//  End program
	
}

//  Function for computing the optimal values.
OptimalValues computeOptimalValues(double Height)
{
	double Cost_total=0.0;

	//  Instantiate the classes.
	Cost N1;
	OptimalValues InFunction;

	for(Length = 2.0; Length<=40.0; Length+=0.1)
	    {
	    //  Calculate width.
	    Width = Volume/(((Length*Length)/6.0)+(Height*Length));
		double Width_round = static_cast<double>(Width*10 + 0.5)/10;
	    //  Calculate surface area of the prism.
		SA_prism = (Length*Width_round) + (2*(Height*Length)) + (2*(Height*Width_round));
	 
	    //  Calculate the surface area of the pyramid.
		SA_pyramid = (Length*(sqrt(((Width_round/2)*(Width_round/2)) + ((Length/2)*(Length/2))))) + (Width_round*(sqrt(((Length/2)*(Length/2)) + ((Length/2)*(Length/2)))));
	 
	    // If/else structure for claculation of cost of the prism.
	    if (SA_prism <= 300)
	    {
	        Cost_prism = (SA_prism)*(N1.Cost_prism_initial);
	    }
	    else
	    {
	        if (SA_prism > 300 && SA_prism <= 500)
	        {
	            Cost_prism_discount1 = ((SA_prism - 300)*((0.9)*(N1.Cost_prism_initial)));
	            Cost_prism = 90000 + Cost_prism_discount1;
	        }
	        else
	        {
	            Cost_prism_discount2 = ((SA_prism - 500)*((0.85)*(N1.Cost_prism_initial)));
	            Cost_prism = 143730 + Cost_prism_discount2;
	        }
	    }
	 
	 
	    //  If/else structure for calculation of pyramid cost.
	    if (SA_pyramid <= 150)
	    {
	        Cost_pyramid = (SA_pyramid)*(N1.Cost_pyr_initial);
	    }
	    else
	    {
	        if (SA_pyramid > 150 && SA_pyramid <= 300)
	        {
	            Cost_pyr_discount1 = ((SA_pyramid - 150)*((0.80)*(N1.Cost_pyr_initial)));
	            Cost_pyramid = 60000 + Cost_pyr_discount1;
	        }
	        else
	        {
	            Cost_pyr_discount2 = ((SA_pyramid - 300)*(0.60*N1.Cost_pyr_initial));
	            Cost_pyramid = 107680 + Cost_pyr_discount2;
	        }
	    }
	 
	    //  Calculation of total cost.
	    Cost_total = Cost_prism + Cost_pyramid;
	   
		if(Cost_total < InFunction.Min)
	    {
			InFunction.Min = Cost_total;
			
	    }
		InFunction.optLength = Length - 0.1;
		InFunction.optWidth = Width + 0.1;
		if(InFunction.Min<Cost_total)
	        break;
		
		}
	return InFunction;//  Return the values to the main function.
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error in calculating my cost

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1915
  • View blog
  • Posts: 5,718
  • Joined: 05-May 12

Re: Error in calculating my cost

Posted 07 June 2012 - 08:10 AM

I'm not going to compute that by hand, so I'll ask: what is your expected output?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,283
  • Joined: 25-December 09

Re: Error in calculating my cost

Posted 07 June 2012 - 08:11 AM

You may want to start by getting rid of your global variables. When I compile your code I get the following warnings:

Quote

main.cpp||In function ‘OptimalValues computeOptimalValues(double)’:|
main.cpp|99|warning: declaration of ‘Height’ shadows a global declaration [-Wshadow]|
main.cpp|40|warning: shadowed declaration is here [-Wshadow]|


This looks like it is part of your problem. The variable Height in your function is not the same variable as the global, so any changes to this variable made in the function are lost when the function returns.

You need to learn how to properly pass variables to and from your functions, and eliminate the use of global variables.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1915
  • View blog
  • Posts: 5,718
  • Joined: 05-May 12

Re: Error in calculating my cost

Posted 07 June 2012 - 08:21 AM

What are the magic numbers on lines 122, 127, 142, and 147?
Was This Post Helpful? 0
  • +
  • -

#5 VTMEC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 12

Re: Error in calculating my cost

Posted 07 June 2012 - 08:46 AM

Jimblumberg, I have moved my variables around. Are they in the right spots now? I am new to C++ and am unsure of what you mean by properly passing the variables from the function.

class Cost
{
public:
	int Cost_prism_initial;
	int Cost_pyr_initial;
	Cost()
	{
		Cost_prism_initial = 300;// Initialize the class objects.
		Cost_pyr_initial = 400;
	}
};

//  Class for storing the optimal values.
class OptimalValues
{
public:
	double optLength;
	double optWidth;
	double Min;
	OptimalValues()//  Initialize class objects.
	{
		optLength = 999999999;
		optWidth = 99999999999;
		Min = 99999999999;
	}
};

//  Prototype the function.
OptimalValues computeOptimalValues(double);

//  Declare the objects.
double SA_prism,SA_pyramid,Cost_prism,Cost_pyramid,Cost_pyr_discount1,
		Cost_pyr_discount2,Cost_prism_discount1,Cost_prism_discount2,Width,Length;
int Volume;
double optMin=99999999999999;

// Start the main function.
int main()
{
		double Height=0.0,minHeight,maxHeight,BestoptMinLength,BestHeight,BestoptMinWidth;
		
		//  Enter the max and min heights.
		cout << "Enter the minimum prism height (decimal number, meters) >>> ";
		cin >> minHeight;

		cout << "Enter the maximum prism height (decimal number, meters) >>> ";
		cin >> maxHeight;
	 
	    //  Enter the volume.
	    cout << "Enter the tank volume (integer, cubic meters) >>> ";
	    cin >> Volume;
		
		//  Print the header of the table.
		cout << "   Prism Height (m)   Prism Length (m)   Prism Width (m)   Minimum Cost ($)" << endl;
		cout << "---------------------------------------------------------------------------" << endl;

		//  For loop to compute the values at each height.
		for(Height = minHeight; Height<=maxHeight; Height+=0.1)
		{
			//  Instantiate the class in the main fuction.
			OptimalValues InMain;
			InMain = computeOptimalValues(Height);
			double MinLength = InMain.optLength;
			double MinWidth = InMain.optWidth;
			double Min = InMain.Min;

		 //  Print height, length, width, and cost in a table.
		cout << setiosflags(ios::fixed) << setprecision(1) << "         " << Height << "               " 
			<< setiosflags(ios::fixed) << setprecision(1) << MinLength << "              " 
			<< setiosflags(ios::fixed) << setprecision(1) << MinWidth << "          " << setiosflags(ios::fixed) 
			<< setprecision(2) << Min << endl;
		cout << endl;

		
		//  Compute the absolute min price.
		if(Min < optMin)
		{
			optMin = Min;
			BestoptMinLength = MinLength;
			BestoptMinWidth = MinWidth;
			BestHeight = Height;
		}
		}
		//  Print the absolute min cost and the size at which it occurs.
		cout << "The minimum cost is $" << setiosflags(ios::fixed) << setprecision(2) << optMin << "." << endl;
		cout << "It is achieved with a prism height of " << setiosflags(ios::fixed) << setprecision(1) << BestHeight
			<< ", a length of " << setiosflags(ios::fixed) << setprecision(1) << BestoptMinLength << " and a width of " <<endl;
		cout << setiosflags(ios::fixed) << setprecision(1) << BestoptMinWidth << "." << endl;
		return 0;//  End program
	
}

//  Function for computing the optimal values.
OptimalValues computeOptimalValues(double Height)
{
	double Cost_total=0.0;
	//  Instantiate the classes.
	Cost N1;
	OptimalValues InFunction;

	for(Length = 2.0; Length<=40.0; Length+=0.1)
	    {
	    //  Calculate width.
	    Width = Volume/(((Length*Length)/6.0)+(Height*Length));
		double Width_round = static_cast<double>((Width*10 + 0.5)/10);
	    //  Calculate surface area of the prism.
		SA_prism = (Length*Width_round) + (2*(Height*Length)) + (2*(Height*Width_round));
	 
	    //  Calculate the surface area of the pyramid.
		SA_pyramid = (Length*(sqrt(((Width_round/2)*(Width_round/2)) + ((Length/2)*(Length/2))))) + (Width_round*(sqrt(((Length/2)*(Length/2)) + ((Length/2)*(Length/2)))));
	 
	    // If/else structure for claculation of cost of the prism.
	    if (SA_prism <= 300)
	    {
	        Cost_prism = (SA_prism)*(N1.Cost_prism_initial);
	    }
	    else
	    {
	        if (SA_prism > 300 && SA_prism <= 500)
	        {
	            Cost_prism_discount1 = ((SA_prism - 300)*((0.9)*(N1.Cost_prism_initial)));
	            Cost_prism = 90000 + Cost_prism_discount1;
	        }
	        else
	        {
	            Cost_prism_discount2 = ((SA_prism - 500)*((0.85)*(N1.Cost_prism_initial)));
	            Cost_prism = 143730 + Cost_prism_discount2;
	        }
	    }
	 
	 
	    //  If/else structure for calculation of pyramid cost.
	    if (SA_pyramid <= 150)
	    {
	        Cost_pyramid = (SA_pyramid)*(N1.Cost_pyr_initial);
	    }
	    else
	    {
	        if (SA_pyramid > 150 && SA_pyramid <= 300)
	        {
	            Cost_pyr_discount1 = ((SA_pyramid - 150)*((0.80)*(N1.Cost_pyr_initial)));
	            Cost_pyramid = 60000 + Cost_pyr_discount1;
	        }
	        else
	        {
	            Cost_pyr_discount2 = ((SA_pyramid - 300)*(0.60*N1.Cost_pyr_initial));
	            Cost_pyramid = 107680 + Cost_pyr_discount2;
	        }
	    }
	 
	    //  Calculation of total cost.
	    Cost_total = Cost_prism + Cost_pyramid;
	   
		if(Cost_total < InFunction.Min)
	    {
			InFunction.Min = Cost_total;
			
	    }
		InFunction.optLength = Length - 0.1;
		InFunction.optWidth = Width + 0.1;
		if(InFunction.Min<Cost_total)
	        break;
		
		
		}
	return InFunction;//  Return the values to the main function.
}



Skydiver, the expected minimum cost output should be $415135.85 but it is coming out at $415262.05. All of my cost values are coming out high.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,283
  • Joined: 25-December 09

Re: Error in calculating my cost

Posted 07 June 2012 - 08:59 AM

Quote

I have moved my variables around. Are they in the right spots now?

In my opinion, no. You still have too many global variables,
//  Declare the objects.
double SA_prism,SA_pyramid,Cost_prism,Cost_pyramid,Cost_pyr_discount1,
		Cost_pyr_discount2,Cost_prism_discount1,Cost_prism_discount2,Width,Length;
int Volume;
double optMin=99999999999999;

// Start the main function.
int main()

In the above snippet why are you declaring the variables outside of any function (global)? Move these declarations into the proper functions where they are required.

Quote

I am new to C++ and am unsure of what you mean by properly passing the variables from the function.

You may want to read the excellent function tutorials contained in my signature.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 3048
  • View blog
  • Posts: 9,283
  • Joined: 25-December 09

Re: Error in calculating my cost

Posted 07 June 2012 - 09:07 AM

Quote

the expected minimum cost output should be $415135.85 but it is coming out at $415262.05. All of my cost values are coming out high.

Have you checked all of you intermediate calculations to find out where your program differs from your hand calculations? I suggest that you run this program through your debugger, single stepping, and evaluate the results of your hand calculations with the computer calculations to see where you are going wrong.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 977
  • View blog
  • Posts: 3,393
  • Joined: 19-February 09

Re: Error in calculating my cost

Posted 07 June 2012 - 03:41 PM

Hi, here you seem to decrement the Length because the minimum was the value in the previous iteration of the loop. You increment the Width I'm wondering if that is valid.


159	        InFunction.optLength = Length - 0.1;
160	        InFunction.optWidth = Width + 0.1;
161	        if(InFunction.Min<Cost_total)
162	            break;





If you swap the operations around would that work.

        if(InFunction.Min<Cost_total)
              break;
        InFunction.optLength = Length;
        InFunction.optWidth = Width;





Design wise, another way is to have classes for prism, pyramid and tank. Methods could calculate the surface area, calculate the cost etc.
Was This Post Helpful? 0
  • +
  • -

#9 VTMEC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 12

Re: Error in calculating my cost

Posted 07 June 2012 - 06:41 PM

Thank you guys for the help. I got my code working correctly. Thank you jimblumberg for the help with the cost problem and thank you #define your suggestion corrected my problem with the width not outputting correctly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1