Something goes wrong with Array Check Sum Calculation ( java) in problem 23

Back to Problem Solutions forum

shatseli     2020-02-13 06:46:13

Dear Brothers and Sisters,

Please kindly advice :

I copied the code used working perfectly for problem 17 solution (Array Check Sume ) into source for this Bubble in Array problem #23 ( please see below ), but something goes wrong - the value of check sume is differs from the suggested by task solution. To avoid the overflow of maximum long integer value I declared the variable for arr_chk_sume as long and always redefine it after dividing by modulo operation.

Thank you

final int Limit = 10000007;
final int  Seed  = 113;
long arr_chk_sume = 0;

Scanner sc = new Scanner(System.in);

// 1.0 fill_Array();
ArrayList<Integer> list_values = new ArrayList<Integer>();

    int value = sc.nextInt();
    while ( value != - 1 )
    {
        // 1. calculate check sume 
        arr_chk_sume  = ( arr_chk_sume + value )*Seed;
        if (arr_chk_sume > Limit)                // to avoid overflow
            arr_chk_sume = arr_chk_sume % Limit;
Rodion (admin)     2020-02-13 07:05:59
User avatar

Hi Eli!

Your code shown here has minor flaw but I'm sure problem is in something different. I looked into your full solution and I think you do something strange here: you calculate checksum BEFORE doing array processing. But problem means that you input array, do processing, then calculate checksum and output it - and number of swaps.

P.S. I dare say your code looks much more cleaner nowadays! That's good! However I dare add one more recommendation - if you find it necessary to mark some part with comment like // 1.0 fill_Array(); - just cut this part out and place it in separate function called fillArray or calculateChecksum. Don't put all stuff in single main function, this is harder to handle for you when code grows :) Also there still is certain inconsistency in using capital letters and underscores in identifiers. And in putting spaces somewhere and lacking them in other places. I hope you'll try Go language in future, it is more strict to such things :)

P.P.S. About minor flaw mentioned:

if (arr_chk_sume > Limit)                // to avoid overflow
        arr_chk_sume = arr_chk_sume % Limit;

first of all, arr_chk_sume > Limit is not correct. When arr_chk_sume == Limit you need this too.

but then, you never need this condition. if arr_chk_sume < Limit then modulo will work anyway! So you can replace these lines with single

arr_chk_sume = (arr_chk_sume + value) * Seed % Limit;
shatseli     2020-02-13 14:40:53

Dear Rodion, I am very hight appreciate your rapid, quick and detailed responce and will try further to follow your recommendations !

Please feel free to point me your ANY crytical note regarding my code. I definetly thought about to separate the code into methods after all stuff will be working corectly.

Now pls. KINDLY ADVICE me the following :

  1. regarding your note about the way of array check sume calculation:

I thought that because the algorythm of calculation DID NOT based on array indexes itself and only on values keept inside array so I ( considering about the time economy ) placed the calculation into the block accepting those values into array elements ( because, according to my opinion, I thought I can do it on step of input flow data processing ).

  1. regarding the replacing code for SOLVED task

In case if I see my solution working properly but I am still remain not satisfied with my code and want to make it more professionaly designed - can I replace it ? If yes, how ?

Thanks in advance

Rodion (admin)     2020-02-13 16:32:49
User avatar

I definetly thought about to separate the code into methods after all stuff will be working corectly.

The matter is that (according to my own experience) when we write code in more organized way, it is usually easier to make it working :)

I thought that because the algorythm of calculation DID NOT based on array indexes

It doesn't use indices explicitly, but you may easily see that if the order of elements is changed, result is changed. It is the whole idea behind "checksum". We need it here to quickly check that after doing "bubble" transformation, your array is in the expected order...

Just try with array of two elements, e.g. [1 2] - checksum for it will be different from [2 1].

can I replace it ? If yes, how ?

That's easy :) Just reopen the same problem and resubmit the updated solution, if you want! Of course you'll need again to submit new correct answer along with it.

shatseli     2020-02-14 14:11:14

As always thanks a lot Rodion and have a nice weekend !

Please login and solve 5 problems to be able to post at forum