Need Advice with Calculations

Hey everyone.

Last time I posted on here I got some very helpful advice and was able to finish my program :). Hopefully it'll happen again.
I'm basically stuck at the calculations part of my program.
I have to calculate the amount of concrete (in cubic meters) needed in a rectangular room, given that there is an opening in the room (i.e a doorway). Sounds easy enough.

The formula for volume for a rectangle(in cubic meters) is V = Length x Width x Height.
But I'm not interested in the total volume of the room, rather the volume of individual walls. Therefore I'm thinking my formula will be something like this:

V = Thickness x Width x Height for Wall 1
V = Thickness x Width x Length for Wall 2.
V = Thickness x Width x Height for Wall 3.
V = Thickness x Width x Length for Wall 4.
+__________________________________________
Total Volume of Walls for a rectangular room.
-
Total Volume of Opening for a rectangular room.
????
Profit

I'm hoping that there's some mathematicians out there that can help me out because there seems to be something wrong with my calculations.

Let's assume that the numbers are already given; here's my attempt at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
double length = 50;
double width = 30;
double height = 2.1;
double thickness = .05;
double opening = 25.5;
float S1 = width * height * thickness;
float S2 = length * height * thickness;
float S3 = width * height * thickness;
float S4 = length * height * thickness;
float S5 = (S1 + S2 + S3 + S4) - (opening * height * thickness);

cout << "Total amount of concrete needed:" << S5 << endl;


return 0;
}


These are the numbers that the professor uses in her example terminal output.
Her answer is 14.101
However when you compile my code you get 14.1225.
So what am I doing wrong?

If you guys need an illustration to see what I mean please go here:
http://web.cs.unlv.edu/lee/cs135/wall_sketch.pdf

And if you would like to see the full prompt please go here:
http://web.cs.unlv.edu/lee/cs135/assign04_cs135fa12.html

Thank you
Last edited on
Without testing the code :
I'll suggest that you should be using the same datatype here (ie: doubles).

The reason that you get a different answer is due to rounding errors.
@soranz
I tried it with all doubles; same answer, but you may be right, it might just be some rounding error.
Be careful not to count the intersections of the walls twice.
@TheIdeasMan

What do you mean exactly?
Is the way I'm approaching the volume of the room correct?
I just create this to help you out! I have the same class!
basically u r counting the intersections twice!! u know the corners, so what u need to do is get the volume of the four intersections and subtracted from the rest. email me to silvaj4 if u got any questions.
@jimesilva4
OHHHH I see!
It keeps counting the corners twice!
Thanks so much man :)
and Thank you @TheIdeasMan

Edit: I fucking love this forum.
Last edited on
Alright brotha if u figured it out turn this shit in before 11 59 PM!! lol
Seems as if I didn't really get it.
Can someone tell me what I'm doing wrong?
My answer to concrete keeps coming out to 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main ()
{
  double length, width, height, thickness, opening;
  float concrete;
  float labor;
  float overhead;
  int n = 14;
  int m = 30;
  const int concrete_cost = 81;
  const float labor_cost = .6;
  const float overhead_cost = .2;
  double S1 = width * height * thickness; // Volume of Wall 1.                                                                                                                                 
  double S2 = length * height * thickness; // Volume of Wall 2.                                                                                                                                
  double S3 = opening * height * thickness; // Volume of Opening.                                                                                                                              
  double S4 = height * thickness * thickness; // Volume of intersections.                                                                                                                      


  cout << "Please enter the exterior length of the wall in meters:" <<  endl;
  cin >> length;
  cout << "Please enter the width of the wall in meters:" <<  endl;
  cin >> width;
  cout << "Please enter the height of the wall in mters:" << endl;
  cin >> height;
  cout << "Please enter the thickness of the wall in centimeters:" << endl;
  cin >> thickness;
  cout << "Please enter the length of the opening in meters:" << endl;
  cin >> opening;
  cout << " \n " << endl;

  cout << "My Name     Section #1003     Assignment #4" << endl;
  cout << "---------------------------------------------------" << endl;
  cout << "JOB COST ESTIMATE" << endl;
  cout << "Wall Dimesions" << endl;
  cout << showpoint << fixed << setprecision(3);
  cout << left << setw(n) << " Length=" << right <<  setw(m) << length << " m" << endl;
  cout << left << setw(n) << " Width=" << right << setw(m) << width << " m" << endl;
  cout << left << setw(n) << " Height=" << right << setw(m) << height << " m" << endl;
  cout << left << setw(n) << " Thickness=" << right << setw(m) << thickness/100 << " m" << endl;
  cout << left << setw(n) << " Opening=" << right << setw(m) << opening << " m" << endl;
  cout << " " << endl;

  concrete = ((S1 + S2 + S1 + S2) - S3) - (S4 + S4 +S4 +S4);
  cout << "Estimated concrete needed:" << concrete << endl;
  cout << " " << endl;




  return 0;
            }
Always initialize variables.

Lines 18 to 21 are assigning uninitialized variables to the S1 - S4. You are looking to calculate S1-S4 AFTER user input - so on line 35.
Last edited on
@soranz
Oh ya lol..
Got it; thank you!
Topic archived. No new replies allowed.