WeixiangYan/CodeTransOcean huggingface dataset:
Task: Write float arrays to a text file

C: #include <stdio.h>
#include <math.h>

int main(int argc, char **argv) {

   float x[4] = {1,2,3,1e11}, y[4];
   int i = 0;
   FILE *filePtr;

   filePtr = fopen("floatArray","w");

   for (i = 0; i < 4; i++) {
      y[i] = sqrt(x[i]);
      fprintf(filePtr, "%.3g\t%.5g\n", x[i], y[i]);
   }

   return 0;
}










Fortran: program writefloats
  implicit none

  real, dimension(10) :: a, sqrta
  integer :: i
  integer, parameter :: unit = 40 

  a = (/ (i, i=1,10) /)
  sqrta = sqrt(a)

  open(unit, file="xydata.txt", status="new", action="write")
  call writexy(unit, a, sqrta)
  close(unit)

contains

  subroutine writexy(u, x, y)
    real, dimension(:), intent(in) :: x, y
    integer, intent(in) :: u

    integer :: i

    write(u, "(2F10.4)") (x(i), y(i), i=lbound(x,1), ubound(x,1))
  end subroutine writexy

end program writefloats
Task: Write float arrays to a text file





Java: import java.io.*;

public class FloatArray {
    public static void writeDat(String filename, double[] x, double[] y,
                                int xprecision, int yprecision)
        throws IOException {
        assert x.length == y.length;
        PrintWriter out = new PrintWriter(filename);
        for (int i = 0; i < x.length; i++)
            out.printf("%."+xprecision+"g\t%."+yprecision+"g\n", x[i], y[i]);
        out.close();
    }

    public static void main(String[] args) {
        double[] x = {1, 2, 3, 1e11};
        double[] y = new double[x.length];
        for (int i = 0; i < x.length; i++)
            y[i] = Math.sqrt(x[i]);
        
        try {
            writeDat("sqrt.dat", x, y, 3, 5);
        } catch (IOException e) {
            System.err.println("writeDat: exception: "+e);
        }

        try {
            BufferedReader br = new BufferedReader(new FileReader("sqrt.dat"));
            String line;
            while ((line = br.readLine()) != null)
                System.out.println(line);
        } catch (IOException e) { }
    }
}






Fortran: program writefloats
  implicit none

  real, dimension(10) :: a, sqrta
  integer :: i
  integer, parameter :: unit = 40 

  a = (/ (i, i=1,10) /)
  sqrta = sqrt(a)

  open(unit, file="xydata.txt", status="new", action="write")
  call writexy(unit, a, sqrta)
  close(unit)

contains

  subroutine writexy(u, x, y)
    real, dimension(:), intent(in) :: x, y
    integer, intent(in) :: u

    integer :: i

    write(u, "(2F10.4)") (x(i), y(i), i=lbound(x,1), ubound(x,1))
  end subroutine writexy

end program writefloats
Task: Write float arrays to a text file







Python: import itertools
def writedat(filename, x, y, xprecision=3, yprecision=5):
    with open(filename,'w') as f:
        for a, b in itertools.izip(x, y):
            print >> f, "%.*g\t%.*g" % (xprecision, a, yprecision, b)




Fortran: program writefloats
  implicit none

  real, dimension(10) :: a, sqrta
  integer :: i
  integer, parameter :: unit = 40 

  a = (/ (i, i=1,10) /)
  sqrta = sqrt(a)

  open(unit, file="xydata.txt", status="new", action="write")
  call writexy(unit, a, sqrta)
  close(unit)

contains

  subroutine writexy(u, x, y)
    real, dimension(:), intent(in) :: x, y
    integer, intent(in) :: u

    integer :: i

    write(u, "(2F10.4)") (x(i), y(i), i=lbound(x,1), ubound(x,1))
  end subroutine writexy

end program writefloats
Task: Write float arrays to a text file








VB: Public Sub main()
    x = [{1, 2, 3, 1e11}]
    y = [{1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791}]
    Dim TextFile As Integer
    TextFile = FreeFile
    Open "filename" For Output As TextFile
    For i = 1 To UBound(x)
        Print #TextFile, Format(x(i), "0.000E-000"), Format(y(i), "0.00000E-000")
    Next i
    Close TextFile
End Sub



Fortran: program writefloats
  implicit none

  real, dimension(10) :: a, sqrta
  integer :: i
  integer, parameter :: unit = 40 

  a = (/ (i, i=1,10) /)
  sqrta = sqrt(a)

  open(unit, file="xydata.txt", status="new", action="write")
  call writexy(unit, a, sqrta)
  close(unit)

contains

  subroutine writexy(u, x, y)
    real, dimension(:), intent(in) :: x, y
    integer, intent(in) :: u

    integer :: i

    write(u, "(2F10.4)") (x(i), y(i), i=lbound(x,1), ubound(x,1))
  end subroutine writexy

end program writefloats
Task: Write float arrays to a text file

C: #include <stdio.h>
#include <math.h>

int main(int argc, char **argv) {

   float x[4] = {1,2,3,1e11}, y[4];
   int i = 0;
   FILE *filePtr;

   filePtr = fopen("floatArray","w");

   for (i = 0; i < 4; i++) {
      y[i] = sqrt(x[i]);
      fprintf(filePtr, "%.3g\t%.5g\n", x[i], y[i]);
   }

   return 0;
}









COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: Write float arrays to a text file



C#: using System.IO;

class Program
{
    static void Main(string[] args)
    {
        var x = new double[] { 1, 2, 3, 1e11 };
        var y = new double[] { 1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791 };

        int xprecision = 3;
        int yprecision = 5;

        string formatString = "{0:G" + xprecision + "}\t{1:G" + yprecision + "}";

        using (var outf = new StreamWriter("FloatArrayColumns.txt"))
            for (int i = 0; i < x.Length; i++)
                outf.WriteLine(formatString, x[i], y[i]);
    }
}







COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: Write float arrays to a text file


C++: template<class InputIterator, class InputIterator2>
void writedat(const char* filename,
              InputIterator xbegin, InputIterator xend,
              InputIterator2 ybegin, InputIterator2 yend,
              int xprecision=3, int yprecision=5)
{
  std::ofstream f;
  f.exceptions(std::ofstream::failbit | std::ofstream::badbit);
  f.open(filename);
  for ( ; xbegin != xend and ybegin != yend; ++xbegin, ++ybegin)
    f << std::setprecision(xprecision) << *xbegin << '\t'
      << std::setprecision(yprecision) << *ybegin << '\n';
}








COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: Write float arrays to a text file





Java: import java.io.*;

public class FloatArray {
    public static void writeDat(String filename, double[] x, double[] y,
                                int xprecision, int yprecision)
        throws IOException {
        assert x.length == y.length;
        PrintWriter out = new PrintWriter(filename);
        for (int i = 0; i < x.length; i++)
            out.printf("%."+xprecision+"g\t%."+yprecision+"g\n", x[i], y[i]);
        out.close();
    }

    public static void main(String[] args) {
        double[] x = {1, 2, 3, 1e11};
        double[] y = new double[x.length];
        for (int i = 0; i < x.length; i++)
            y[i] = Math.sqrt(x[i]);
        
        try {
            writeDat("sqrt.dat", x, y, 3, 5);
        } catch (IOException e) {
            System.err.println("writeDat: exception: "+e);
        }

        try {
            BufferedReader br = new BufferedReader(new FileReader("sqrt.dat"));
            String line;
            while ((line = br.readLine()) != null)
                System.out.println(line);
        } catch (IOException e) { }
    }
}





COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: Write float arrays to a text file







Python: import itertools
def writedat(filename, x, y, xprecision=3, yprecision=5):
    with open(filename,'w') as f:
        for a, b in itertools.izip(x, y):
            print >> f, "%.*g\t%.*g" % (xprecision, a, yprecision, b)



COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: Write float arrays to a text file








VB: Public Sub main()
    x = [{1, 2, 3, 1e11}]
    y = [{1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791}]
    Dim TextFile As Integer
    TextFile = FreeFile
    Open "filename" For Output As TextFile
    For i = 1 To UBound(x)
        Print #TextFile, Format(x(i), "0.000E-000"), Format(y(i), "0.00000E-000")
    Next i
    Close TextFile
End Sub


COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: Write float arrays to a text file




Go: package main

import (
    "fmt"
    "os"
)

var (
    x = []float64{1, 2, 3, 1e11}
    y = []float64{1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791}

    xprecision = 3
    yprecision = 5
)

func main() {
    if len(x) != len(y) {
        fmt.Println("x, y different length")
        return
    }
    f, err := os.Create("filename")
    if err != nil {
        fmt.Println(err)
        return
    }
    for i := range x {
        fmt.Fprintf(f, "%.*e, %.*e\n", xprecision-1, x[i], yprecision-1, y[i])
    }
    f.Close()
}






COBOL:        identification division.
       program-id. wr-float.
       environment division.
       input-output section.
       file-control.
           select report-file assign "float.txt"
               organization sequential.
       data division.
       file section.
       fd report-file
           report is floats.
       working-storage section.
       1 i binary pic 9(4).
       1 x-values comp-2.
        2 value 1.0.
        2 value 2.0.
        2 value 3.0.
        2 value 1.0e11.
       1 redefines x-values comp-2.
        2 x occurs 4.
       1 comp-2.
        2 y occurs 4.
       report section.
       rd floats.
       1 float-line type de.
        2 line plus 1.
         3 column 1 pic -9.99e+99 source x(i).
         2 column 12 pic -9.9999e+99 source y(i).
       procedure division.
       begin.
           open output report-file
           initiate floats
           perform varying i from 1 by 1
           until i > 4
               compute y(i) = function sqrt (x(i))
               generate float-line
           end-perform
           terminate floats
           close report-file
           stop run
           .
       end program wr-float.
Task: 21 game

C: 



#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#if __STDC_VERSION__ >= 199901L || _MSC_VER >= 1800
#include <stdbool.h>
#else
#define bool int
#define true  1
#define false 0
#endif

#define GOAL               21
#define NUMBER_OF_PLAYERS   2
#define MIN_MOVE            1
#define MAX_MOVE            3
#define BUFFER_SIZE       256

#define _(STRING) STRING



static char DESCRIPTION[] = 
    "21 Game                                                          \n"
    "                                                                 \n"
    "21 is a two player game, the game is played by choosing a number \n"
    "(1, 2, or 3) to be added to the running total. The game is won by\n"
    "the player whose chosen number causes the running total to reach \n"
    "exactly 21. The running total starts at zero.                    \n\n";

static int total;


void update(char* player, int move)
{
    printf("%8s:  %d = %d + %d\n\n", player, total + move, total, move);
    total += move;
    if (total == GOAL)
        printf(_("The winner is %s.\n\n"), player);
}


int ai()
{

#if GOAL < 32 && MIN_MOVE == 1 && MAX_MOVE == 3
    static const int precomputed[] = { 1, 1, 3, 2, 1, 1, 3, 2, 1, 1, 3, 2, 1, 1,
        3, 2, 1, 1, 3, 2, 1, 1, 3, 2, 1, 1, 3, 2, 1, 1, 3 };
    update(_("ai"), precomputed[total]);
#elif MIN_MOVE == 1 && MAX_MOVE == 3
    static const int precomputed[] = { 1, 1, 3, 2};
    update(_("ai"), precomputed[total % (MAX_MOVE + 1)]);
#else
    int i;
    int move = 1;
    for (i = MIN_MOVE; i <= MAX_MOVE; i++)
        if ((total + i - 1) % (MAX_MOVE + 1) == 0)
            move = i;
    for (i = MIN_MOVE; i <= MAX_MOVE; i++)
        if (total + i == GOAL)
            move = i;
    update(_("ai"), move);
#endif
}


void human(void)
{
    char buffer[BUFFER_SIZE];
    int move;
 
    while ( printf(_("enter your move to play (or enter 0 to exit game): ")),
            fgets(buffer, BUFFER_SIZE, stdin), 
            sscanf(buffer, "%d", &move) != 1 ||
            (move && (move < MIN_MOVE || move > MAX_MOVE || total+move > GOAL)))
        puts(_("\nYour answer is not a valid choice.\n"));
    putchar('\n');
    if (!move) exit(EXIT_SUCCESS);
    update(_("human"), move);
}


int main(int argc, char* argv[])
{
    srand(time(NULL));
    puts(_(DESCRIPTION));
    while (true)
    {
        puts(_("\n---- NEW GAME ----\n"));
        puts(_("\nThe running total is currently zero.\n"));
        total = 0;

        if (rand() % NUMBER_OF_PLAYERS)
        {
            puts(_("The first move is AI move.\n"));
            ai();
        }
        else
            puts(_("The first move is human move.\n"));

        while (total < GOAL)
        {
            human();
            ai();
        }
    }
}
Task: 21 game







Python: from random import randint
def start():
	game_count=0
	print("Enter q to quit at any time.\nThe computer will choose first.\nRunning total is now {}".format(game_count))
	roundno=1
	while game_count<21:
		print("\nROUND {}: \n".format(roundno))
		t = select_count(game_count)
		game_count = game_count+t
		print("Running total is now {}\n".format(game_count))
		if game_count>=21:
			print("So, commiserations, the computer has won!")
			return 0
		t = request_count()
		if not t:
			print('OK,quitting the game')
			return -1
		game_count = game_count+t
		print("Running total is now {}\n".format(game_count))
		if game_count>=21:
			print("So, congratulations, you've won!")
			return 1
		roundno+=1

def select_count(game_count):
	
	if game_count<18:
		t= randint(1,3)
	else:
		t = 21-game_count
	print("The computer chooses {}".format(t))
	return t

def request_count():
	
	t=""
	while True:
		try:
			t = raw_input('Your choice 1 to 3 :')
			if int(t) in [1,2,3]:
				return int(t)
			else:
				print("Out of range, try again")
		except:
			if t=="q":
				return None
			else:
				print("Invalid Entry, try again")

c=0
m=0
r=True
while r:
	o = start()
	if o==-1:
		break
	else:
		c+=1 if o==0 else 0
		m+=1 if o==1 else 0
	print("Computer wins {0} game, human wins {1} games".format(c,m))
	t = raw_input("Another game?(press y to continue):")
	r = (t=="y")
Task: 21 game








VB: 

Class MainWindow
    Private Const GOAL As Integer = 21
    Private total As Integer = 0
    Private random As New Random

    Private Sub Update(box As TextBox, player As String, move As Integer)
        total = total + move
        box.Text = move
        boxTotal.Text = total
        If total + 1 > GOAL Then button1.IsEnabled = False
        If total + 2 > GOAL Then button2.IsEnabled = False
        If total + 3 > GOAL Then button3.IsEnabled = False
        If total = GOAL Then
            winner.Content = $"The winner is {player}."
        End If
    End Sub

    Private Sub Ai()
        Dim move As Integer = 1
        For i = 1 To 3
            If (total + i - 1) Mod 4 = 0 Then move = i
        Next i
        For i = 1 To 3
            If total + i = GOAL Then move = i
        Next i
        Update(boxAI, "AI", move)
    End Sub

    Private Sub Choice(sender As Object, e As RoutedEventArgs) _
            Handles button1.Click, button2.Click, button3.Click
        Update(boxHuman, "human", sender.Content)
        If total < GOAL Then Ai()
    End Sub

    
    
    
    Private Sub StartGame(sender As Object, e As RoutedEventArgs) Handles restart.Click

        total = 0

        boxAI.Text = ""
        boxHuman.Text = ""
        boxTotal.Text = ""
        
        winner.Content = ""

        button1.IsEnabled = True
        button2.IsEnabled = True
        button3.IsEnabled = True

        
        
        
        If random.Next(2) = 0 Then
            first.Content = "First player is AI player."
            Ai()
        Else
            first.Content = "First player is human player."
        End If
    End Sub
End Class