Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Iteration returns zero

CenkayArkan
Explorer
0 Likes
1,267

Hi experts,

I'm facing an issue. I solved the problem but I wonder what is happening? What am I missing? I tried to calculate lcm(least common multiple) with the number range. After iteration, the result returns me zero.

When I changed it to an attribute instead of returning a parameter, it worked well. And also I tried to change my gcd method with a function, it also worked.

I just wonder what is the reason for this problem. I'm sharing my code and screen view.

I don't understand why rv_rcd parameter returns zero.

CLASS lcl_math DEFINITION.
  PUBLIC SECTION.
    TYPES ty_range TYPE TABLE OF int4.
    METHODS gcd IMPORTING iv_val1       TYPE int4
                          iv_val2       TYPE int4
                RETURNING VALUE(rv_gcd) TYPE int4.
    METHODS findlcm IMPORTING it_range         TYPE ty_range
                    RETURNING VALUE(rv_lcm) TYPE int4.
ENDCLASS.


CLASS lcl_math IMPLEMENTATION.


  METHOD gcd.
    IF ( iv_val2 = 0 ).
      rv_gcd = iv_val1.
      WRITE : / 'RV_GCD: ' , rv_gcd.
      RETURN.
    ENDIF.
    me->gcd( iv_val1 = iv_val2 iv_val2 = iv_val1 MOD iv_val2 ).
  ENDMETHOD.


  METHOD findlcm.
    DATA : lv_counter      TYPE int4 VALUE 2,
           lv_gcd TYPE int4.
    rv_lcm = it_range[ 1 ].
    WHILE lv_counter <= lines( it_range ).
      DATA(val) = it_range[ lv_counter ].
      TRY.
          lv_gcd = me->gcd( iv_val1 = val iv_val2 = rv_lcm ).
          WRITE : / 'LV_GCD: ' , lv_gcd.
          rv_lcm = ( val * rv_lcm ) / lv_gcd.
        CATCH cx_sy_zerodivide.
      ENDTRY.
      lv_counter = lv_counter + 1.
    ENDWHILE.
  ENDMETHOD.


ENDCLASS.


START-OF-SELECTION.
  DATA(gref_math) = NEW lcl_math( ).
  gref_math->findlcm(
    EXPORTING
      it_range = VALUE #( ( 10 ) ( 15 ) ( 6 ) ( 20 ) )
    RECEIVING
      rv_lcm = DATA(gv_lcm)
  ).
  ULINE.
  WRITE gv_lcm.

1 ACCEPTED SOLUTION
Read only

VXLozano
Active Contributor
1,174
METHOD gcd.
    IF ( iv_val2 = 0 ).
      rv_gcd = iv_val1.
      WRITE : / 'RV_GCD: ' , rv_gcd.
      RETURN.
    ENDIF.
    me->gcd( iv_val1 = iv_val2 iv_val2 = iv_val1 MOD iv_val2 ). "<--- not assigned to anything
ENDMETHOD.

If not, you call the method again, without assigning its returning value, so the method returns zero.

2 REPLIES 2
Read only

VXLozano
Active Contributor
1,175
METHOD gcd.
    IF ( iv_val2 = 0 ).
      rv_gcd = iv_val1.
      WRITE : / 'RV_GCD: ' , rv_gcd.
      RETURN.
    ENDIF.
    me->gcd( iv_val1 = iv_val2 iv_val2 = iv_val1 MOD iv_val2 ). "<--- not assigned to anything
ENDMETHOD.

If not, you call the method again, without assigning its returning value, so the method returns zero.

Read only

0 Likes
1,174

Thank you so much 🙂