Declaring an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dejavu33
    New Member
    • Mar 2007
    • 8

    Declaring an array

    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:

    Code:
    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?
    Last edited by Killer42; Mar 14 '07, 02:01 AM. Reason: Please use CODE.../CODE tags around your code.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Do you have anything inside the For...Next loop?

    Comment

    • dejavu33
      New Member
      • Mar 2007
      • 8

      #3
      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?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by dejavu33
        ...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.

        Comment

        • dejavu33
          New Member
          • Mar 2007
          • 8

          #5
          Ok i decided to go another way with this problem and this is what i have so far:

          Code:
           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.

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            You are close to being correct: just a little confused. I rearranged your code just a little bit:
            Code:
            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

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by dejavu33
              ...<<---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.

              Comment

              • dejavu33
                New Member
                • Mar 2007
                • 8

                #8
                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:

                Code:
                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.

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #9
                  Code:
                  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^)

                  Comment

                  • dejavu33
                    New Member
                    • Mar 2007
                    • 8

                    #10
                    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.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by dejavu33
                      hehehe...thank you. I guess ive been staring at the computer screen for the longest that i cant see the small mistakes anymore.
                      Welcome to the world of programming. :)

                      As someone who has been debugging code for many years, take it from me - it's always much easier to see errors in someone else's code than in your own. (Same goes for proofreading documents, but that's another story). When you wrote it, you tend to read not what is actually there, but what you already know is there.

                      Also, in programming it's usually the tiny, almost impossible-to-see details that catch you out.

                      Comment

                      • SammyB
                        Recognized Expert Contributor
                        • Mar 2007
                        • 807

                        #12
                        Originally posted by dejavu33
                        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.
                        Just dim another array and use the =
                        Code:
                        		Dim a() As Integer = {1, 2}
                        		Dim b(2) As Integer
                        		b = a
                        		MsgBox(b(1))

                        Comment

                        • computerenigma13
                          New Member
                          • Mar 2007
                          • 36

                          #13
                          dim the arrays as 2 differnt names and it should work right
                          Last edited by Killer42; Mar 21 '07, 02:49 AM. Reason: Font and Size tags don't work here.

                          Comment

                          • dejavu33
                            New Member
                            • Mar 2007
                            • 8

                            #14
                            Ok, I have been working and adding more things to my code. I think Im getting a little confused again on some parts though...maybe even complicating it more than it should be.

                            This is what im trying to do: (i split it into sections to make it understandable)

                            1. I want the user 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.

                            2. I also should check the user's input to prevent input of any wrong numbers. For example, any negative numbers except for -1

                            3. I also want to make two copies of the array... making sure that two new arrays are created only of the necessary size. For example, as it was entered by the user.

                            4. I want to implement any sorting algorithm and use it to sort the first copy of the array. I used the selection sort algorithm.

                            5. I also want to sort the second copy of the array and i used the standard array.sort algorithm

                            6. And finally outputting all three arrays in one table.


                            And here is how I kind of want the output to be displayed:

                            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[0]=4 x3[0]=4
                            x1[1]=4 x2[1]=6 x3[0]=6


                            Now, my problem is that my output is not displaying at all. It was before but with all the changes I made to my code, I cant seem to find why it doesnt display anymore. Any help will be appreciated. I dont want to make it complicated but I feel like I have.

                            Thank you.

                            Here is the code that I have:


                            Code:
                            Module SortArray
                            
                                Sub Main()
                            
                                    Dim numbers(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)
                                    num = Console.ReadLine()
                                    While num <> -1
                                        Console.Write("Please enter the value (-1 to stop):" & vbTab)
                                        num = Console.ReadLine()
                                    End While
                            
                                    Do
                                        num = Console.ReadLine()
                                        If (num <> -1) Then
                                            numbers(i) = num
                                            i = i + 1
                                        End If
                                    Loop Until (num = -1 Or i = 100)
                            
                                    'display values in array
                                    Console.WriteLine("x1" & vbTab & "x2" & vbTab & "x3")
                                    DisplayArray(numbers)
                            
                            
                                    For i As Integer = 0 To numbers.GetUpperBound(0)
                                        Console.Write(i & vbTab & numbers(i))
                                    Next
                            
                                    'creating two copies of the array
                                    Dim numbers1 As Integer()
                                    For j As Integer = 0 To numbers1.GetUpperBound(0)
                                        Console.Write(" " & numbers1(i))
                                    Next
                                    ModifyArray(numbers)
                                    Console.Write(" " & numbers1(i))
                            
                                    Dim numbers2 As Integer()
                                    numbers2 = numbers1
                            
                                    Console.WriteLine("x3")
                                    Array.Sort(numbers)
                                    DisplayArray(numbers)
                                End Sub
                            
                                Public Sub DisplayArray(ByVal theArray() As Object)
                                    Dim obj As Object
                                    For Each obj In theArray
                                        Console.WriteLine("{0}", obj)
                                    Next obj
                                    Console.WriteLine(ControlChars.Lf)
                            
                                    'using selection sort algorithm to sort first copy of array
                                End Sub
                                Sub Selectionsort(ByVal List() As Long, ByVal min As Integer, _
                                ByVal max As Integer)
                                    Dim i As Integer
                                    Dim j As Integer
                                    Dim bestValue As Long
                                    Dim bestJ As Integer
                            
                                    For i = min To max - 1
                                        bestValue = List(i)
                                        bestJ = i
                                        For j = i + 1 To max
                                            If List(j) < bestValue Then
                                                bestValue = List(j)
                                                bestJ = j
                                            End If
                                        Next j
                                        List(bestJ) = List(i)
                                        List(i) = bestValue
                                    Next i
                            
                                End Sub
                            
                                'sorting the second array using the array.sort algorithm
                            
                                Sub ModifyArray(ByVal arrayParameter As Integer())
                                    For j As Integer = 0 To arrayParameter.GetUpperBound(0)
                                        arrayParameter(j) *= 2
                                    Next
                                End Sub
                            
                            End Module

                            Comment

                            • dejavu33
                              New Member
                              • Mar 2007
                              • 8

                              #15
                              I still have no idea what im doing wrong. I cant seem to make it work.
                              Can anyone point me to the right direction please.

                              Comment

                              Working...