473,471 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with Arrays

6 New Member
I am writing a program that takes a group of integers from a text file. There are 20 or them all between 0-9. I am trying to display the frequency of each digit in a list box. I thought I had the code correct but for some reason it’s off by one. Also I am drawing a blank when it comes to displaying the order of the numbers in the text file but only displaying each number once. My current code is listed down below. Any help would be awesome. Thanks

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Numbers(10) As Integer
  3.  
  4.     Private number As Integer
  5.     Private UpperBound As Integer
  6.  
  7.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
  8.         Dim sr As IO.StreamReader = IO.File.OpenText("Num.txt")
  9.  
  10.         For i As Integer = 0 To 20
  11.             number = CInt(sr.ReadLine)
  12.             Numbers(CInt(number)) += 1
  13.         Next
  14.         sr.Close()
  15.  
  16.         For i As Integer = 0 To 9
  17.             lstDisplay.Items.Add(Numbers(i))
  18.         Next
  19.  
  20.     End Sub
  21.  
May 5 '08 #1
1 957
kuzen
20 New Member
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Numbers(2,10) As Integer
  3.     Private number As Integer
  4.     Private UpperBound As Integer
  5.  
  6.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
  7.         Dim sr As IO.StreamReader = IO.File.OpenText("Num.txt")
  8.  
  9.         For i As Integer = 0 To 19     'before there were 21 loops for 20 lines?
  10.             number = CInt(sr.ReadLine)
  11.             If Numbers(0,CInt(number)) =0 then
  12.             Numbers(1,CInt(number))=i
  13.             End if
  14.             Numbers(0,CInt(number)) += 1
  15.         Next
  16.         sr.Close()
  17.  
  18.     End Sub
  19.  
I altered your code a bit...the second dimension I added to your array indicates the number of the line in your txt file at which some number starts appearing.
Sorting the array out by this dimension you get the order of each number's appearance in the list. You will lose the numbers though since your numbers are apparently indices of Numbers() at the same time and sorting by the second dimension is about reindexing. You will probably need another dimension to keep the actual values independently.
May 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
2
by: Xarky | last post by:
Hi, I have a two dimensional array as follows: double myArray = new double; Now I have a method which is returning double, which is of size 41. I need to put the data of the array returned...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
1
by: codergem | last post by:
The following code is for heap sort but its not working properly. According to my analysis the Logic is correct but still its not showing the correct output. Thanks. #include "stdafx.h"...
4
by: sonjaa | last post by:
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was...
3
by: MariyaGel | last post by:
I have wrote the program and it worked fine until I had to include one more array into it. As you can see below the two arrays of volume and mass are working properly now I need to include a third...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.