This is a very stupid exercise
Given an array of n integers removes the first max element and last min element from that array.
Can anyone help me understand what is wrong? This is the first time I write in VB.
Imports System
Public Module Program
Public Sub Main(args() As string)
Dim numbers = New Integer() {1, 2, 4, 8, 5, 4, 9, 63, 2, 7, 0, 5, 4, 2}
Console.WriteLine("Initial Array {0}" target)
Dim targetSize As Integer = numbers.Length - 2
Dim target(targetSize) As Integer
Dim max = numbers(0)
Dim maxIndex = 0
Dim min = numbers(0)
Dim minIndex = 0
For index = 0 To numbers.Length
If max < numbers(index) Then
maxIndex = index
Stop
End If
Exit For
For index = 0 To numbers.Length
If min > numbers(index) Then
minIndex = index
End If
Exit For
Dim targetIndex As Integer = 0
For index = 0 To numbers.Length
If (index <> minIndex Or index <> maxIndex) Then
target(targetIndex) = numbers(index)
targetIndex = targetIndex 1
End If
Exit For
Console.WriteLine("Target Array {0}" target)
End Sub
End Module
And This is the Output.
Visual Basic.Net Compiler version 0.0.0.5943 (Mono 4.0.1 - tarball)
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.
/runtime/vb/3yn8kpgq9_3ynmpk553/HelloWorld.vb (40,9) : error VBNC30084: CHANGEME
/runtime/vb/3yn8kpgq9_3ynmpk553/HelloWorld.vb (40,9) : error VBNC30084: CHANGEME
/runtime/vb/3yn8kpgq9_3ynmpk553/HelloWorld.vb (40,9) : error VBNC30084: CHANGEME
There were 3 errors and 0 warnings.
Compilation took 00:00:00.4196520
Error: Command failed: timeout 7 vbnc HelloWorld.vb
CodePudding user response:
There are several issues with your code, for example:
target
is not declared- the for-loops have no
Next
- The
Stop
should be anExit For
- the logic is broken, you want the min- and max-values but you don't check all
- arrays are fixed sized, you cannot remove items, use a
List(Of T)
You can simplify all to:
Dim numbers As Int32() = {1, 2, 4, 8, 5, 4, 9, 63, 2, 7, 0, 5, 4, 2}
Dim min As Int32 = numbers.Min()
Dim max As Int32 = numbers.Max()
Dim numberList As List(Of Int32) = numbers.ToList()
Dim indexOfFirstMax As Int32 = numberList.IndexOf(max)
numberList.RemoveAt(indexOfFirstMax)
Dim indexOfLastMin As Int32 = numberList.LastIndexOf(min)
numberList.RemoveAt(indexOfLastMin)
numbers = numberList.ToArray()