473,809 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring an array

8 New Member
Hi!

I dont understand arrays very much. Im trying to create an array of up to 100 positive numbers with the use of -1 to stop entering if the user needs an array containing less than 100 elements.

Ive seen them declared many ways but since im want to create an array of up to 100, Im using the following:

Expand|Select|Wrap|Line Numbers
  1. Dim array as integer()
  2. Array=new integer (0 to 100) {}
  3.  
  4. Console.Write("Please enter the value (-1 to stop)")
  5.         For i As Integer = 0 To array.GetUpperBound(0)
  6.  
  7.         Next
Am I declaring it correctly? When I run the program, I cant input a number. Am I missing a step?
Mar 14 '07 #1
16 2169
Killer42
8,435 Recognized Expert Expert
Do you have anything inside the For...Next loop?
Mar 14 '07 #2
dejavu33
8 New Member
For i As Integer = 0 To array.GetUpperB ound(0)
Console.Write(i & vbTab & array(i))
Next



This gives me the 100 positive numbers in the array but what i wanted to do was to have the user input the numbers

For example:
Please enter the value (-1 to stop):
6
Please enter the value (-1 to stop):
4
Please enter the value (-1 to stop):
-1

x1[0]=6
x2[1]=4


So, im assuming that my array is wrong right?
Mar 14 '07 #3
Killer42
8,435 Recognized Expert Expert
...This gives me the 100 positive numbers in the array but what i wanted to do was to have the user input the numbers
...
So, im assuming that my array is wrong right?
As far as I know (not really familiar with your version of VB) your array is probably OK. But there is nothing in your code to indicate a user input. All I can see you doing is writing the values from your array onto the console. I expect that will come out as all zeroes.
Mar 14 '07 #4
dejavu33
8 New Member
Ok i decided to go another way with this problem and this is what i have so far:

Expand|Select|Wrap|Line Numbers
  1.  Dim numbers As Integer    'An array for storing the input values.
  2.         Dim numCt As Integer()    'The number of numbers saved in the array.
  3.         Dim num As Integer         'One of the numbers input by the user.
  4.  
  5.         numbers = New Integer(0 To 99) {}  <<---I get an error here saying     Value of type '1-dimensional array of Integer' cannot be converted to 'Integer'   What does this mean?
  6.  
  7.  
  8.         numCt = New Integer(0) {}
  9.  
  10.         Console.Write(" Please enter the value (-1 to stop): " & vbTab)
  11.         numbers = Console.ReadLine()
  12.  
  13.         'Get the numbers and put them in the array
  14.         While (True)
  15.             num = Console.ReadLine()
  16.             If (num <= 0) Then
  17.  
  18.             End If
  19.         End While
  20.  
  21.         Console.Write("Please enter the value (-1 to stop): " & vbTab)
  22.         numbers = Console.ReadLine()
  23.     End Sub
  24.  
  25. End Module
And here is what im trying to do:
-allow a user to create an array of up to 100 positive numbers with the use of -1 to stop entering if user needs an array containing less than 100 elements


Can anyone tell me if im on the right track? or even close? or am i just way off?

Any help would be appreciated. Thank you.
Mar 19 '07 #5
SammyB
807 Recognized Expert Contributor
You are close to being correct: just a little confused. I rearranged your code just a little bit:
Expand|Select|Wrap|Line Numbers
  1. Dim iNums(0 To 99) As Integer 'The array.
  2. Dim num As Integer 'One of the numbers input by the user.
  3. Dim i As Integer
  4. i = 0
  5. num = 0
  6. Console.Write("Please enter the value (-1 to stop): " & vbTab)
  7. Do
  8.     num = Console.ReadLine()
  9.     If (num <> -1) Then
  10.         iNums(i) = num
  11.         i = i + 1
  12.     End If
  13. Loop Until (num = -1 Or i = 100)
Do you see now how to define the array? Now, add a loop at the end that prints out the array. HTH --Sam
Mar 19 '07 #6
Killer42
8,435 Recognized Expert Expert
...<<---I get an error here saying Value of type '1-dimensional array of Integer' cannot be converted to 'Integer' What does this mean?
numbers is not defined as an array - just a single (um... "scalar" I think they call it) Integer variable. I can't tell you how to fix it though, as this is a later version of VB than I'm familiar with.
Mar 19 '07 #7
dejavu33
8 New Member
Oh, ok. I see how to define the array now. Yes, you are correct...I was somewhat confused. Thank you SammyB for your help.

I just have another question. I added another loop at the end of the code to display my output. But, everytime i run the program...it doesnt display it.
This is what i added:

Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("x1" & vbTab & "x2")
  2. For j As Integer = 0 To array.GetUpperBound(0)
  3.             Console.Write(i & vbTab & array(i))
  4.         Next
i dont think i need another loop do i? i just dont understand why its not displaying.
Mar 19 '07 #8
SammyB
807 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("x1" & vbTab & "x2")
  2. For j As Integer = 0 To array.GetUpperBound(0)
  3. Console.Write(i & vbTab & array(i))
  4. Next
Your loop variable is j
You used i to index the array

Welcome to loops! (^o^)
Mar 19 '07 #9
dejavu33
8 New Member
hehehe...thank you. I guess ive been staring at the computer screen for the longest that i cant see the small mistakes anymore.

Hey by any chance do you know if there is a way to make two copies of an array. For example, If i wanted to make two copies of the data inputted by the user in the above array? If so, how could that be done. I think I read somewhere that it could be done but dont know how and ive tried looking it up online for information but cant find any information on it.
Mar 20 '07 #10

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

Similar topics

29
4060
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least enough to convert a favorite PHP script to work on an ASP.NET site. I'm experimenting with simple example scripts that will help me learn how to implement each "piece" of the puzzle.
5
10424
by: ms_chika | last post by:
Hi to all, I have this problem in declaring a two-dimesional array, how will i declare such array with unknown number of rows? Your help will be very much appreciated. Thanks in advance. *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
18
1669
by: Nathan | last post by:
If you're wondering why I post so many questions, it's because I want to make an entry in the Guinness Book of World Records. But because I post so many, I try to make them simple. Here is (I hope) a simple one: I've declared a "control array" just after the WFD generated code like this: Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5} But when I try to do something like this:
3
2188
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my approach proper here or is the a better way for setting the instance of this specific format for an array. -- mark
1
1683
by: John Dann | last post by:
Is there a way of declaring an array of a structure where the structure has a constructor? So if I had a structure say Friend MyStructure prop1 as integer prop2 as string sub New (p1 as integer, p2 as string) me.prop1=p1 me.prop2=p2 End Sub
3
303
by: hn.ft.pris | last post by:
I've got following code test C++'s functor. For the sake of easy-reading, I omit some declearations. #include <algorithm> #include <functional> using namespace std;
5
1921
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I am declaring an element like this: public static List<myListElementmyList = new List<myListElement>(); I would like to declare an array of this myList. How would I modify the declaration of myList so it will represents an array? Thanks Eitan
4
1658
by: Peter Duniho | last post by:
On Thu, 14 Aug 2008 18:56:00 -0700, Phill <Phill@discussions.microsoft.comwrote: For future reference, if you are asking for help with an error (compile or execution), you really should post the complete text of the error and be specific about when and where it happens. That said, in your code it's clear what's wrong:
8
3467
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas the second uses pointer notation. But apart from that what are the implications / dangers, etc. if any.
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10635
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.