Input/Output for pairs of numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 30: Line 30:
V (a, b) = input().split(‘ ’).map(Int)
V (a, b) = input().split(‘ ’).map(Int)
print(do_stuff(a, b))</lang>
print(do_stuff(a, b))</lang>


=={{header|Ada}}==
There can be newlines before or between numbers. The pairs may be on separate lines or the same line.
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
count : Integer;
First : Integer;
Second : Integer;
begin
Get (count);
for I in 1 .. count loop
Get (First);
Get (Second);
Put (Item => First + Second, Width => 1);
New_Line;
end loop;
end Main;</lang>
{{output}}
Output using the example input:
<pre>
3
30
2
102
10
</pre>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==

Revision as of 16:54, 22 February 2021

Input/Output for pairs of numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

From lines of input starting with a line containing the numbers of pairs to follows, followed by that number of pairs of integers separated by a space on separate lines from STDIN, output the sum of each pair to STDOUT.


Sample input with corresponding output

Input

5
1 2
10 20
-3 5
100 2
5 5

Output

3
30
2
102
10

11l

Translation of: Python

<lang 11l>F do_stuff(a, b)

  R a + b

V t = Int(input()) L 1..t

  V (a, b) = input().split(‘ ’).map(Int)
  print(do_stuff(a, b))</lang>


Ada

There can be newlines before or between numbers. The pairs may be on separate lines or the same line. <lang Ada>with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is

  count  : Integer;
  First  : Integer;
  Second : Integer;

begin

  Get (count);
  for I in 1 .. count loop
     Get (First);
     Get (Second);
     Put (Item => First + Second, Width => 1);
     New_Line;
  end loop;

end Main;</lang>

Output:

Output using the example input:

3
30
2
102
10

ALGOL 68

Simple version - there can be newlines before or between the numbers <lang algol68># read a number from stand in then read and add that many pairs of numbers from stand in #

  1. 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

   INT a, b;
   read( ( a, b, newline ) );
   print( ( a + b, newline ) )

OD </lang> Strict version - the pairs of numbers must appear on the same line. <lang algol68>

  1. read a number from stand in then read and add that many pairs of numbers from stand in #
  2. and write the sum to stand out. If non integer data is supplied, a runtime error will occur #
  3. This version does not allow the pairs of numbers to be spread over several lines #

STRING line; FILE numbers; associate( numbers, line ); TO ( INT n

  ; read( ( line, newline ) )
  ; reset( numbers )
  ; get( numbers, ( n ) )
  ; n
  )

DO

   INT a, b;
   read( ( line, newline ) );
   reset( numbers );
   get( numbers, ( a, b ) );
   print( ( a + b, newline ) )

OD</lang>

Output:
         +3
        +30
         +2
       +102
        +10

AWK

<lang awk>NR == 1 {n=$1; next} NR > n+1 {exit} {print $1+$2}</lang>

Batch File

<lang dos> @echo off setlocal enabledelayedexpansion

set /p pairs=

for /l %%i in (1,1,%pairs%) do set /p pair%%i= for /l %%i in (1,1,%pairs%) do (

 for %%j in (!pair%%i!) do (
   set /a sum%%i+=%%j
 )

)

for /l %%i in (1,1,%pairs%) do echo !sum%%i! pause>nul </lang>

Input:
5
10 10
5 6
-3 2
-6 -8
111 2
Output:
20
11
-1
-14
113

BBC BASIC

The specification is a bit ambiguous, but I understood it as wanting us to read all the numbers in first 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. <lang bbcbasic>INPUT n% DIM pairs%(n% - 1, 1) FOR i% = 0 TO n% - 1

 INPUT s$
 pairs%(i%, 0) = VAL(LEFT$(s$, INSTR(s$, " ")))
 pairs%(i%, 1) = VAL(MID$(s$, INSTR(s$, " ")))

NEXT FOR i% = 0 TO n% - 1

 PRINT pairs%(i%, 0) + pairs%(i%, 1)

NEXT</lang> With the sample inputs:

?5
?1 2
?10 20
?-3 5
?100 2
?5 5
         3
        30
         2
       102
        10

C

<lang C>#include <stdio.h>

  1. include <stdlib.h>

int main(void) { int i, n, a, b, *f; scanf("%d", &n); f = malloc(sizeof(*f) * n);

for (i = 0; i < n; i++) { if (2 != scanf("%d %d", &a, &b)) abort(); f[i] = a + b; }

for (i = 0; i < n; i++) printf("%d\n", f[i]);

return 0; }</lang>

Output for example input

3
30
2
102
10

C#

<lang csharp>using System; using static System.Linq.Enumerable;

public class Program {

   static void Main(string[] args)
   {

int count = Convert.ToInt32(Console.ReadLine()); for (int line = 0; line < count; line++) {

           Console.WriteLine(Console.ReadLine().Split(' ').Sum(i => Convert.ToInt32(i)));

}

   }

}</lang>

Output:
3
30
2
102
10

C++

Modified in order to take in all inputs and then give the output, the original gave the output for each pair immediately. <lang cpp>

  1. include <iostream>

using namespace std;

int doStuff(int a, int b) {

   return a + b;

}

int main() {

int t, **list;

cin >> t;

list = new int*[t];

for(int j=0; j<t; j++){

list[j] = new int[2]; cin >> list[j][0]>> list[j][1];

}

cout << endl;

for(int j=0;j<t;j++){ cout << doStuff(list[j][0], list[j][1]) << endl;; } return 0; } </lang>

Run as per given input

5
1 2
10 20
-3 5
100 2
5 5

3
30
2
102
10

D

This works with any number of integers on lines. <lang d>void main() {

   import std.stdio, std.string, std.conv, std.algorithm;
   foreach (immutable _; 0 .. readln.strip.to!uint)
       readln.split.to!(int[]).sum.writeln;

}</lang>

Factor

<lang factor> USING: io math.parser prettyprint sequences splitting ; IN: rosetta-code.pair-output

process-line ( str -- n )
   " " split [ string>number ] map-sum ;
main ( -- ) lines 1 tail [ process-line ] map [ . ] each ;

MAIN: main </lang>

Output:
3
30
2
102
10

Fortran

Works with: Fortran version 95 and later

<lang fortran>program i_o_pairs

 implicit none
 integer :: npairs
 integer :: i
 integer, allocatable :: pairs(:,:)
 read(*,*) npairs
 allocate(pairs(npairs,2))

 do i = 1, npairs
   read(*,*) pairs(i,:)
 end do
 write(*, "(i0)") sum(pairs, 2)

end program</lang>

FreeBASIC

<lang freebasic>' FB 1.05.0 Win64

Dim As UInteger n Dim As Integer x, y Input "", n Dim sums(1 To n) As Integer For i As Integer = 1 To n

 Input "", x, y
 sums(i) =  x + y

Next Print For i As Integer = 1 To n

 Print Str(sums(i))

Next Sleep</lang>

Output:
5
1 2
10 20
-3 5
100 2
5 5

3
30
2
102
10

Go

<lang go>package main

import ( "fmt" "log" )

func main() { var lines int n, err := fmt.Scanln(&lines) if n != 1 || err != nil { log.Fatal(err) }

var a, b int for ; lines > 0; lines-- { n, err = fmt.Scanln(&a, &b) if n != 2 || err != nil { log.Fatal(err) } fmt.Println(a + b) } }</lang>

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.

<lang Haskell>main = do

   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</lang>

J

<lang J> $ cat <<EOF | jconsole -js '([: exit 0: [: smoutput [: ,. [: ({. {. }.) [: (+/"1) [: (0&".;._2) (1!:1)) 3' > 5 > 1 2 > 10 20 > -3 5 > 100 2 > 5 5 > EOF

 3
30
 2

102

10

</lang> Considerably simpler than [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.

Java

<lang java>import java.util.Scanner;

public class Main {

public static int doStuff(int a, int b){ int sum = a+b; return sum; }

public static void main(String[] args) { Scanner in = new Scanner(System.in);

int n = in.nextInt(); for(int i=0; i<n; i++){ int a = in.nextInt(); int b= in.nextInt(); int result = doStuff(a, b); System.out.println(result); } } }</lang>

Julia

<lang julia>parseints() = (a = split(strip(readline()), r"\s+"); map(x -> parse(Int, x), a))

const lines = parseints()[1]

for _ in 1:lines

   println(sum(parseints()))

end

</lang>

Output:
3
5 6
11
8 2
10
9 23
32

Kotlin

<lang scala>// version 1.0.6

import java.util.Scanner

fun main(args: Array<String>) {

   val sc = Scanner(System.`in`)  // note: backticks required as 'in' is a Kotlin keyword
   val n = sc.nextInt()
   val x = IntArray(n)
   val y = IntArray(n)
   for (i in 0 until n) {
       x[i] = sc.nextInt()
       y[i] = sc.nextInt()  
   }
   println()
   for (i in 0 until n) println(x[i] + y[i])

}</lang> Sample input/output:

Output:
5
1 2
10 20
-3 5
100 2
5 5

3
30
2
102
10

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. <lang Lua>local intTab, numLines, sum = {}, io.read() for i = 1, numLines do

   sum = 0
   for number in io.read():gmatch("%S+") do sum = sum + number end
   table.insert(intTab, sum)

end for _, result in pairs(intTab) do print(result) end</lang>

OCaml

<lang ocaml>let () =

 let n = int_of_string (input_line stdin) in
 for i = 1 to n do
   let line = input_line stdin in
   match String.split_on_char ' ' line with
   | a::b::[] ->
       let x = int_of_string a + int_of_string b in
       print_int x;
       print_newline ()
   | _ ->
       raise (Invalid_argument "wrong input")
 done</lang>
Output:
$ cat input.txt
5
1 2
10 20
-3 5
100 2
5 5
$ cat input.txt | ocaml pairs.ml 
3
30
2
102
10

PARI/GP

Interestingly, this task is not possible to implement directly in GP, since input(), like the gp REPL itself, ignores spaces. One must use PARI: <lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <pari/pari.h>

int main(void);

int main() {

 int i, n, a, b;
 GEN f, sum;
 pari_sp ltop;
 
 // 1 MB stack, not using prime table
 pari_init(1000000, 0);
 
 scanf("%d", &n);
 GEN f = cgetg(n+1, t_VEC);
 for (i = 1; i <= n; i++) {
   if (2 != scanf("%d %d", &a, &b)) abort();
   ltop = avma;

// Add a and b in PARI sum = addii(stoi(a), stoi(b));

// Store the sum in a vector, collecting garbage as you go. gel(f, i) = gerepileupto(ltop, sum);

 }
 pari_printf("%Ps", f);
 return 0;

}</lang> Of course for such a simple task this has very little advantage over C, but it does demonstrate the general principle.

Perl

Reads from STDIN, added any pair of numbers. <lang perl>$n = scalar <>;

for (1..$n) {

   ($a,$b) = split ' ', <>;
   print $a + $b . "\n";

}</lang>

Phix

<lang Phix>string line = gets(0) sequence r = scanf(trim(line),"%d"), s = {} if length(r)!=1 then

   puts(1,"input not a number\n")
   abort(0)

end if puts(1,"\n") for i=1 to r[1][1] do

   line = gets(0)
   r = scanf(trim(line),"%d %d")
   if length(r)!=1 then
       puts(1,"input not a pair of numbers\n")
       abort(0)
   end if
   s &= sum(r[1])
   puts(1,"\n")

end for puts(1,"===\n") ?s</lang>

Output:

(or more accurately the final state of the console)

5
1 2
10 20
-3 5
100 2
5 5
===
{3,30,2,102,10}

PowerShell

<lang PowerShell>

  1. script.ps1

$in, $line = (Get-Content $args[0]), 0 $nb = $in[$line++] 1..$nb | foreach {

   $sum = 0
   $in[$line++].Split() | foreach{ $sum += $_} 
   $sum

}

  1. ./script file.txt

</lang>

Python

<lang python>def do_stuff(a, b): return a + b

t = input() for x in range(0, t): a, b = raw_input().strip().split() print do_stuff(int(a), int(b))</lang>

Python: Alternative

Or without the function do_stuff() and that works for Python 3 and Python 2: <lang python>>>> try: raw_input except NameError: raw_input = input

>>> for i in range(int(raw_input())): print(sum(int(numberstring) for numberstring in raw_input().strip().split()))


5 1 2 3 10 20 30 -3 5 2 100 2 102 5 5 10 >>> </lang> (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. <lang python>>>> for i in range(int(raw_input('lines: '))): print(sum(int(numberstring)

                 for numberstring in raw_input('two numbers: ').strip().split()))


lines: 5 two numbers: 1 2 3 two numbers: 10 20 30 two numbers: -3 5 2 two numbers: 100 2 102 two numbers: 5 5 10 >>> </lang>

Racket

<lang Racket>#lang racket

(define line-number (read)) ;reads all kind of things
(for ([i (in-range line-number)])
(displayln (+ (read) (read))))

(define line-count (string->number ;only reads numbers

                   (string-trim (read-line)))) 

(for ([i (in-range line-count)])

 (displayln (apply + 
                   (map string->number 
                        (string-split (read-line))))))</lang>

Raku

(formerly Perl 6) <lang perl6>for ^get() { say [+] get.words }</lang> 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, get can read from a file specified as a command-line argument, but defaults to taking STDIN if no filename is specified.

REXX

This version isn't limited to summing integers, any form of number that REXX supports can be used. <lang rexx>/*REXX pgm reads a number (from the CL), reads that number of pairs, & writes their sum.*/

                                                /*all input is from the  Command Line. */
    do  linein()                                /*read the number of pairs to be add*ed*/
    $=linein()                                  /*read a line (a record) from the C.L. */
    say word($, 1)   +   word($, 2)             /*display the sum of a pair of numbers.*/
    end   /*linein() */
                                                /*stick a fork in it,  we're all done. */</lang>

Ring

<lang ring>

  1. Project : Input/Output for Pairs of Numbers

pairs = ["5", "1 2", "10 20", "-3 5", "100 2", "5 5"] for n = 1 to len(pairs)

   nr = 0
   for p = 1 to len(pairs[n])
       if substr(pairs[n], p, 1) = " "
          nr = p
       ok
   next
   if nr > 0
      n1 = number(left(pairs[n], nr - 1))
      n2 = number(right(pairs[n], len(pairs[n]) - nr + 1))
      n3 = n1 + n2
      see n3 + nl
   ok

next </lang> Output:

3
30
2
102
10

Ring: Alternative

<lang ring>

  1. Project : Input/Output for Pairs of Numbers (Alternative)

pairs = ["5", "1 2", "10 20", "5 -3", "100 2", "5 5"] for n = 1 to len(pairs)

   nr = 0
   for p = 1 to len(pairs[n])
       if substr(pairs[n], p, 1) = " "
          pairs[n] = substr(pairs[n], " ", "+")
          nr = p
       ok
   next
   if nr > 0
      eval("ev = " + pairs[n])
      see ev + nl
   ok

next >>> </lang>

3
30
2
102
10

Ruby

<lang ruby>n = gets.to_i n.times do

 a, b = gets.split.map(&:to_i)
 puts a + b

end</lang>

Scala

<lang Scala>object IOPairs extends App {

 private val in = scala.io.StdIn
 private val n = in.readInt()
 for (_ <- 0 until n) {
   val Array(a, b) = in.readLine().split(" ").map(_.toInt)
   def doStuff(a: Long, b: Long): Long = a + b
   println(doStuff(a, b))
 }

} </lang>

Tcl

<lang tcl>gets stdin n while {$n > 0} {

   if {[scan [gets stdin] "%d %d" a b] == 2} {
       puts [expr {$a + $b}]
   }
   incr n -1

}</lang>

UNIX Shell

Works with: Bourne Again SHell

<lang bash>read n while (( n > 0 )); do

   read a b
   echo $((a+b))
   ((n--))

done</lang>

Ursa

<lang ursa>decl int amount set amount (in int console)

decl int<> ints for (decl int i) (< i amount) (inc i)

       decl string input
       set input (in string console)
       append (int (split input " ")<0>) (int (split input " ")<1>) ints

end for

out endl console

for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))

       out (int (+ ints ints<(int (+ i 1))>)) endl console

end for</lang> Networked version. Runs on port 20000. <lang ursa>decl serverport sp decl port p sp.attach 20000 set p (sp.getconn)

decl int amount set amount (in int p)

decl int<> ints for (decl int i) (< i amount) (inc i)

       decl string input
       set input (in string p)
       append (int (split input " ")<0>) (int (split input " ")<1>) ints

end for

out endl p

for (set i 0) (< i (size ints)) (set i (int (+ 2 i)))

       out (int (+ ints ints<(int (+ i 1))>)) endl p

end for</lang>

Wren

This assumes that both Stdin and Stdout are connected to a terminal. <lang ecmascript>import "io" for Stdin

var output = Fn.new { |pairs| pairs.each { |p| System.print(p[0] + p[1]) } }

var n = Num.fromString(Stdin.readLine()) if (!n || !n.isInteger || n < 1) Fiber.abort("Number of pairs must be a positive integer.") var pairs = [] for (i in 0...n) {

   var line = Stdin.readLine()
   var sp = line.split(" ")
   if (sp.count != 2) Fiber.abort("Each line must contain 2 integers, separated by a space.")
   var p1 = Num.fromString(sp[0])
   if (!p1 || !p1.isInteger) Fiber.abort("First value is not an integer.")
   var p2 = Num.fromString(sp[1])
   if (!p2 || !p2.isInteger) Fiber.abort("Second value is not an integer.")
   pairs.add([p1, p2])

} System.print() output.call(pairs)</lang>

Output:

Sample input/output:

5
1 2
10 20
-3 5
100 2
5 5

3
30
2
102
10

zkl

Using the console as the input stream: <lang zkl>fcn pairs{

  n:=ask("num pairs: ").toInt(); 
  do(n){ask("1 pair: ").split(" ").sum().println()}

}</lang>

Output:
pairs()
num pairs: 5
1 pair: 1 2
3
1 pair: 10 20
30
1 pair: -3 5
2
1 pair: 100 2
102
1 pair: 5 5 
10