Carpet calculator calculation problems. Help!

jenny_may

Member
Joined
Aug 20, 2011
Messages
7
Programming Experience
Beginner
I'm trying to make a carpet calculator that will do the following:

1) How many metres of carpet are
needed to cover a room.
2) How many rolls of carpet that
will be.
3) How much full-width carpet
will be left on the last roll.

The carpet is supplied in rolls that are 5 meters wide and 30 metres long. Any excess width is discarded. This means that rooms that are wider than 5 meters need several strips of carpet. The smallest space that will be carpeted is 1m x1m, the biggest is 20m x 20m. I only need whole meter lengths, so example if only 1.6meters of carpet were used to cover a room it would be rounded to 2 meters.

Here's the code that's giving me incorrect calculations. The number of rolls required to carpet the room seems to be the biggest miscalculation. I'm at my wits end with this problem.

VB.NET:
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim width, length As Single 'Defines variables.
        If IsValidInput(txtWidth.Text) Then
            width = txtWidth.Text
        End If
        If IsValidInput(txtLength.Text) Then
            length = txtLength.Text
        End If
        CalculateAndDisplay(width, length)
    End Sub

    Sub CalculateAndDisplay(ByVal width, ByVal length)

        Dim strips, totalLength, rolls, carpetLeft As Integer 'Defines variables.
        Const rollwidth = 5 'Sets the constant amount of the variable"
        Const rollLength = 30 'Sets the constant amount of the variable"

        strips = width / rollwidth
        If (width Mod rollwidth) > 0 Then strips = strips + length
        totalLength = strips * length
        rolls = totalLength / rollLength
        txtMetersRequired.Text = totalLength.ToString()
        txtRollsRequired.Text = rolls.ToString()
        txtWidthRemaining.Text = carpetLeft.ToString()

        If (totalLength Mod rollLength) > 0 Then
            txtMetersRequired.Text = totalLength.ToString()
            txtRollsRequired.Text = rolls.ToString()
            txtWidthRemaining.Text = carpetLeft.ToString()
        Else : carpetLeft = 0

        End If
    End Sub
 
Shouldn't
VB.NET:
If (width Mod rollwidth) > 0 Then strips = strips + length
be
VB.NET:
If (width Mod rollwidth) > 0 Then strips = strips + 1
?
 
Shouldn't
VB.NET:
If (width Mod rollwidth) > 0 Then strips = strips + length
be
VB.NET:
If (width Mod rollwidth) > 0 Then strips = strips + 1
?

Thanks for the reply. I changed it to + 1 but still the calculations are incorrect. For example. I entered a room with dimensions 9 meters width by 9 meters length and these were my outputs after pressing calculate:

Meters of carpet required: 153
Rolls required: 5
Full-width carpet remaining: 0

That is still totally wrong and I'm still at a loss for where my calculations are going wrong :(
 
Ok, I can see the problem.... you're using Integers but your calculations produce fractions.

Let's take your example of a 9 x 9 room... your code to calculate the strips by dividing the width by the rollwidth -

VB.NET:
strips = width / rollwidth

is going to produce the figure 1.8, which you're then assigning to the strips, however since that's an Integer it's going to round up to 2.... likewise your rolls object is an integer that you're assigning a potentially fractional value to, so again it'll be subject to rounding.

Working with Integers is fine for your solution, but you need to guarantee the logic behind assigning values to them... use System.Math.Floor
and System.Math.Round to make sure you know how a fraction will be handled.

Also, you're not declaring your Const object types, which you really should be doing.
 
Working with Integers is fine for your solution, but you need to guarantee the logic behind assigning values to them... use System.Math.Floor
and System.Math.Round to make sure you know how a fraction will be handled.

Also, you're not declaring your Const object types, which you really should be doing.

Thanks again for the reply! I'm a beginner at this, how would I go about implementing System.Math.Floor, System.Math.Round and declaring my Const object types in my code?
 
VB.NET:
        If (width Mod rollwidth) > 0 Then strips = strips + length

Note: (incase you don't already know) you can use += which does the same thing

strips = strips + length
strips += length 'same as above

'can also be done with other operators, -= , /=, *=



i suppose ill answer the last question as well...

how would I go about implementing System.Math.Floor, System.Math.Round and declaring my Const object types in my code?

floor and round are methods, see the links to documentation on msdn that Menthos provided.

Working with Integers is fine for your solution, but you need to guarantee the logic behind assigning values to them... use System.Math.Floor
and System.Math.Round to make sure you know how a fraction will be handled.

Also, you're not declaring your Const object types, which you really should be doing.

Example of using floor,round method:

        Dim someDouble As Double = 234.345

        someDouble = Math.Floor(someDouble)

        'result: now someDouble is
        'someDouble = 234


        Dim someDouble As Double = 234.345

        someDouble = Math.Round(someDouble, 2)

        'result: now someDouble is
        'someDouble = 234.34


also about the constants, i believe Menthos is talking about the type? so be more explicit, ex:
Const rollwidth As Integer = 5
 
Thanks for your help FlippedBeyond. Here's where I am so far. I've fixed the constants and the carpet leftover not displaying (it seems to be working now).My problem is with how many rolls arerequired. Example a small room which only uses 10 meters from the 30meter roll, the program will display in the text box that 0 rolls areused. I want to round 0 to 1 (even if only 1 meter is used I want itto display that 1 roll is required) and if more than 30 meters isused for the program to round up to the next number. Example: 31meters = 2 rolls, 63 meters = 3 rolls, 88 meters = 4 rolls and so on.What code would give me such a result? I've been playing withMath.Ceiling but I'm lost :(

VB.NET:
[COLOR=#000000][FONT=Consolas][SIZE=2]CalculateAndDisplay([/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]ByVal[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]width, [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]ByVal[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]length)[/SIZE][/FONT][/COLOR]


[COLOR=#0000ff][FONT=Consolas][SIZE=2]Dim[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]strips, totalLength, carpetLeft [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]As[/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Integer[/SIZE][/FONT][/COLOR][COLOR=#008000][FONT=Consolas][SIZE=2]'Definesvariables[/SIZE][/FONT][/COLOR]
[COLOR=#0000ff][FONT=Consolas][SIZE=2]Dim[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]rolls [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]As[/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Integer[/SIZE][/FONT][/COLOR]
[COLOR=#0000ff][FONT=Consolas][SIZE=2]Const[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]rollwidth [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]As[/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Integer[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]= 5 [/SIZE][/FONT][/COLOR][COLOR=#008000][FONT=Consolas][SIZE=2]'Setsthe constant amount of the variable"[/SIZE][/FONT][/COLOR]
[COLOR=#0000ff][FONT=Consolas][SIZE=2]Const[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]rollLength [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]As[/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Integer[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]= 30 [/SIZE][/FONT][/COLOR][COLOR=#008000][FONT=Consolas][SIZE=2]'Setsthe constant amount of the variable"[/SIZE][/FONT][/COLOR]




[COLOR=#000000]        [FONT=Consolas][SIZE=2]strips= width / rollwidth[/SIZE][/FONT][/COLOR]
[COLOR=#000000]       [FONT=Consolas][SIZE=2]totalLength= strips * length[/SIZE][/FONT][/COLOR]
      [COLOR=#0000ff][FONT=Consolas][SIZE=2]If[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]rolls > 0 [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Then[/SIZE][/FONT][/COLOR][COLOR=#2b91af][FONT=Consolas][SIZE=2]Math[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2].Ceiling(1)[/SIZE][/FONT][/COLOR]
[COLOR=#000000]      [FONT=Consolas][SIZE=2]rolls= totalLength / rollLength[/SIZE][/FONT][/COLOR]
[COLOR=#000000]       [FONT=Consolas][SIZE=2]carpetLeft= rollLength - totalLength[/SIZE][/FONT][/COLOR]
[COLOR=#000000]       [FONT=Consolas][SIZE=2]txtMetersRequired.Text= totalLength.ToString()[/SIZE][/FONT][/COLOR]
[COLOR=#000000]       [FONT=Consolas][SIZE=2]txtRollsRequired.Text= rolls.ToString()[/SIZE][/FONT][/COLOR]
[COLOR=#000000]       [FONT=Consolas][SIZE=2]txtWidthRemaining.Text= carpetLeft.ToString()[/SIZE][/FONT][/COLOR]




[COLOR=#0000ff][FONT=Consolas][SIZE=2]If[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2](totalLength [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Mod[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]rollLength) > 0 [/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Consolas][SIZE=2]Then[/SIZE][/FONT][/COLOR]
[COLOR=#000000]           [FONT=Consolas][SIZE=2]txtMetersRequired.Text= totalLength.ToString()[/SIZE][/FONT][/COLOR]
[COLOR=#000000]           [FONT=Consolas][SIZE=2]txtRollsRequired.Text= rolls.ToString()[/SIZE][/FONT][/COLOR]
[COLOR=#000000]           [FONT=Consolas][SIZE=2]txtWidthRemaining.Text= carpetLeft.ToString()[/SIZE][/FONT][/COLOR]
[COLOR=#0000ff][FONT=Consolas][SIZE=2]Else[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Consolas][SIZE=2]: carpetLeft = 0 [/SIZE][/FONT][/COLOR]
 
Last edited:
If rolls > 0 Then Math.Ceiling(1)

just a side note: the problem with this statement is that you are calling the ceiling method, however you are not storing that result anywhere... also 'rolls' will always be 0 at this point because it was never set to anything.

Thanks for your help FlippedBeyond. Here's where I am so far. I've fixed the constants and the carpet leftover not displaying (it seems to be working now).My problem is with how many rolls arerequired. Example a small room which only uses 10 meters from the 30meter roll, the program will display in the text box that 0 rolls areused. I want to round 0 to 1 (even if only 1 meter is used I want itto display that 1 roll is required) and if more than 30 meters isused for the program to round up to the next number. Example: 31meters = 2 rolls, 63 meters = 3 rolls, 88 meters = 4 rolls and so on.What code would give me such a result? I've been playing withMath.Ceiling but I'm lost :(

np, i think your on the right track, your logic is just a bit off, take a look at the code below. I tried to follow the same logic you used in your code sample. This code should give you the rolls required to cover the width, how many meters u would have if u got that many rolls and how many meters u would have left. Your going to have to do something similar to get the length, if you have any questions just let me know, i tried to explain it with some commenting. I used a console,but you can simply change the console.writeline statements to something like txtSomething.text = "" if you need.

    Sub CalculateAndDisplay(ByVal width As Integer, ByVal length As Integer)
        Const rollwidth As Integer = 5 'Setsthe constant amount of the variable"

        Dim strips As Integer 'the total rolls needed to cover the width
        Dim totalWidth As Integer 'the total width you would have in meters if you bought however many rolls
        Dim carpetWidthLeft As Integer 'the remaining width of carpet after cutting what is needed 'width'

        strips = Math.Ceiling(width / rollwidth) 'ex: width = 1; 1/5 = 1 roll 
        										'ex2: width = 6; 6/5 = 2 rolls
        										'so you can just use the ceiling method here, since you want to round up
        totalWidth = strips * rollwidth

        carpetWidthLeft = totalWidth - width

        Console.WriteLine("MetersRequired to cover width: " & totalWidth.ToString())
        Console.WriteLine("RollsRequired to cover width: " & strips.ToString())
        Console.WriteLine("WidthRemaining after covering width: " & carpetWidthLeft.ToString())
    End Sub
 
Last edited:
Back
Top