Array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Array

SmokeSmoke Posts: 2Member
I need a little help. I am inexperienced at QBasic and have no clue how to do this.

write a program that inputs 20 integers from the keyboard and stores them in an array known as A.

Then output the numbers in original input order with 5 numbers per line.

I then need to ouput the numbers in reverse order from which they were input with 4 numbers per line.

Then output the # of numbers that were input that are strictly between 17 and 35.

Input a number X. Search the array to see if X appears in A. If so output a message stating so and print the subscript of each position in which X occurs, that is , print I whenever A(I) = x. If X never appears in A, then print a message so stating.

A step by step help of some type would be very appreciative. I have no clue how to do this

Comments

  • deldel Posts: 51Member
    Sounds like homework. :-)

    : write a program that inputs 20 integers from the keyboard and stores them in an array known as A.

    Inputing is the easy part. Just use a counted loop (ie. it will loop 20 times) to ask the user 20 times for integers and store each integer in a separate array element.
    [code]
    DIM A(20) AS INTEGER
    FOR GetValues% = 1 TO 20
    PRINT "Enter integer"; GetValues%; ": ";
    INPUT "", A(GetValues%)
    NEXT GetValues%
    [/code]
    You will need this block of code before any of the blocks of code below for the program to work. Otherwise it will say "subscript out of range", which basically means you haven't dimensioned the array properly or at all (ie. you are trying to access data you haven't stored).

    : Then output the numbers in original input order with 5 numbers per line.

    Theres a couple ways you could do this. The method below is not the most efficient, but probably the easiest to understand. It uses another counted loop, except this time the loop increments by 5. So in effect, it will only loop four times (ie. i%=1, i%=6, i%=11, i%=16). Note that it only goes up to 16 - this is because you are printing i%+4, which is 20 when i% is 16.
    [code]
    FOR i% = 1 TO 16 STEP 5
    PRINT A(i%); A(i%+1); A(i%+2); A(i%+3); A(i%+4)
    NEXT i%
    [/code]
    : I then need to ouput the numbers in reverse order from which they were input with 4 numbers per line.

    Just rearranging the previous code:
    [code]
    FOR i% = 17 TO 1 STEP -4
    PRINT A(i%+3); A(i%+2); A(i%+1); A(i%)
    NEXT i%
    [/code]
    : Then output the # of numbers that were input that are strictly between 17 and 35.

    Another counted loop together with a binary selection (ie. IF...THEN...ELSE...).
    [code]
    TotalNum% = 0
    FOR i% = 1 TO 20
    IF A(i%) >= 17 AND A(i%) <= 35 THEN
    TotalNum% = TotalNum% + 1
    END IF
    NEXT i%
    PRINT TotalNum%; "numbers are between 17 and 35 inclusive."
    [/code]
    I'm not sure what you mean by "strictly" between. I have interpreted this as greater than or equal to 17 and less than or equal to 35. If you you mean just greater and less than and not equal to, you will need to change the equality signs in the IF...THEN line of code.

    : Input a number X. Search the array to see if X appears in A. If so output a message stating so and print the subscript of each position in which X occurs, that is , print I whenever A(I) = x. If X never appears in A, then print a message so stating.

    Similar to the one above:
    [code]
    CONST TRUE = 1, FALSE = 0
    INPUT "Enter a number to search for: ", x%
    MatchFound% = FALSE
    FOR i% = 1 TO 20
    IF A(i%) = x% THEN
    PRINT "Found at subscript"; i%
    MatchFound% = TRUE 'ie. MatchFound% = TRUE
    END IF
    NEXT i%
    IF NOT MatchFound% THEN
    PRINT "That number was not found in the array."
    END IF
    [/code]
    QBASIC doesn't have boolean variables (ie. true or false). Instead, you will find most QB programmers use integers as booleans. Sometimes it makes it easier use "constants" such as TRUE or FALSE rather than 1 or 0.

    If you wanted, you could get rid of the statement "CONST TRUE = 1, FALSE = 0" and then everywhere where it says "FALSE" in the program, replace it with a zero, and everywhere where it says "TRUE", replace it with a one. (This is basically what the compiler does when you execute the program.)

    I haven't tried any of these programs out. Let me know if any of them don't work & I'll see what I can do.


Sign In or Register to comment.