How to resolve the algorithm A+B step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm A+B step by step in the Action! programming language

Table of Contents

Problem Statement

A+B   ─── a classic problem in programming contests,   it's given so contestants can gain familiarity with the online judging system being used.

Given two integers,   A and B. Their sum needs to be calculated.

Two integers are written in the input stream, separated by space(s):

The required output is one integer:   the sum of A and B.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm A+B step by step in the Action! programming language

Source code in the action! programming language

BYTE FUNC Find(CHAR ARRAY s CHAR c BYTE POINTER err)
  BYTE i
  FOR i=1 TO s(0)
  DO
    IF s(i)=c THEN
      err^=0 RETURN (i)
    FI
  OD
  err^=1
RETURN (0)

INT FUNC Decode(CHAR ARRAY s BYTE start,stop BYTE POINTER err)
  CHAR ARRAY tmp(20),tmp2(20)
  INT value

  IF s(start)='+ THEN
    start==+1
  FI
  SCopyS(tmp,s,start,stop)
  value=ValI(tmp)
  
  ;Check if conversion failed
  IF value=0 AND s(start)#'0 THEN
    err^=1 RETURN (0)
  FI

  ;Check if value is out of range
  IF value<-1000 OR value>1000 THEN
    err^=1 RETURN (0)
  FI

  err^=0
RETURN (value)

PROC Main()
  CHAR ARRAY s(20)
  BYTE pos,err,err2,value
  INT a,b,sum

  DO
    PrintE("Enter two integer numbers between -1000 and 1000, separated by a space or Q for quit")
    InputS(s)
    IF s(0)=1 AND (s(1)='Q OR s(1)='q) THEN
      EXIT
    FI
  
    pos=Find(s,' ,@err)
    IF err=0 THEN
      a=Decode(s,1,pos-1,@err)
      b=Decode(s,pos+1,s(0),@err2)
      err=err OR err2
    FI

    IF err=0 THEN
      sum=a+b
      PrintF("Their sum is %I%E",sum)
    ELSE
      PrintE("Invalid input!")
    FI
    PutE();
  OD
RETURN

  

You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Clojure programming language
You may also check:How to resolve the algorithm Lychrel numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm World Cup group stage step by step in the Phix programming language
You may also check:How to resolve the algorithm Morse code step by step in the VBA programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Forth programming language