Input/Output for pairs of numbers: Difference between revisions
Added Easylang
(Added XPL0 example.) |
(Added Easylang) |
||
(9 intermediate revisions by 8 users not shown) | |||
Line 23:
{{trans|Python}}
<
R a + b
Line 29:
L 1..t
V (a, b) = input().split(‘ ’).map(Int)
print(do_stuff(a, b))</
=={{header|Action!}}==
<
INT sum,i
CHAR ARRAY tmp(100)
Line 65:
PrintIE(sums(i))
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_pairs_of_numbers.png Screenshot from Atari 8-bit computer]
Line 85:
=={{header|Ada}}==
There can be newlines before or between numbers. The pairs may be on separate lines or the same line.
<
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 100:
New_Line;
end loop;
end Main;</
{{output}}
Output using the example input:
Line 113:
=={{header|ALGOL 68}}==
Simple version - there can be newlines before or between the numbers
<
# and write the sum to stand out. If non integer data is supplied, a runtime error will occur #
TO ( INT n; read( ( n, newline ) ); n ) DO
Line 120:
print( ( a + b, newline ) )
OD
</syntaxhighlight>
Strict version - the pairs of numbers must appear on the same line.
<
# read a number from stand in then read and add that many pairs of numbers from stand in #
# and write the sum to stand out. If non integer data is supplied, a runtime error will occur #
Line 141:
get( numbers, ( a, b ) );
print( ( a + b, newline ) )
OD</
{{out}}
<pre>
Line 150:
+10
</pre>
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 GOSUB 230"INPUT LINE"
110 LET N = VAL (L$) - 1
120 IF N < 0 THEN END
130 DIM SUM(N)
140 FOR I = 0 TO N
150 GOSUB 330"SUM PAIR FROM INPUT LINE"
160 LET SUM(I) = S
170 NEXT I
190 FOR I = 0 TO N
200 PRINT SUM(I)
210 NEXT
220 END
230 LET L$ = ""
240 LET C$ = ""
250 FOR C = 0 TO 1 STEP 0
260 LET L$ = L$ + C$
270 GET C$
280 PRINT CHR$ (0)C$;
290 LET C = C$ = CHR$ (13)
300 NEXT C
310 LET C = FRE (0)
320 RETURN
330 GOSUB 230"INPUT LINE"
340 FOR C = 1 TO LEN(L$)
350 IF MID$(L$, C, 1) <> " " THEN NEXT C
360 S = VAL(MID$(L$, 1, C - 1)) + VAL(MID$(L$, C + 1))
370 RETURN</syntaxhighlight>
'''Input'''
<pre>
5
1 2
10 20
-3 5
100 2
5 5
</pre>
{{out}}
<pre>
3
30
2
102
10
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">printNumbers: function [num]->
print [num\0 "+" num\1 "=" num\0 + num\1]
lineCount: to :integer strip input ""
do.times:lineCount [
numbers: to [:integer] split.words input ""
printNumbers numbers
]</syntaxhighlight>
{{out}}
<pre>3
2 10
2 + 10 = 12
4 5
4 + 5 = 9
-123 45
-123 + 45 = -78</pre>
=={{header|AWK}}==
<
NR > n+1 {exit}
{print $1+$2}</
=={{header|Batch File}}==
<
@echo off
setlocal enabledelayedexpansion
Line 172 ⟶ 241:
for /l %%i in (1,1,%pairs%) do echo !sum%%i!
pause>nul
</syntaxhighlight>
{{in}}
<pre>
Line 193 ⟶ 262:
=={{header|BBC BASIC}}==
The specification is a bit ambiguous, but I understood it as wanting us to read all the numbers in <i>first</i> and then print all the sums. This program does that. It could be a couple of lines shorter if we were allowed to use a comma instead of a space as separator.
<
DIM pairs%(n% - 1, 1)
FOR i% = 0 TO n% - 1
Line 202 ⟶ 271:
FOR i% = 0 TO n% - 1
PRINT pairs%(i%, 0) + pairs%(i%, 1)
NEXT</
With the sample inputs:
<pre>?5
Line 217 ⟶ 286:
=={{header|C}}==
<
#include <stdlib.h>
Line 236 ⟶ 305:
return 0;
}</
Output for example input
Line 249 ⟶ 318:
=={{header|C sharp}}==
<
using static System.Linq.Enumerable;
Line 261 ⟶ 330:
}
}
}</
{{out}}
<pre>
Line 273 ⟶ 342:
=={{header|C++}}==
<
#include <vector>
using namespace std;
Line 297 ⟶ 366:
cout << doStuff(list[j].first, list[j].second) << endl;;
}
}</
Run as per given input
Line 318 ⟶ 387:
=={{header|D}}==
This works with any number of integers on lines.
<
import std.stdio, std.string, std.conv, std.algorithm;
foreach (immutable _; 0 .. readln.strip.to!uint)
readln.split.to!(int[]).sum.writeln;
}</
=={{header|EasyLang}}==
<syntaxhighlight>
n = number input
for i to n
a[] = number strsplit input " "
print a[1] + a[2]
.
input_data
5
1 2
10 20
-3 5
100 2
5 5
</syntaxhighlight>
{{out}}
<pre>
3
30
2
102
10
</pre>
=={{header|Factor}}==
<
USING: io math.parser prettyprint sequences splitting ;
IN: rosetta-code.pair-output
Line 335 ⟶ 429:
MAIN: main
</syntaxhighlight>
{{out}}
<pre>
Line 347 ⟶ 441:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<
implicit none
Line 362 ⟶ 456:
write(*, "(i0)") sum(pairs, 2)
end program</
=={{header|FreeBASIC}}==
<
Dim As UInteger n
Line 379 ⟶ 473:
Print Str(sums(i))
Next
Sleep</
{{out}}
Line 396 ⟶ 490:
10
</pre>
=={{header|Free Pascal}}==
''See [[#Pascal|Pascal]]''
=={{header|Go}}==
<
import (
Line 420 ⟶ 517:
fmt.Println(a + b)
}
}</
=={{header|Haskell}}==
This solution will actually add any number of integers placed on each line. Additionally, after removing the bits of code that cut out the specified number of lines, the solution will sum any number of lines of integers.
<
contents <- getContents
let numberOfLines = read.head.lines$ contents
nums = map (map read.words).take numberOfLines.tail.lines$ contents
sums = map sum nums
mapM_ print sums</
=={{header|J}}==
<syntaxhighlight lang="j">
$ cat <<EOF | jconsole -js '([: exit 0: [: smoutput [: ,. [: ({. {. }.) [: (+/"1) [: (0&".;._2) (1!:1)) 3'
> 5
Line 447 ⟶ 544:
102
10
</syntaxhighlight>
Considerably simpler than [[http://rosettacode.org/wiki/Input/Output_for_Lines_of_Text#J|see explanation]] output for lines of text, this sentence is a single fork. J pads the numeric arrays of 0&".;._2 (numbers cut) with 0 . We form the +/"1 (row sums), then take the sum of the first row of the beheaded sums ({. {. }.) for display. ,. (raveled items) reshapes the vector into a column-vector shaped matrix. And the [: (cap) causes the monadic form of the verb to cap's right.
=={{header|Java}}==
<
public class Main {
Line 471 ⟶ 568:
}
}
}</
=={{header|jq}}==
Line 478 ⟶ 575:
The solution below assumes the input is in a file named input.txt, and is quite lenient about the presentation of the numbers. For example, it does not require that each pair of numbers be presented on the same line.
<syntaxhighlight lang="sh">
< input.txt jq -n '
input as $n
Line 486 ⟶ 583:
| [input,input] | add
end'
</syntaxhighlight>
{{out}}
<pre>
Line 497 ⟶ 594:
=={{header|Julia}}==
<
const lines = parseints()[1]
Line 504 ⟶ 601:
println(sum(parseints()))
end
</
<pre>
3
Line 516 ⟶ 613:
=={{header|Kotlin}}==
<
import java.util.Scanner
Line 531 ⟶ 628:
println()
for (i in 0 until n) println(x[i] + y[i])
}</
Sample input/output:
{{out}}
Line 551 ⟶ 648:
=={{header|Lua}}==
This solution will sum any number of space-separated numbers per input line, assuming the user won't input too many to store in the available RAM.
<
for i = 1, numLines do
sum = 0
Line 557 ⟶ 654:
table.insert(intTab, sum)
end
for _, result in pairs(intTab) do print(result) end</
=={{header|Nim}}==
<
let lineCount = stdin.readLine.parseInt()
Line 568 ⟶ 665:
assert fields.len == 2
let pair = fields.map(parseInt)
echo pair[0] + pair[1]</
{{Out}}
Line 580 ⟶ 677:
=={{header|OCaml}}==
<
let n = int_of_string (input_line stdin) in
for i = 1 to n do
Line 591 ⟶ 688:
| _ ->
raise (Invalid_argument "wrong input")
done</
{{out}}
Line 613 ⟶ 710:
Interestingly, this task is not possible to implement directly in GP, since <code>input()</code>, like the gp REPL itself, ignores spaces. One must use PARI:
<
#include <stdlib.h>
#include <pari/pari.h>
Line 646 ⟶ 743:
pari_printf("%Ps", f);
return 0;
}</
Of course for such a simple task this has very little advantage over C, but it does demonstrate the general principle.
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">program inputOutputForPairsOfNumbers(input, output);
var
lines: integer;
x: integer;
y: integer;
begin
readLn(lines);
for lines := 1 to lines do
begin
readLn(x, y);
writeLn(x + y)
end
end.</syntaxhighlight>
{{out}}
3
30
2
102
10
=={{header|Perl}}==
Reads from STDIN, added any pair of numbers.
<
for (1..$n) {
($a,$b) = split ' ', <>;
print $a + $b . "\n";
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
Line 678 ⟶ 796:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"===\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
(or more accurately the final state of the console)
Line 691 ⟶ 809:
{3,30,2,102,10}
</pre>
===avoiding file i/o===
And hence runnable in a browser, as well as on the desktop.<br>
User input would need to be a proper GUI rather than a console prompt, perhaps like [[Arithmetic/Integer#Phix]] or the much prettier/more polished [[Morse_code#Phix]].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"""
5
1 2
10 20
-3 5
100 2
5 5"""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input not a number"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d %d"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"input not a pair of numbers"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Output same as the last line of the above.
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
# script.ps1
Line 705 ⟶ 852:
# ./script file.txt
</syntaxhighlight>
=={{header|Python}}==
<
return a + b
Line 714 ⟶ 861:
for x in range(0, t):
a, b = raw_input().strip().split()
print do_stuff(int(a), int(b))</
===Python: Alternative===
Or without the function do_stuff() and that works for Python 3 and Python 2:
<
except NameError: raw_input = input
Line 738 ⟶ 885:
5 5
10
>>> </
(All but the first line of single numbers, (the 5), is output from the program).
===Python: With prompts===
More than is asked for by the task, but if working interactively then the following version adds prompts.
<
print(sum(int(numberstring)
for numberstring in raw_input('two numbers: ').strip().split()))
Line 759 ⟶ 906:
two numbers: 5 5
10
>>> </
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> []
$ "How many pairs? " input
quackery times
[ $ "Pair "
i^ 1+ number$ join
$ ": " join input
join
$ " + echo cr " join ]
quackery</syntaxhighlight>
{{out}}
<pre>How many pairs? 5
Pair 1: 1 2
Pair 2: 10 20
Pair 3: -3 5
Pair 4: 100 2
Pair 5: 5 5
3
30
2
102
10
</pre>
=={{header|Racket}}==
<
;(define line-number (read)) ;reads all kind of things
;(for ([i (in-range line-number)])
Line 773 ⟶ 947:
(displayln (apply +
(map string->number
(string-split (read-line))))))</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
This does more than the task asks. It will sum as many numbers as you care to put on each line, and the numbers need not be integers, but may also be a mix of rational, floating-point, or complex numbers. More subtly, <tt>get</tt> can read from a file specified as a command-line argument, but defaults to taking STDIN if no filename is specified.
=={{header|REXX}}==
This version isn't limited to summing integers, any form of number that REXX supports can be used.
<
/*all input is from the Command Line. */
do linein() /*read the number of pairs to be add*ed*/
Line 788 ⟶ 962:
say word($, 1) + word($, 2) /*display the sum of a pair of numbers.*/
end /*linein() */
/*stick a fork in it, we're all done. */</
=={{header|Ring}}==
<
# Project : Input/Output for Pairs of Numbers
Line 809 ⟶ 983:
ok
next
</syntaxhighlight>
Output:
<pre>
Line 820 ⟶ 994:
===Ring: Alternative===
<
# Project : Input/Output for Pairs of Numbers (Alternative)
Line 837 ⟶ 1,011:
ok
next
>>> </
<pre>
3
Line 844 ⟶ 1,018:
102
10
</pre>
=={{header|RPL}}==
« 1 "How many pairs" "" INPUT STR→ '''FOR''' j
"Enter pair #" j + "" INPUT STR→ +
'''NEXT'''
» '<span style=color:blue">TASK</span>' STO
{{out}}
<pre>
5: 3
4: 30
3: 2
2: 102
1: 10
</pre>
=={{header|Ruby}}==
<
n.times do
a, b = gets.split.map(&:to_i)
puts a + b
end</
=={{header|Scala}}==
<
private val in = scala.io.StdIn
private val n = in.readInt()
Line 867 ⟶ 1,055:
}
</syntaxhighlight>
=={{header|Tcl}}==
<
while {$n > 0} {
if {[scan [gets stdin] "%d %d" a b] == 2} {
Line 876 ⟶ 1,064:
}
incr n -1
}</
=={{header|UNIX Shell}}==
Line 882 ⟶ 1,070:
{{works with|Bourne Again SHell}}
<
while (( n > 0 )); do
read a b
echo $((a+b))
((n--))
done</
=={{header|Ursa}}==
<
set amount (in int console)
Line 904 ⟶ 1,092:
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
out (int (+ ints<i> ints<(int (+ i 1))>)) endl console
end for</
Networked version. Runs on port 20000.
<
decl port p
sp.attach 20000
Line 925 ⟶ 1,113:
for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))
out (int (+ ints<i> ints<(int (+ i 1))>)) endl p
end for</
=={{header|Wren}}==
This assumes that both Stdin and Stdout are connected to a terminal.
<
var output = Fn.new { |pairs| pairs.each { |p| System.print(p[0] + p[1]) } }
Line 947 ⟶ 1,135:
}
System.print()
output.call(pairs)</
{{out}}
Line 968 ⟶ 1,156:
=={{header|XPL0}}==
The input file must be redirected on the command line, for example: iopair <iopair.txt
<
for N:= 1 to IntIn(1) do
[IntOut(0, IntIn(1) + IntIn(1));
CrLf(0);
]</
{{out}}
Line 985 ⟶ 1,173:
=={{header|zkl}}==
Using the console as the input stream:
<
n:=ask("num pairs: ").toInt();
do(n){ask("1 pair: ").split(" ").sum().println()}
}</
{{out}}
<pre>
|