• 0

Visual Basic - Making 1000 = "*"


Question

Need help making every 1000 = "*"

attach is what the final result should look like
This is my code:

Public Class Form1
 
    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
 
        For intX = 0 To intNUM_PLANETS - 1
 
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
 
            'output bar chart on next line'
        Next
        
    End Sub

post-503111-0-56641300-1376880908.png

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

ermm is it something like this? this does not work but like what code should i add inside the lstPlanets.items.add(.....)

Option Strict On
Option Explicit On
'Anthony L
'CS 115
'Summer 2013
 Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
        Dim stars = New String("*"c, 1) 'stars = "*****"
 
        For intX = 0 To intNUM_PLANETS - 1
 
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            lstPlanets.Items.Add(stars)
        Next
 
 
    End Sub
Link to comment
Share on other sites

  • 0

The bar graph would be based on dblArrayPlanetSizes(). You could just divide this figure by 1000 and print the number of stars based on that.

Link to comment
Share on other sites

  • 0

Note: psuedo code below:

Const intNUM_PLANETS As Integer = 9

Dim strArrayPlanetNames() As String =
   {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
Dim dblArrayPlanetSizes() As Double =
   {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
For intX = 0 To intNUM_PLANETS - 1
   lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
  
   Dim numberOfStars As ??? = dblArrayPlanetSizes(intX))/1000   //??? - select variable type without decimal places
   for intY = 0 To numberOfStars
      lstPlanets.Items.Add("*")
   Next

Next
Link to comment
Share on other sites

  • 0

pseudo code: (I know its not perfect, but illustrates the point)

 

numstars = dblArrayPlanetSizes(intX) / 1000

for s = 0 to numstars

  echo "*"

next

Link to comment
Share on other sites

  • 0
Take the time to think about your problem, the data you have and in which form it is available. There's no point trying to get code examples to paste into your assignment if you don't understand how to reason about solving your problem. The code I gave you was not meant to be pasted verbatim into your assignment, but simply to illustrate how to create a string containing a repeating character.

 

Here's your problem in words:

For every planet you need to print its name and a certain amount of stars that corresponds to its size divided by 1000 and rounded up.

 

The planet names are in the array strArrayPlanetNames. The sizes are in the array dblArrayPlanetSizes.

 

For each planet, you'll first need to get its name and size from these arrays. Printing the name is easy. Printing the stars consists in

  1. finding the number of stars to print (i.e. the size divided by 1000 and rounded up)
  2. creating a string containing that many stars (I've showed you how already)
  3. printing that string to the output
 

From there it's only a matter of language syntax. Do you not understand what is an array, or how to get an element from an array? Or how to perform a division? Do you not understand what a for loop is? It's not clear where exactly lies the source of your confusion.

  • Like 1
Link to comment
Share on other sites

  • 0

The best way to learn is by troubleshooting. Come up with a flowchart first, then write pseudo code for each block. This will makes things a lot clearer when writing code (at first).

 

If you just copy someone else's code, you will forget it after a few minutes.

Link to comment
Share on other sites

  • 0

Yeah that's true but i do not learn a lot since this is an online class and the teacher never teach us or told us to read page something so i always google and read by myself which is kinda hard for me and there are several things that i have not learn maybe? or not? and yes it's very hard for me to know these codes... and btw 68k i tried using your code but then it multiply a lot of times and i do not really get the term of IntY = 0 so do we like start from the first number or?

this is what i have so far:

For intX = 0 To intNUM_PLANETS - 1
 
                'output name and size'
                lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            Dim numberOfStars As Double = dblArrayPlanetSizes(CInt((intX) / 1000))
            For intY = 0 To numberOfStars
                lstPlanets.Items.Add("*")
            Next
 
        Next
Link to comment
Share on other sites

  • 0

Yeah that's true but i do not learn a lot since this is an online class and the teacher never teach us or told us to read page something so i always google and read by myself which is kinda hard for me and there are several things that i have not learn maybe? or not? and yes it's very hard for me to know these codes... and btw 68k i tried using your code but then it multiply a lot of times and i do not really get the term of IntY = 0 so do we like start from the first number or?

this is what i have so far:

For intX = 0 To intNUM_PLANETS - 1

 

                'output name and size'

                lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))

            Dim numberOfStars As Double = dblArrayPlanetSizes(CInt((intX) / 1000))

            For intY = 0 To numberOfStars

                lstPlanets.Items.Add("*")

            Next

 

        Next

What is CInt((intX) / 1000) supposed to be? It's the array index divided by 1000 and converted to an integer - which it already is. The value of this expression will always be 0. This doesn't make sense. You're not thinking about what your values represent.

 

Then you create a variable named numberOfStars which you assign to one of the values of dblArrayPlanetSizes array. This doesn't make sense: this array contains planet sizes. If you take an element from it, this element is a planet size, not a number of stars.

 

Also, you don't want to add each star as a separate list item; this will add each star on its own line. Create the string as I showed you and then add it as a single item to the list.

 

You want to 1) get the planet size from the array, 2) divide that size by 1000; 3) use the result of that division as the number of stars to create your string.

 

That's three sub-problems to solve. You know how to solve each of these individually; every one is just a single basic of code. Here are some hints (replace the comments with your code):

 

Dim planetSize = '... get the size from the array'
Dim numberOfStars = '... divide that size by 1000 and round up'
Dim stars = New String("*"C,  '... use the calculated number of stars to specify how many stars to put'
lstPlanet.Items.Add('... add that string the output'

By the way please use code tags when you post code.

Link to comment
Share on other sites

  • 0

+Asik

I manage to get it working but is there another way to round this up?

 
               lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            Dim numberOfStars As Double = Math.Round(dblArrayPlanetSizes(intX) / 1000) '... divide that size by 1000 and round up'
            Dim stars = New String("*"c, CInt(numberOfStars))  '... use the calculated number of stars to specify how many stars to put'
            lstPlanets.Items.Add(stars + "*") '... add that string the output'
Link to comment
Share on other sites

  • 0

... why would you create a new duplicate array containing the planet sizes? How could you then divide that array by 1000? That wouldn't even compile.

 

The snippet I gave you goes inside your for loop. Use the loop index variable to access the existing planet size array you have, and retrieve a single planet size at a time.

 

By the way, the code you post often doesn't even compile. If you're working inside an IDE like Visual Studio, you should see red squiggles under the problematic lines and errors listed below. You don't need people on a forum to tell you your code doesn't work when Visual Studio already tells you that. Always make sure your code compiles and runs before anything else.

Link to comment
Share on other sites

  • 0

 

Yeah that's true but i do not learn a lot since this is an online class and the teacher never teach us or told us to read page something so i always google and read by myself which is kinda hard for me and there are several things that i have not learn maybe? or not? and yes it's very hard for me to know these codes... and btw 68k i tried using your code but then it multiply a lot of times and i do not really get the term of IntY = 0 so do we like start from the first number or?

this is what i have so far:

For intX = 0 To intNUM_PLANETS - 1
 
                'output name and size'
                lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
            Dim numberOfStars As Double = dblArrayPlanetSizes(CInt((intX) / 1000))
            For intY = 0 To numberOfStars
                lstPlanets.Items.Add("*")
            Next
 
        Next

 

From http://msdn.microsoft.com/en-us/library/vstudio/5z06z1kb.aspx:

 

You use a For...Next structure when you want to repeat a set of statements a set number of times.

In the following example, the index variable starts with a value of 1 and is incremented with each iteration of the loop, ending after the value of index reaches 5.

For index As Integer = 1 To 5
    Debug.Write(index.ToString & " ")
Next
Debug.WriteLine("")
' Output: 1 2 3 4 5

Every time the For loop is initiated the variable "index" would be reset to "1".

Link to comment
Share on other sites

  • 0

i tried it and it work well :|

Why are you surprised? IMO the method you're following doesn't seem to teach you the fundamentals right; you don't really understand what you're doing and just rushing through exercices. You'll hit a wall pretty soon at that rate. Get a good book and read it carefully. Learn to:

 

 - understand compiler error messages and how to find information about them

 - step through your code in the debugger so you can visualize what your program is doing at every statement

 

With good foundations you shouldn't need to ask people to guide you every step of the way, and then wonder how the program ended up working. Unfortunately, many courses are just plain bad, so you need to get additional resources by yourself. When I learned C++ I bought myself a huge tutorial book and read it on the bus; the material shown in class and exercices was insufficient by far.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.