Results 1 to 4 of 4

Thread: Need help on programming (FORTRAN)

  1. #1

    Default Need help on programming (FORTRAN)

    Hi,

    Let's say I have 7092 apples and I want to separate the 7092 apples evenly to 8 baskets. Unfortunately, 7092/8 is 886.5 which is not possible. Hence the next best way (first scenario) is to give 886 apples to the first 7 baskets each, and the last one to have 890 apples.

    However, this is still not the best way to separately the apples evenly. An even better way (second scenario) would be to give the first 4 baskets 887 apples, and the last 4 baskets to have 886 apples each.

    So how will the pseudo-code look like for the second scenario?

    Thanks in advance.

    Regards,
    TM

  2. #2
    Member
    Join Date
    Apr 2003
    Location
    East
    Posts
    379

    Default

    a simple logic will look like this: (uncompiled, simulated)

    dim x(8);

    for i=1 to 8

    if i<5

    for j=1 to 887
    add 1 to x(i)
    next j
    endfor

    else

    for j=1 to 886
    add 1 to x(i)
    next j
    endfor

    endif

    next i
    endfor



  3. #3
    Member
    Join Date
    Jan 2002
    Location
    ClubSNAP East
    Posts
    1,702

    Default

    Do an integer division or modulo. I know nuts about FORTRAN but in Java, for example, you can have something like:

    7 % 2 = 1; // remainder is returned

    So logic-wise, you can check if the remainder ain't zero, and if so, you can assign more to the other / last basket.

    HTH,

    :Later,
    Last edited by Pinoy; 22nd May 2003 at 03:40 PM.

  4. #4
    Moderator
    Join Date
    Jan 2002
    Location
    Singapore
    Posts
    1,165

    Default

    How about this?

    -----
    NUMBER_OF_APPLES=7092
    NUMBER_OF_BASKET=8
    EACH_BASKET = INTEGER (NUMBER_OF_APPLES/ NUMBER_OF_BASKET)
    REMAINDER= MOD (NUMBER_OF_APPLES/ NUMBER_OF_BASKET)

    I=0
    DO NUMBER_OF_BASKET, 1
    I=I+1
    BASKET(I)=EACH_BASKET
    END DO

    I=0
    DO REMAINDER, 1
    I=I+1
    BASKET(I)=BASKET(I)+1
    END DO

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •