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: - Dim array as integer()
-
Array=new integer (0 to 100) {}
-
-
Console.Write("Please enter the value (-1 to stop)")
-
For i As Integer = 0 To array.GetUpperBound(0)
-
-
Next
Am I declaring it correctly? When I run the program, I cant input a number. Am I missing a step?
16 2169
Do you have anything inside the For...Next loop?
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?
...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.
Ok i decided to go another way with this problem and this is what i have so far: - Dim numbers As Integer 'An array for storing the input values.
-
Dim numCt As Integer() 'The number of numbers saved in the array.
-
Dim num As Integer 'One of the numbers input by the user.
-
-
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?
-
-
-
numCt = New Integer(0) {}
-
-
Console.Write(" Please enter the value (-1 to stop): " & vbTab)
-
numbers = Console.ReadLine()
-
-
'Get the numbers and put them in the array
-
While (True)
-
num = Console.ReadLine()
-
If (num <= 0) Then
-
-
End If
-
End While
-
-
Console.Write("Please enter the value (-1 to stop): " & vbTab)
-
numbers = Console.ReadLine()
-
End Sub
-
-
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.
SammyB 807
Recognized Expert Contributor
You are close to being correct: just a little confused. I rearranged your code just a little bit: - Dim iNums(0 To 99) As Integer 'The array.
-
Dim num As Integer 'One of the numbers input by the user.
-
Dim i As Integer
-
i = 0
-
num = 0
-
Console.Write("Please enter the value (-1 to stop): " & vbTab)
-
Do
-
num = Console.ReadLine()
-
If (num <> -1) Then
-
iNums(i) = num
-
i = i + 1
-
End If
-
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
...<<---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.
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: - Console.WriteLine("x1" & vbTab & "x2")
-
For j As Integer = 0 To array.GetUpperBound(0)
-
Console.Write(i & vbTab & array(i))
-
Next
i dont think i need another loop do i? i just dont understand why its not displaying.
SammyB 807
Recognized Expert Contributor - Console.WriteLine("x1" & vbTab & "x2")
-
For j As Integer = 0 To array.GetUpperBound(0)
-
Console.Write(i & vbTab & array(i))
-
Next
Your loop variable is j
You used i to index the array
Welcome to loops! (^o^)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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!
|
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:
|
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
|
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
| |
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;
|
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
|
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:
|
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.
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |