Dear monks, over the last few days I had this idea of writing a program that simulates the arithmetic addition on paper but with the strict condition that arrays for the numbers and the result should be the feeding and output mechanisms of the program, I somewhat succeeded at making the program work for the cases where either one of the arrays is longer than the other one and where both arrays are of the same length (much wrangling was involved btw). The program seems to work fine after all, it adds two numbers together and where the summation is >= 10 it carries over 1 to the following position to be evaluated. My very humble logical abilities crashed at this situation, an example:
@a=qw(1 0 9 15 6); @b=qw( 1 1 4 );
I wanted to treat the list (1,0,9,15,6) as the number (109156)but I failed because the program output is
1 0 9 15 6 1 1 4 __________+ 1 1 1 7 0 (which corresponds to the program logic)
whereas the expected output (and the correct one), should have been
1 0 9 15 6 1 1 4 __________+ 10 9 2 7 0 (which corresponds to the rules of math)
I wanted to avoid converting the entire arrays to strings and instead pop an array element to a string one at a time and unshift a string to an array one at a time. How can I extend this logic to correspond to math rules and give me the desired output even if the sum of two opposite elements from an array were larger than 20 or 30..etc for that matter?. any suggestions would be cherished and any criticism is welcome too.here is my code, uncomment commented lines to try the different scenarios I thought of but only one case should be uncommmented at a time since I am using the same variable names for every different case of the three cases.
#!/usr/local/bin/perl use strict; use warnings; my (@a, @b, @result); my ($a_len, $b_len,$a, $b, $sum); @a=qw(1 0 9 15 6); #case1 when @a > @b @b=qw( 1 1 4 ); #@a = qw( 1 5 9); #case2 when @a < @b #@b = qw(10 9 3 6); #case3 when @a == @b #@a = qw(1 1 6 9 2); #@b = qw(2 1 4 4 8); $a_len = @a; #to make decisions on which case to follow $b_len = @b; print "@a\n"; print "@b\n"; print "__________+\n"; while(@a || @b){ if($a_len > $b_len){ $a = pop @a; $b= @b !=0? pop @b : 0; #pop $b or assign zero wher +e empty. $sum = $a + $b ; if($sum >= 10 && @a!=0){ $sum -=10; #assign to sum the ones posi +tion and strip the tens position. $a[-1]+=1; # add one to the following p +osition. } unshift @result, $sum; }elsif($a_len < $b_len){ $a= @a!=0? pop @a: 0; # pop $a or ass +ign zero where empty $b = pop @b; $sum = $a + $b ; if($sum >= 10 && @b !=0){ $sum -=10; $b[-1]+=1; } unshift @result, $sum; }else{ $a = pop @a; $b = pop @b; $sum = $a+$b; if($sum >= 10){ $sum -=10; $a[-1]+=1; } unshift @result, $sum; } } print "@result";
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

In reply to Arithmetical Simulation by biohisham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.