Turn on thread page Beta
    • Thread Starter
    Offline

    0
    ReputationRep:
    I've got a couple of questions like this. I think that it's upposed to be easy but there's a part to it that i just don't get. Here it is:
    X=0, K=0
    FOR I = 1 TO 6
    FOR J = 1 TO 5
    X = I x J
    K = K + 1
    PRINT K, I, "times", J, "equals", X
    NEXT J
    NEXT I
    END

    What is FOR I = 1 TO 6 and FOR J = 1 TO 5 all about? How do i know what I and J are? Will the rest make sense when i figure this out?
    Thanks for any help.
    Offline

    11
    ReputationRep:
    The "FOR I = 1 TO 6" bit means you only apply the algorithm if I takes a value between 1 and 6 inclusive.

    The algorithm's not well-written, but I think you should just start with I and J equal to 1, since this is logical. The algorithm should then output the squares of 1, 2, 3, 4 and 5. I don't think it'll output the square of 6 because the algorithm doesn't run when J > 5.
    • Thread Starter
    Offline

    0
    ReputationRep:
    thanks, i thought it was going to be something really complicated.
    Offline

    8
    ReputationRep:
    (Original post by coral_fangs)
    I've got a couple of questions like this. I think that it's upposed to be easy but there's a part to it that i just don't get. Here it is:
    X=0, K=0
    FOR I = 1 TO 6
    FOR J = 1 TO 5
    X = I x J
    K = K + 1
    PRINT K, I, "times", J, "equals", X
    NEXT J
    NEXT I
    END

    What is FOR I = 1 TO 6 and FOR J = 1 TO 5 all about? How do i know what I and J are? Will the rest make sense when i figure this out?
    Thanks for any help.
    This code fraction simply outputs some partial multiplication tables.
    You have an outer loop and an inner loop.
    The inner loop variable varies from 1 to 5 for each value taken by the outer loop variable.
    When I =1, say, J takes the values from 1 to 5, and X takes the values,
    X = I*J = 1*1
    X = I*J = 1*2
    X = I*J = 1*3
    .....

    And within the inner loop, you have a print statement that writes,

    K, I, "times", J, "equals", X

    puting in values for the outer loop variable I = 1, and K constantly incrementing,

    1, 1 times 1 equals 1
    2, 1 times 2 equals 2
    3, 1 times 3 equals 3
    4, 1 times 4 equals 4
    5, 1 times 5 equals 5

    There ends the first run of the innner loop. The outer loop now increments it's loop variable, I, to equal 2, and the inner loop now outputs a 2nd times table.

    6, 2 times 1 equals 2
    7, 2 times 2 equals 4
    8, 2 times 3 equals 6
    9, 2 times 4 equals 8
    10, 2 times 5 equals 10

    etc.
 
 
 
Turn on thread page Beta
Updated: August 11, 2004

1,954

students online now

800,000+

Exam discussions

Find your exam discussion here

Poll
Should universities take a stronger line on drugs?
Useful resources

Make your revision easier

Maths

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

Equations

How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

Thinking about a maths degree?

Chat with other maths applicants

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.