Write a program that reads several pairs of numbers and, for each pair, prints the sum. The standard input and output devices are usually the keyboard and screen of a command line session. Here is an example session with this program:
$ ./add
0 0
0
3 7
10
12 21
33
-123 321
198
1234 4321
5555
Each line of input contains two numbers x and y where -2 000 000 000 ≤ x, y ≤ 2 000 000 000
For each line of input there should be a line of output with the result of adding x to y.
Numbers may be given in the input with leading zeroes. The output should have no leading zeroes. Input is terminated by the end-of-file (EOF)
0 0
3 7
12 21
-123 321
1234 4321
0
10
33
198
5555
add
functionIn order to get a full score,
your program should be implemented using an add
function
that receives two integers as arguments and returns an integer.
Please refer to the information for the chosen language:
int add(int x, int y);
def add(x,y):
add :: Int -> Int -> Int
int add(int x, int y);
public static int Add(int x, int y)
inside a class Program
public static int add(int x, int y)
inside a public class Add
function add(x, y)
function add(x, y)
def add(x,y)
(define (add x y) ...)
(define (add x y) ...)
The add
function should not print anything.
It should just perform the computation and return an integer.
The function and input/output processing
must exist in the same program.
Create a single submission with the function and main program.
If you are confused by the above,
try earning a partial score first.
Specifically for this exercise when using Python, JavaScript, Lua or Ruby,
avoid using sys.exit()
, process.exit()
, os.exit()
or exit
,
as your program is appended with some extra assertions in one of the test sets.
Submit your solution to be graded according to the following list:
add
functionIf you do not know where to start, read the Computer Science by Example book. First, setup your environment then learn the programming basics. Here are some hints:
Automated judge: Keep in mind that when your program is submitted it will not be run by a human but instead by an automated judge. Instructions should be followed exactly or the judge will not give you a full score.
Your program should not print messages like
Please type two numbers:
or The sum is:
.
Instead, just print the result
followed by a line break
as in the example output.
Produce output as you go: You do not need to accumulate input and then produce everything at the end. It is enough to produce output as you go. As soon as you read a pair of numbers write their sum to standard output.
Detecting the end of file. In this exercise, input is terminated by the end-of-file (EOF). Here are ways to detect EOF in C, Python and Haskell:
In C.
The scanf
function returns the numbers of items read from stdin
.
Since this exercise requires you to read two numbers each line,
you can compare scanf
’s result to one as a while
condition:
while (scanf(...)==2) {
...
}
Which translates to, “while you’re able to read two items from standard input, do …”
In Python.
The pattern for line in sys.stdin:
can be used
to create a loop where a file is processed line by line
until the end-of-file (EOF).
In Haskell.
You can use interact
to declare the main function
and implement your solution as a function from String
to String
:
io :: String -> String
io = ...
main :: IO
main = interact io
EOF is then represented as the nil list constructor (""
or []
)
at the end of the argument String
.
On the terminal, you can simulate the end-of-file (EOF) by holding “Ctrl” and pressing “D”, i.e., Ctrl-D.
Beware of leading zeroes.
C users should beware of leading zeroes.
Use %d
instead of %i
to avoid treating numbers with leading zeroes as octals.
try next: mult
Copyright © 2020-2023 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/add