• 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
https://www.neowin.net/forum/topic/1171589-visual-basic-making-1000/
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
  • 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
  • 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
  • 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.

  • 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
  • 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.

  • 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'
  • 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.

  • 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".

  • 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.

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

    • No registered users viewing this page.
  • Posts

    • DiskGenius 6.2.0.1829 - All Versions: Free, Lite & Portable by Razvan Serea DiskGenius is a full-featured partition manager, which is designed to optimize disk usage for Windows users. It will efficiently help you recover lost data, resize/split partition, backup files, edit hex data, check bad sectors, manage virtual disks, erase data, etc.. Create a system image backup for current Windows with simple clicks to keep the operating system under protection. DiskGenius key features: Partition Management - It can create format, resize, extend, backup, split, hide and clone partition, both MBR and GPT are supported. Disk and partition conversion - Convert dynamic disk to basic, convert virtual disk format and convert MBR to GPT, convert primary partition to logical. File recovery - It can recover files deleted or emptied form recycle bin, recover files from damaged partition or disk and recover files by file type and supports file preview and file filter. Partition recovery - It is the best partition recovery program in that it can recover files from damaged, corrupted and RAW partitions, search for lost partition and recover files from it, besides, it can fix partition table. RAID recovery - It can reconstruct Virtual RAID and recover files from it, and all RAID types are supported. Sector Editor - A Hex editor is embedded to help users edit raw hex data and recover data manually. Backup and Restore - It can backup and restore partition including system partition, hard disk and partition table. Bad Tracks - It can check and repair bad sectors for all storage devices; check hard disk S.M.A.R.T. information. Delete files permanently - It can delete files permanently so that they can't be recovered by any data recovery software. Virtual Disk - It supports virtual disks, including VMware, Virtual PC and Virtual Box. Create WinPE bootable disk and you can manage disk partition when system crashes or there is no operating system on your computer. Support FAT12/FAT16/FAt32/exFAT/NTFS/EXT2/EXT3/EXT4 file system format. DiskGenius 6.2.0.1829 changelog: Add the "Disk Speed Test" feature. Add the "Windows Boot Repair and Conversion" feature. Add the BMB21-2019 erase standard to the "Erase Sectors" feature. Add support for restoring an individual partition from a PMFX disk image file. Enhanced The "Verify Or Repair Bad Sectors/Blocks" feature displays disk read speed in the detection window during scanning. The "Quick Partition" dialog box allows users to quickly select the number of partitions by pressing the numeric keys 1, 2, 7, 8, or 9. The "Set Volume Name" dialog box supports selecting preset volume labels provided by the software. The "Copy Sectors" feature supports resuming copy tasks after modifying the number of skipped bad sectors. Add the "TRIM Optimization" option to the format dialog box. The "Clone Partition" and "Clone Disk" features perform TRIM optimization on target partitions or disks before cloning. Add support for Not Equal To search conditions (prefixed with "!") when searching hexadecimal data in the sector editor. Optimize the display of capacity values in the program interface to show two decimal places. Add a minimize button to dialogs that may require long processing time. Enhance support for the ReFS file system. Enhance support for newer HIF and MP4 formats when recovering files by type. Enhance support for the EXT4 file system. Enhance compatibility of the "File Recovery" feature with special data structures. Fixed Fixed the issue that the selected file system type automatically reverted to NTFS after changing it to exFAT or EXT4 in the "Quick Partition" dialog box. Fixed inaccurate Unicode string search results in the "Sector Editor" feature. Fixed the issue that exceptions might occur when adding multiple disks in the "Erase Sectors" feature. Fixed the issue that insufficient target disk space was incorrectly reported in some cases when cloning, backing up, or restoring disks. Fixed the issue that folder modification timestamps were not preserved when copying files from ReFS partitions. Fixed the issue that Excel-format reports generated by features such as file copying or bad sector checking could not be opened when the report contained more than one million rows. Fixed the issue that folders were not displayed in the exclude-folder dialog box when backing up partitions to image files. Fixed the issue that the "Erase Sectors" feature could not be executed in some cases. Download: DiskGenius 6.2.0.1829 | 63.9 MB (Freeware, paid upgrade available) Download: DiskGenius Portable 64-bit | 40.0 MB Download: DiskGenius Portable 32-bit | 36.0 MB Download: DiskGenius Lite 64-bit | 13.4 MB Download: DiskGenius Lite 32-bit | 11.6 MB View: DiskGenius Home Page | DiskGenius Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Really? Use a better search engine https://www.google.com/search?...ourceid=chrome&ie=UTF-8
    • Seems like Neowin has transitioned into being simps for the white house. I can't find a review for the last UFC games that came out.
  • Recent Achievements

    • Week One Done
      agatameier earned a badge
      Week One Done
    • One Month Later
      agatameier earned a badge
      One Month Later
    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      513
    2. 2
      +Edouard
      182
    3. 3
      PsYcHoKiLLa
      143
    4. 4
      ATLien_0
      95
    5. 5
      Steven P.
      76
  • Tell a friend

    Love Neowin? Tell a friend!