WeixiangYan/CodeTransOcean huggingface dataset:
Task: Bitmap_Write a PPM file




Go: package raster

import (
    "fmt"
    "io"
    "os"
)


func (b *Bitmap) WritePpmTo(w io.Writer) (err error) {
    
    if _, err = fmt.Fprintln(w, "P6"); err != nil {
        return
    }

    
    for _, c := range b.Comments {
        if _, err = fmt.Fprintln(w, c); err != nil {
            return
        }
    }

    
    _, err = fmt.Fprintf(w, "%d %d\n255\n", b.cols, b.rows)
    if err != nil {
        return
    }

    
    b3 := make([]byte, 3*len(b.px))
    n1 := 0
    for _, px := range b.px {
        b3[n1] = px.R
        b3[n1+1] = px.G
        b3[n1+2] = px.B
        n1 += 3
    }
    if _, err = w.Write(b3); err != nil {
        return
    }
    return
}


func (b *Bitmap) WritePpmFile(fn string) (err error) {
    var f *os.File
    if f, err = os.Create(fn); err != nil {
        return
    }
    if err = b.WritePpmTo(f); err != nil {
        return
    }
    return f.Close()
}







Fortran: program main

  use rgbimage_m

  implicit none

  integer :: nx, ny, i, j, k

  type(rgbimage) :: im

  
  nx = 400
  ny = 300
  call im%init(nx, ny)

  
  do i = 1, nx
    do j = 1, ny
      call im%set_pixel(i, j, [(nint(rand()*255), k=1,3)])
    end do
  end do

  
  call im%write('fig.ppm')

end program
Task: Bitmap_Write a PPM file





Java: import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class PPMWriter {

    public void bitmapToPPM(File file, BasicBitmapStorage bitmap) throws IOException {
        file.delete();

        try (var os = new FileOutputStream(file, true);
             var bw = new BufferedOutputStream(os)) {
            var header = String.format("P6\n%d %d\n255\n",
                    bitmap.getWidth(), bitmap.getHeight());

            bw.write(header.getBytes(StandardCharsets.US_ASCII));

            for (var y = 0; y < bitmap.getHeight(); y++) {
                for (var x = 0; x < bitmap.getWidth(); x++) {
                    var pixel = bitmap.getPixel(x, y);
                    bw.write(pixel.getRed());
                    bw.write(pixel.getGreen());
                    bw.write(pixel.getBlue());
                }
            }
        }
    }
}






Fortran: program main

  use rgbimage_m

  implicit none

  integer :: nx, ny, i, j, k

  type(rgbimage) :: im

  
  nx = 400
  ny = 300
  call im%init(nx, ny)

  
  do i = 1, nx
    do j = 1, ny
      call im%set_pixel(i, j, [(nint(rand()*255), k=1,3)])
    end do
  end do

  
  call im%write('fig.ppm')

end program
Task: Bitmap_Write a PPM file







Python: 
import io
ppmfileout = io.StringIO('')

def writeppmp3(self, f):
    self.writeppm(f, ppmformat='P3')
        
def writeppm(self, f, ppmformat='P6'):
    assert ppmformat in ['P3', 'P6'], 'Format wrong'
    magic = ppmformat + '\n'
    comment = '
    maxval = max(max(max(bit) for bit in row) for row in self.map)
    assert ppmformat == 'P3' or 0 <= maxval < 256, 'R,G,B must fit in a byte'
    if ppmformat == 'P6':
        fwrite = lambda s: f.write(bytes(s, 'UTF-8'))
        maxval = 255
    else:
        fwrite = f.write
        numsize=len(str(maxval))
    fwrite(magic)
    fwrite(comment)
    fwrite('%i %i\n%i\n' % (self.width, self.height, maxval))
    for h in range(self.height-1, -1, -1):
        for w in range(self.width):
            r, g, b = self.get(w, h)
            if ppmformat == 'P3':
                fwrite('   %*i %*i %*i' % (numsize, r, numsize, g, numsize, b))
            else:
                fwrite('%c%c%c' % (r, g, b))
        if ppmformat == 'P3':
            fwrite('\n')

Bitmap.writeppmp3 = writeppmp3
Bitmap.writeppm = writeppm


bitmap = Bitmap(4, 4, black)
bitmap.fillrect(1, 0, 1, 2, white)
bitmap.set(3, 3, Colour(127, 0, 63))

bitmap.writeppmp3(ppmfileout)

print(ppmfileout.getvalue())




ppmfileout = open('tmp.ppm', 'wb')
bitmap.writeppm(ppmfileout)
ppmfileout.close()




Fortran: program main

  use rgbimage_m

  implicit none

  integer :: nx, ny, i, j, k

  type(rgbimage) :: im

  
  nx = 400
  ny = 300
  call im%init(nx, ny)

  
  do i = 1, nx
    do j = 1, ny
      call im%set_pixel(i, j, [(nint(rand()*255), k=1,3)])
    end do
  end do

  
  call im%write('fig.ppm')

end program
Task: Bitmap_Write a PPM file






PHP: class Bitmap {
  public $data;
  public $w;
  public $h;
  public function __construct($w = 16, $h = 16){
    $white = array_fill(0, $w, array(255,255,255));
    $this->data = array_fill(0, $h, $white);
    $this->w = $w;
    $this->h = $h;
  }

  public function fill($x = 0, $y = 0, $w = null, $h = null, $color = array(0,0,0)){
    if (is_null($w)) $w = $this->w;
    if (is_null($h)) $h = $this->h;
    $w += $x;
    $h += $y;
    for ($i = $y; $i < $h; $i++){
      for ($j = $x; $j < $w; $j++){
        $this->setPixel($j, $i, $color);
      }
    }
  }
  public function setPixel($x, $y, $color = array(0,0,0)){
    if ($x >= $this->w) return false;
    if ($x < 0) return false;
    if ($y >= $this->h) return false;
    if ($y < 0) return false;
    $this->data[$y][$x] = $color;
  }
  public function getPixel($x, $y){
    return $this->data[$y][$x];
  }
  public function writeP6($filename){
    $fh = fopen($filename, 'w');
    if (!$fh) return false;
    fputs($fh, "P6 {$this->w} {$this->h} 255\n");
    foreach ($this->data as $row){
      foreach($row as $pixel){
        fputs($fh, pack('C', $pixel[0]));
        fputs($fh, pack('C', $pixel[1]));
        fputs($fh, pack('C', $pixel[2]));
      }
    }
    fclose($fh);
  }
}

$b = new Bitmap(16,16);
$b->fill();
$b->fill(2, 2, 18, 18, array(240,240,240));
$b->setPixel(0, 15, array(255,0,0));
$b->writeP6('p6.ppm');





Fortran: program main

  use rgbimage_m

  implicit none

  integer :: nx, ny, i, j, k

  type(rgbimage) :: im

  
  nx = 400
  ny = 300
  call im%init(nx, ny)

  
  do i = 1, nx
    do j = 1, ny
      call im%set_pixel(i, j, [(nint(rand()*255), k=1,3)])
    end do
  end do

  
  call im%write('fig.ppm')

end program
Task: Bitmap_Write a PPM file






PHP: class Bitmap {
  public $data;
  public $w;
  public $h;
  public function __construct($w = 16, $h = 16){
    $white = array_fill(0, $w, array(255,255,255));
    $this->data = array_fill(0, $h, $white);
    $this->w = $w;
    $this->h = $h;
  }

  public function fill($x = 0, $y = 0, $w = null, $h = null, $color = array(0,0,0)){
    if (is_null($w)) $w = $this->w;
    if (is_null($h)) $h = $this->h;
    $w += $x;
    $h += $y;
    for ($i = $y; $i < $h; $i++){
      for ($j = $x; $j < $w; $j++){
        $this->setPixel($j, $i, $color);
      }
    }
  }
  public function setPixel($x, $y, $color = array(0,0,0)){
    if ($x >= $this->w) return false;
    if ($x < 0) return false;
    if ($y >= $this->h) return false;
    if ($y < 0) return false;
    $this->data[$y][$x] = $color;
  }
  public function getPixel($x, $y){
    return $this->data[$y][$x];
  }
  public function writeP6($filename){
    $fh = fopen($filename, 'w');
    if (!$fh) return false;
    fputs($fh, "P6 {$this->w} {$this->h} 255\n");
    foreach ($this->data as $row){
      foreach($row as $pixel){
        fputs($fh, pack('C', $pixel[0]));
        fputs($fh, pack('C', $pixel[1]));
        fputs($fh, pack('C', $pixel[2]));
      }
    }
    fclose($fh);
  }
}

$b = new Bitmap(16,16);
$b->fill();
$b->fill(2, 2, 18, 18, array(240,240,240));
$b->setPixel(0, 15, array(255,0,0));
$b->writeP6('p6.ppm');
Task: Bitmap_Write a PPM file






PHP: class Bitmap {
  public $data;
  public $w;
  public $h;
  public function __construct($w = 16, $h = 16){
    $white = array_fill(0, $w, array(255,255,255));
    $this->data = array_fill(0, $h, $white);
    $this->w = $w;
    $this->h = $h;
  }

  public function fill($x = 0, $y = 0, $w = null, $h = null, $color = array(0,0,0)){
    if (is_null($w)) $w = $this->w;
    if (is_null($h)) $h = $this->h;
    $w += $x;
    $h += $y;
    for ($i = $y; $i < $h; $i++){
      for ($j = $x; $j < $w; $j++){
        $this->setPixel($j, $i, $color);
      }
    }
  }
  public function setPixel($x, $y, $color = array(0,0,0)){
    if ($x >= $this->w) return false;
    if ($x < 0) return false;
    if ($y >= $this->h) return false;
    if ($y < 0) return false;
    $this->data[$y][$x] = $color;
  }
  public function getPixel($x, $y){
    return $this->data[$y][$x];
  }
  public function writeP6($filename){
    $fh = fopen($filename, 'w');
    if (!$fh) return false;
    fputs($fh, "P6 {$this->w} {$this->h} 255\n");
    foreach ($this->data as $row){
      foreach($row as $pixel){
        fputs($fh, pack('C', $pixel[0]));
        fputs($fh, pack('C', $pixel[1]));
        fputs($fh, pack('C', $pixel[2]));
      }
    }
    fclose($fh);
  }
}

$b = new Bitmap(16,16);
$b->fill();
$b->fill(2, 2, 18, 18, array(240,240,240));
$b->setPixel(0, 15, array(255,0,0));
$b->writeP6('p6.ppm');






Ada: with Ada.Characters.Latin_1;
with Ada.Streams.Stream_IO;  use Ada.Streams.Stream_IO;

procedure Put_PPM (File : File_Type; Picture : Image) is
   use Ada.Characters.Latin_1;
   Size   : constant String := Integer'Image (Picture'Length (2)) & Integer'Image (Picture'Length (1));
   Buffer : String (1..Picture'Length (2) * 3);
   Color  : Pixel;
   Index  : Positive;
begin
   String'Write (Stream (File), "P6" & LF);
   String'Write (Stream (File), Size (2..Size'Last) & LF);
   String'Write (Stream (File), "255" & LF);
   for I in Picture'Range (1) loop
      Index := Buffer'First;
      for J in Picture'Range (2) loop
         Color := Picture (I, J);
         Buffer (Index)     := Character'Val (Color.R);
         Buffer (Index + 1) := Character'Val (Color.G);
         Buffer (Index + 2) := Character'Val (Color.B);
         Index := Index + 3;
      end loop;
      String'Write (Stream (File), Buffer);
   end loop;
   Character'Write (Stream (File), LF);
end Put_PPM;
Task: Bitmap_Write a PPM file


C++: #include <fstream>
#include <cstdio>

int main() {
    constexpr auto dimx = 800u, dimy = 800u;

    using namespace std;
    ofstream ofs("first.ppm", ios_base::out | ios_base::binary);
    ofs << "P6" << endl << dimx << ' ' << dimy << endl << "255" << endl;

    for (auto j = 0u; j < dimy; ++j)
        for (auto i = 0u; i < dimx; ++i)
            ofs << (char) (i % 256) << (char) (j % 256) << (char) ((i * j) % 256);       

    ofs.close();

    return EXIT_SUCCESS;
}
Task: Create a two-dimensional array at runtime



C#: class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter two integers. Space delimited please: ");
            string s = Console.ReadLine();
            
            int[,] myArray=new int[(int)s[0],(int)s[2]];
            myArray[0, 0] = 2;
            Console.WriteLine(myArray[0, 0]);

            Console.ReadLine();
        }
    }









Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime

C: #include <stdio.h>

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

   int user1 = 0, user2 = 0;
   printf("Enter two integers.  Space delimited, please:  ");
   scanf("%d %d",&user1, &user2);
   int array[user1][user2];
   array[user1/2][user2/2] = user1 + user2;
   printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);

   return 0;
}











Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime


C++: #include <iostream>

int main()
{
  
  int dim1, dim2;
  std::cin >> dim1 >> dim2;

  
  double* array_data = new double[dim1*dim2];
  double** array = new double*[dim1];
  for (int i = 0; i < dim1; ++i)
    array[i] = array_data + dim2*i;

  
  array[0][0] = 3.5;

  
  std::cout << array[0][0] << std::endl;

  
  delete[] array;
  delete[] array_data;

  return 0;
}










Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime




Go: package main

import "fmt"

func main() {
    var row, col int
    fmt.Print("enter rows cols: ")
    fmt.Scan(&row, &col)

    
    a := make([][]int, row)
    for i := range a {
        a[i] = make([]int, col)
    }

    
    fmt.Println("a[0][0] =", a[0][0])

    
    a[row-1][col-1] = 7

    
    fmt.Printf("a[%d][%d] = %d\n", row-1, col-1, a[row-1][col-1])

    
    a = nil
    
}








Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime





Java: import java.util.Scanner;

public class twoDimArray {
  public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int nbr1 = in.nextInt();
        int nbr2 = in.nextInt();
        
        double[][] array = new double[nbr1][nbr2];
        array[0][0] = 42.0;
        System.out.println("The number at place [0 0] is " + array[0][0]);
  }
}







Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime







Python: width = int(raw_input("Width of myarray: "))
height = int(raw_input("Height of Array: "))
myarray = [[0] * width for i in range(height)]
myarray[0][0] = 3.5
print (myarray[0][0])





Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime








VB: Module Program
    Sub Main()
        Console.WriteLine("Enter two space-delimited integers:")
        Dim input = Console.ReadLine().Split()
        Dim rows = Integer.Parse(input(0))
        Dim cols = Integer.Parse(input(1))

        
        Dim arr(rows - 1, cols - 1) As Integer

        arr(0, 0) = 2
        Console.WriteLine(arr(0, 0))
    End Sub
End Module




Ada: with Ada.Text_Io;
with Ada.Float_Text_Io;
with Ada.Integer_Text_Io;

procedure Two_Dimensional_Arrays is
   type Matrix_Type is array(Positive range <>, Positive range <>) of Float;
   Dim_1 : Positive;
   Dim_2 : Positive;
begin
   Ada.Integer_Text_Io.Get(Item => Dim_1);
   Ada.Integer_Text_Io.Get(Item => Dim_2);
   
   declare
      Matrix : Matrix_Type(1..Dim_1, 1..Dim_2);
   begin
      Matrix(1, Dim_2) := 3.14159;
      Ada.Float_Text_Io.Put(Item => Matrix(1, Dim_2), Fore => 1, Aft => 5, Exp => 0);
      Ada.Text_Io.New_Line;
   end;
   
end Two_Dimensional_Arrays;
Task: Create a two-dimensional array at runtime

C: #include <stdio.h>

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

   int user1 = 0, user2 = 0;
   printf("Enter two integers.  Space delimited, please:  ");
   scanf("%d %d",&user1, &user2);
   int array[user1][user2];
   array[user1/2][user2/2] = user1 + user2;
   printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);

   return 0;
}
Task: Create a two-dimensional array at runtime



C#: class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter two integers. Space delimited please: ");
            string s = Console.ReadLine();
            
            int[,] myArray=new int[(int)s[0],(int)s[2]];
            myArray[0, 0] = 2;
            Console.WriteLine(myArray[0, 0]);

            Console.ReadLine();
        }
    }
Task: Create a two-dimensional array at runtime


C++: #include <iostream>

int main()
{
  
  int dim1, dim2;
  std::cin >> dim1 >> dim2;

  
  double* array_data = new double[dim1*dim2];
  double** array = new double*[dim1];
  for (int i = 0; i < dim1; ++i)
    array[i] = array_data + dim2*i;

  
  array[0][0] = 3.5;

  
  std::cout << array[0][0] << std::endl;

  
  delete[] array;
  delete[] array_data;

  return 0;
}
Task: Create a two-dimensional array at runtime





Java: import java.util.Scanner;

public class twoDimArray {
  public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int nbr1 = in.nextInt();
        int nbr2 = in.nextInt();
        
        double[][] array = new double[nbr1][nbr2];
        array[0][0] = 42.0;
        System.out.println("The number at place [0 0] is " + array[0][0]);
  }
}
Task: Create a two-dimensional array at runtime







Python: width = int(raw_input("Width of myarray: "))
height = int(raw_input("Height of Array: "))
myarray = [[0] * width for i in range(height)]
myarray[0][0] = 3.5
print (myarray[0][0])
Task: Create a two-dimensional array at runtime








VB: Module Program
    Sub Main()
        Console.WriteLine("Enter two space-delimited integers:")
        Dim input = Console.ReadLine().Split()
        Dim rows = Integer.Parse(input(0))
        Dim cols = Integer.Parse(input(1))

        
        Dim arr(rows - 1, cols - 1) As Integer

        arr(0, 0) = 2
        Console.WriteLine(arr(0, 0))
    End Sub
End Module
Task: Create a two-dimensional array at runtime




Go: package main

import "fmt"

func main() {
    var row, col int
    fmt.Print("enter rows cols: ")
    fmt.Scan(&row, &col)

    
    a := make([][]int, row)
    for i := range a {
        a[i] = make([]int, col)
    }

    
    fmt.Println("a[0][0] =", a[0][0])

    
    a[row-1][col-1] = 7

    
    fmt.Printf("a[%d][%d] = %d\n", row-1, col-1, a[row-1][col-1])

    
    a = nil
    
}
Task: Create a two-dimensional array at runtime



C#: class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter two integers. Space delimited please: ");
            string s = Console.ReadLine();
            
            int[,] myArray=new int[(int)s[0],(int)s[2]];
            myArray[0, 0] = 2;
            Console.WriteLine(myArray[0, 0]);

            Console.ReadLine();
        }
    }








Fortran: PROGRAM Example

  IMPLICIT NONE
  INTEGER :: rows, columns, errcheck
  INTEGER, ALLOCATABLE :: array(:,:)

  WRITE(*,*) "Enter number of rows"
  READ(*,*) rows
  WRITE(*,*) "Enter number of columns"
  READ(*,*) columns
 
  ALLOCATE (array(rows,columns), STAT=errcheck) 
 
  array(1,1) = 42
 
  WRITE(*,*) array(1,1)
 
  DEALLOCATE (array, STAT=errcheck)

END PROGRAM Example
Task: Create a two-dimensional array at runtime


C++: #include <iostream>

int main()
{
  
  int dim1, dim2;
  std::cin >> dim1 >> dim2;

  
  double* array_data = new double[dim1*dim2];
  double** array = new double*[dim1];
  for (int i = 0; i < dim1; ++i)
    array[i] = array_data + dim2*i;

  
  array[0][0] = 3.5;

  
  std::cout << array[0][0] << std::endl;

  
  delete[] array;
  delete[] array_data;

  return 0;
}









Fortran: PROGRAM Example

  IMPLICIT NONE
  INTEGER :: rows, columns, errcheck
  INTEGER, ALLOCATABLE :: array(:,:)

  WRITE(*,*) "Enter number of rows"
  READ(*,*) rows
  WRITE(*,*) "Enter number of columns"
  READ(*,*) columns
 
  ALLOCATE (array(rows,columns), STAT=errcheck) 
 
  array(1,1) = 42
 
  WRITE(*,*) array(1,1)
 
  DEALLOCATE (array, STAT=errcheck)

END PROGRAM Example
Task: Create a two-dimensional array at runtime

C: #include <stdio.h>

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

   int user1 = 0, user2 = 0;
   printf("Enter two integers.  Space delimited, please:  ");
   scanf("%d %d",&user1, &user2);
   int array[user1][user2];
   array[user1/2][user2/2] = user1 + user2;
   printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);

   return 0;
}










Fortran: PROGRAM Example

  IMPLICIT NONE
  INTEGER :: rows, columns, errcheck
  INTEGER, ALLOCATABLE :: array(:,:)

  WRITE(*,*) "Enter number of rows"
  READ(*,*) rows
  WRITE(*,*) "Enter number of columns"
  READ(*,*) columns
 
  ALLOCATE (array(rows,columns), STAT=errcheck) 
 
  array(1,1) = 42
 
  WRITE(*,*) array(1,1)
 
  DEALLOCATE (array, STAT=errcheck)

END PROGRAM Example
Task: Create a two-dimensional array at runtime





Java: import java.util.Scanner;

public class twoDimArray {
  public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int nbr1 = in.nextInt();
        int nbr2 = in.nextInt();
        
        double[][] array = new double[nbr1][nbr2];
        array[0][0] = 42.0;
        System.out.println("The number at place [0 0] is " + array[0][0]);
  }
}






Fortran: PROGRAM Example

  IMPLICIT NONE
  INTEGER :: rows, columns, errcheck
  INTEGER, ALLOCATABLE :: array(:,:)

  WRITE(*,*) "Enter number of rows"
  READ(*,*) rows
  WRITE(*,*) "Enter number of columns"
  READ(*,*) columns
 
  ALLOCATE (array(rows,columns), STAT=errcheck) 
 
  array(1,1) = 42
 
  WRITE(*,*) array(1,1)
 
  DEALLOCATE (array, STAT=errcheck)

END PROGRAM Example
Task: Create a two-dimensional array at runtime







Python: width = int(raw_input("Width of myarray: "))
height = int(raw_input("Height of Array: "))
myarray = [[0] * width for i in range(height)]
myarray[0][0] = 3.5
print (myarray[0][0])




Fortran: PROGRAM Example

  IMPLICIT NONE
  INTEGER :: rows, columns, errcheck
  INTEGER, ALLOCATABLE :: array(:,:)

  WRITE(*,*) "Enter number of rows"
  READ(*,*) rows
  WRITE(*,*) "Enter number of columns"
  READ(*,*) columns
 
  ALLOCATE (array(rows,columns), STAT=errcheck) 
 
  array(1,1) = 42
 
  WRITE(*,*) array(1,1)
 
  DEALLOCATE (array, STAT=errcheck)

END PROGRAM Example