Input/Output for pairs of numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(add Tcl)
(add Bash)
Line 169: Line 169:
incr n -1
incr n -1
}</lang>
}</lang>

=={{header|Unix Shell}}==

{{works with|Bourne Again SHell}}

<lang bash>read n
while (( n > 0 )); do
read a b
echo $((a+b))
((n--))
done</lang>

Revision as of 21:50, 17 December 2013

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

AWK

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

C++

<lang cpp>#include <iostream> using namespace std;

int doStuff(int a, int b) {

   return a + b;

}

int main() { int t; cin >> t; for(int j=0; j<t; j++){ int a; int b; cin >> a; cin >> b; cout << doStuff(a, b) << endl;; } return 0; }</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>

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>


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.

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>

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, then reads that # of pairs, writes their sum.*/

      do  linein()                    /*read the number of pairs to add*/
      x=linein()                      /*read a line (record) from input*/
      say word(x,1) + word(x,2)       /*write the sum of a pair of nums*/
      end
                                      /*stick a fork in it, we're done.*/</lang>

Ruby

<lang ruby>def do_stuff(a, b) a + b end

t = gets.to_i for i in 1..t do a, b = gets.strip.split.map {|i| i.to_i} puts do_stuff(a, b) end</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>