Jump to content

Need a bit of help with a Number Sum project

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts
Hey guys whats going on. I need a bit of help with this project of mine. basically I'm supposed to create an application that sums a series of numbers. Prompt a user to enter a starting and ending number. Sum all the integers from the starting value to the ending value, then using labels display the sum. the bonus is to also display an expression of the numbers summed.

If you need more info visit here: http://appocomp1.wikispaces.com/Day+62
(thats also a pretty nice place for some challenges and what not if you want.

I know I'm supposed to implement a do while loop somewhere. Heres what I got so far.

Private Sub btncalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalc.Click

        Dim strtthing As Integer = txtboxstrtnum.Text

        Dim endthing As Integer = txtboxendnum.Text

        Do

            If endthing > strtthing Then

                endthing = endthing - 1

                lbldisp.Text = endthing

            End If

        Loop

    End Sub


#2
Bound

Bound

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hey ANG, how's it going? :c-smile:

By summing numbers, do you mean using Sigma and summing a series of numbers? Like this:
Posted Image

If so, the math is a little difficult for me to remember. :c-^_^: From the looks of it, it looks like your program would have 2 textboxes with 1 labeled Starting Number and the other Ending Number and a button that says Calculate. From there you could use a Do loop to do the calculations, so you're right about that. But I'm not sure the code inside your do loop is doing what it's supposed to be doing.

I visited Summation - Wikipedia, the free encyclopedia to refresh my memory on summation, and they include a portion of using programming languages to perform summation problems. Here's their VB portion (might be outdated):


Sum = 0

 For I = M To N

     Sum = Sum + X(I)

 Next I


They use a For loop instead, but I'm sure it's possible with a Do loop as well.

If I'm correct, you could have a starting number of 1 and an ending number of 100 and the total would be 5050 because 1 + 2 + 3 + 4 + 5 ... + 100 = 5050 and that is the basic principle of summation.

Personally, I'm very lazy and I just used the formula:


iSolution = ((iEnd * iEnd) + iEnd) / 2


But since you're using a Do loop you'll have to break the mathematics down into steps. I've got to go to sleep now, but if this isn't solved by tomorrow I'll help you out more. :c-grin:

Bound

#3
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim startnum As Integer = TextBox1.Text
        Dim endnum As Integer = TextBox2.Text
        Dim total As Integer = 0
        Dim expression As String = ""

        While startnum <= endnum
            total = total + startnum
            If startnum.Equals(endnum) Then
                expression = expression + startnum.ToString + " = "
            Else
                expression = expression + startnum.ToString + " + "
            End If
            startnum = startnum + 1
        End While

        Label1.Text() = expression + total.ToString
    End Sub

This is how I do it though not the best way. Maybe you can think of a more efficient way of doing it.

Anyway, Hope it helps.

#4
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts

Xdawn90 said:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim startnum As Integer = TextBox1.Text
        Dim endnum As Integer = TextBox2.Text
        Dim total As Integer = 0
        Dim expression As String = ""

        While startnum <= endnum
            total = total + startnum
            If startnum.Equals(endnum) Then
                expression = expression + startnum.ToString + " = "
            Else
                expression = expression + startnum.ToString + " + "
            End If
            startnum = startnum + 1
        End While

        Label1.Text() = expression + total.ToString
    End Sub

This is how I do it though not the best way. Maybe you can think of a more efficient way of doing it.

Anyway, Hope it helps.

Thanks for your help so much guys! And yes Bound I believe a sigma was what he was talking about. I put in Dawn's code and it worked great. Practically perfect. The only issue is that the label displays "40 + 41 + 42 + 43" so on and so forth. I don't want to trouble you guys anymore so I'll figure this one out! Plus I need to stop feeling like I'm using you guys so I'll post my new code for just displaying the final number in the end in a few! But thanks for the tips! I FREAKING LOVE THIS PLACE! - AfterShock

#5
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts

ANG AfterShock said:

Thanks for your help so much guys! And yes Bound I believe a sigma was what he was talking about. I put in Dawn's code and it worked great. Practically perfect. The only issue is that the label displays "40 + 41 + 42 + 43" so on and so forth. I don't want to trouble you guys anymore so I'll figure this one out! Plus I need to stop feeling like I'm using you guys so I'll post my new code for just displaying the final number in the end in a few! But thanks for the tips! I FREAKING LOVE THIS PLACE! - AfterShock

Ah Well that was easy! Boom!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim startnum As Integer = TextBox1.Text
        Dim endnum As Integer = TextBox2.Text
        Dim total As Integer = 0
        Dim expression As String = ""

        While startnum <= endnum
            total = total + startnum
            If startnum.Equals(endnum) Then
                expression = expression + startnum.ToString + " = "
            Else
                expression = expression + startnum.ToString + " + "
            End If
            startnum = startnum + 1
        End While
        Label1.Text() = total
    End Sub


#6
Bound

Bound

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
You're welcome, even though Xdawn90 did the work, not me lol :c-smile:

Looks good :c-thumbup:

#7
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
You are welcome. Glad that it helps. :c-smile:

You can remove the if statement and the "expression" variable as well.

#8
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts
Yup A for me! A for you guys! thx dawn and bound! :c-laugh:

AND MERRY CHRISTMAS TO THE BOTH!
Posted Image
If you need any help with designing your program with graphics to the caliber of the above then just hit me up with a PM. I'd be more than willing to help! God bless! :c-cool: