none
Small Basic Arrays

    Question

  • I am trying to make a program in Small Basic in which the user has to enter multiple numbers until the user enters "0" or enters nothing. Then the program has to show the largest and the smallest number which the user has entered. But the program also has to show all the numbers entered in the opposite order. For example if the user enter: 3,12,8. The program has to show: 8,12,3. Also the program doesn't know the amount of numbers the user will enter!

    My question is how am i able to show the numbers entered in opposite order?

    I was able to show the largest and smallest numbers but not the numbers in opposite order. I know that i have to use an array but i haven’t been succesfull.

    This is what i have now:

    TextWindow.Write("Enter a number: ")

    varNumber = TextWindow.ReadNumber()

    varMax = 0

    varMin = varNumber

    While varNumber <> 0

      If varNumber > varMax Then

        varMax = varNumber

      EndIf

      If varNumber < varMin Then

        varMin = varNumber

      EndIf

      TextWindow.Write("Enter a number: ")

      varNumber = TextWindow.ReadNumber()

    EndWhile

    TextWindow.WriteLine("The largest number is "+varMax)

    TextWindow.WriteLine("The smallest number is "+varMin)

    Saturday, April 01, 2017 10:29 PM

Answers

  • Dillan,

    As you suggested you need to use an array. I sure hope this is not homework! Here's an example.

    'Enter any quantity of numbers
    TextWindow.Write("Enter a Number: ")
    Number=TextWindow.Read()
    Count=1
    While Number <> ""
      NumArray[count]=Number
      TextWindow.Write("Enter a Number: ")
      Number=TextWindow.Read()
      Count=Count+1
    EndWhile

    'Show Original Order
    For x=1 To Array.GetItemCount(NumArray)
      TextWindow.WriteLine("Original Order: "+NumArray[x])
    EndFor

    'Show Numbers in Backward Order
    For x= Array.GetItemCount(NumArray) To 1 Step -1
      TextWindow.WriteLine("Backward Order" +NumArray[x])
    EndFor

    'Show Max Number and Min Number Entered
    MaxNum=NumArray[1] 'This sets MaxNum to a starting value
    MinNum=NumArray[1] 'This sets MinNum to a starting value
    For x=1 To Array.GetItemCount(NumArray)
      MaxNum=Math.Max(MaxNum,NumArray[x])
      MinNum=Math.Min(MinNum,NumArray[x])
    EndFor
    TextWindow.WriteLine("Max Number is: "+MaxNum+" Min Number is: "+MinNum)


    JR


    • Edited by jricestkEditor Sunday, April 02, 2017 2:03 AM Edit
    • Marked as answer by Dillan M Sunday, April 02, 2017 5:04 PM
    Sunday, April 02, 2017 2:02 AM
    Answerer