WeixiangYan/CodeTransOcean huggingface dataset:
Task: Literals_Integer




Go: package main

import "fmt"

func main() {
	fmt.Println(727 == 0x2d7)         
	fmt.Println(727 == 01327)         
	fmt.Println(727 == 0b10110_10111) 
	fmt.Println(727 == '˗')           
}





VB: Sub Main()


Dim l1 As Long, l2 As Long, l3 As Long

Dim i1 As Integer, i2 As Integer, i3 As Integer

Dim b1 As Byte, b2 As Byte, b3 As Byte
  
  l1 = 1024&
  l2 = &H400&
  l3 = &O2000&
  Debug.Assert l1 = l2
  Debug.Assert l2 = l3
  
  i1 = 1024
  i2 = &H400
  i3 = &O2000
  Debug.Assert i1 = i2
  Debug.Assert i2 = i3
  
  b1 = 255
  b2 = &O377
  b3 = &HFF
  Debug.Assert b1 = b2
  Debug.Assert b2 = b3
  
End Sub
Task: Literals_Integer

C: #include <stdio.h>

int main(void)
{
  printf("%s\n",
         ( (727 == 0x2d7) && 
           (727 == 01327)    ) ? "true" : "false");

  return 0;
}








VB: Sub Main()


Dim l1 As Long, l2 As Long, l3 As Long

Dim i1 As Integer, i2 As Integer, i3 As Integer

Dim b1 As Byte, b2 As Byte, b3 As Byte
  
  l1 = 1024&
  l2 = &H400&
  l3 = &O2000&
  Debug.Assert l1 = l2
  Debug.Assert l2 = l3
  
  i1 = 1024
  i2 = &H400
  i3 = &O2000
  Debug.Assert i1 = i2
  Debug.Assert i2 = i3
  
  b1 = 255
  b2 = &O377
  b3 = &HFF
  Debug.Assert b1 = b2
  Debug.Assert b2 = b3
  
End Sub
Task: Literals_Integer




Go: package main

import "fmt"

func main() {
	fmt.Println(727 == 0x2d7)         
	fmt.Println(727 == 01327)         
	fmt.Println(727 == 0b10110_10111) 
	fmt.Println(727 == '˗')           
}


Java: public class IntegerLiterals {
    public static void main(String[] args) {
        System.out.println( 727 == 0x2d7 && 
                            727 == 01327   );
    }
}
Task: Literals_Integer


C++: #include <iostream>

int main()
{
  std::cout << ( (727 == 0x2d7) && 
                 (727 == 01327)     ? "true" : "false")
            << std::endl;
 
  return 0;
}




Java: public class IntegerLiterals {
    public static void main(String[] args) {
        System.out.println( 727 == 0x2d7 && 
                            727 == 01327   );
    }
}
Task: Literals_Integer


C++: #include <iostream>

int main()
{
  std::cout << ( (727 == 0x2d7) && 
                 (727 == 01327)     ? "true" : "false")
            << std::endl;
 
  return 0;
}






Python: >>> 
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>>
Task: Literals_Integer



C#: int a = 42;
int b = 0x2a;





Python: >>> 
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>>
Task: Literals_Integer







Python: >>> 
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>>


VB: Sub Main()


Dim l1 As Long, l2 As Long, l3 As Long

Dim i1 As Integer, i2 As Integer, i3 As Integer

Dim b1 As Byte, b2 As Byte, b3 As Byte
  
  l1 = 1024&
  l2 = &H400&
  l3 = &O2000&
  Debug.Assert l1 = l2
  Debug.Assert l2 = l3
  
  i1 = 1024
  i2 = &H400
  i3 = &O2000
  Debug.Assert i1 = i2
  Debug.Assert i2 = i3
  
  b1 = 255
  b2 = &O377
  b3 = &HFF
  Debug.Assert b1 = b2
  Debug.Assert b2 = b3
  
End Sub
Task: Literals_Integer

C: #include <stdio.h>

int main(void)
{
  printf("%s\n",
         ( (727 == 0x2d7) && 
           (727 == 01327)    ) ? "true" : "false");

  return 0;
}






PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
Task: Literals_Integer



C#: int a = 42;
int b = 0x2a;




PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
Task: Literals_Integer


C++: #include <iostream>

int main()
{
  std::cout << ( (727 == 0x2d7) && 
                 (727 == 01327)     ? "true" : "false")
            << std::endl;
 
  return 0;
}





PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
Task: Literals_Integer




Go: package main

import "fmt"

func main() {
	fmt.Println(727 == 0x2d7)         
	fmt.Println(727 == 01327)         
	fmt.Println(727 == 0b10110_10111) 
	fmt.Println(727 == '˗')           
}



PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
Task: Literals_Integer





Java: public class IntegerLiterals {
    public static void main(String[] args) {
        System.out.println( 727 == 0x2d7 && 
                            727 == 01327   );
    }
}


PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
Task: Literals_Integer






PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)


Python: >>> 
>>> 0b1011010111 == 0o1327 == 727 == 0x2d7
True
>>>
Task: Literals_Integer






PHP: <?php
if (727 == 0x2d7 && 
    727 == 01327) {
  echo "true\n";
}

$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)



VB: Sub Main()


Dim l1 As Long, l2 As Long, l3 As Long

Dim i1 As Integer, i2 As Integer, i3 As Integer

Dim b1 As Byte, b2 As Byte, b3 As Byte
  
  l1 = 1024&
  l2 = &H400&
  l3 = &O2000&
  Debug.Assert l1 = l2
  Debug.Assert l2 = l3
  
  i1 = 1024
  i2 = &H400
  i3 = &O2000
  Debug.Assert i1 = i2
  Debug.Assert i2 = i3
  
  b1 = 255
  b2 = &O377
  b3 = &HFF
  Debug.Assert b1 = b2
  Debug.Assert b2 = b3
  
End Sub
Task: Zig-zag matrix

C: #include <stdio.h>
#include <stdlib.h>
 
int main(int c, char **v)
{
	int i, j, m, n, *s;
 
	
	if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
 
	
	s = malloc(sizeof(int) * m * m);
 
	for (i = n = 0; i < m * 2; i++)
		for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
			s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
 
	for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
		printf("%3d", s[i]);

	
	return 0;
}



C#: public static int[,] ZigZag(int n)
{
    int[,] result = new int[n, n];
    int i = 0, j = 0;
    int d = -1; 
    int start = 0, end = n * n - 1;
    do
    {
        result[i, j] = start++;
        result[n - i - 1, n - j - 1] = end--;

        i += d; j -= d;
        if (i < 0)
        {
            i++; d = -d; 
        }
        else if (j < 0)
        {
            j++; d = -d; 
        }
    } while (start < end);
    if (start == end)
        result[i, j] = start;
    return result;
}
Task: Zig-zag matrix

C: #include <stdio.h>
#include <stdlib.h>
 
int main(int c, char **v)
{
	int i, j, m, n, *s;
 
	
	if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
 
	
	s = malloc(sizeof(int) * m * m);
 
	for (i = n = 0; i < m * 2; i++)
		for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
			s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
 
	for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
		printf("%3d", s[i]);

	
	return 0;
}


C++: #include <vector>
#include <memory>	
#include <cmath>	
#include <iostream>
#include <iomanip>	

using namespace std;

typedef vector< int > IntRow;
typedef vector< IntRow > IntTable;

auto_ptr< IntTable > getZigZagArray( int dimension )
{
	auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
		dimension, IntRow( dimension ) ) );

	
	int lastValue = dimension * dimension - 1;
	int currNum = 0;
	int currDiag = 0;
	int loopFrom;
	int loopTo;
	int i;
	int row;
	int col;
	do
	{
		if ( currDiag < dimension ) 
		{
			loopFrom = 0;
			loopTo = currDiag;
		}
		else 
		{
			loopFrom = currDiag - dimension + 1;
			loopTo = dimension - 1;
		}

		for ( i = loopFrom; i <= loopTo; i++ )
		{
			if ( currDiag % 2 == 0 ) 
			{
				row = loopTo - i + loopFrom;
				col = i;
			}
			else 
			{
				row = i;
				col = loopTo - i + loopFrom;
			}

			( *zigZagArrayPtr )[ row ][ col ] = currNum++;
		}

		currDiag++;
	}
	while ( currDiag <= lastValue );

	return zigZagArrayPtr;
}

void printZigZagArray( const auto_ptr< IntTable >& zigZagArrayPtr )
{
	size_t dimension = zigZagArrayPtr->size();

	int fieldWidth = static_cast< int >( floor( log10(
		static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;

	size_t col;
	for ( size_t row = 0; row < dimension; row++ )
	{
		for ( col = 0; col < dimension; col++ )
			cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
		cout << endl;
	}
}

int main()
{
	printZigZagArray( getZigZagArray( 5 ) );
}
Task: Zig-zag matrix

C: #include <stdio.h>
#include <stdlib.h>
 
int main(int c, char **v)
{
	int i, j, m, n, *s;
 
	
	if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
 
	
	s = malloc(sizeof(int) * m * m);
 
	for (i = n = 0; i < m * 2; i++)
		for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
			s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
 
	for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
		printf("%3d", s[i]);

	
	return 0;
}




Go: package main

import (
    "fmt"
    "strconv"
)

func zz(n int) []int {
    r := make([]int, n*n)
    i := 0
    n2 := n * 2
    for d := 1; d <= n2; d++ {
        x := d - n
        if x < 0 {
            x = 0
        }
        y := d - 1
        if y > n-1 {
            y = n - 1
        }
        j := n2 - d
        if j > d {
            j = d
        }
        for k := 0; k < j; k++ {
            if d&1 == 0 {
                r[(x+k)*n+y-k] = i
            } else {
                r[(y-k)*n+x+k] = i
            }
            i++
        }
    }

    return r
}

func main() {
    const n = 5
    w := len(strconv.Itoa(n*n - 1))
    for i, e := range zz(n) {
        fmt.Printf("%*d ", w, e)
        if i%n == n-1 {
            fmt.Println("")
        }
    }
}
Task: Zig-zag matrix

C: #include <stdio.h>
#include <stdlib.h>
 
int main(int c, char **v)
{
	int i, j, m, n, *s;
 
	
	if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
 
	
	s = malloc(sizeof(int) * m * m);
 
	for (i = n = 0; i < m * 2; i++)
		for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
			s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
 
	for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
		printf("%3d", s[i]);

	
	return 0;
}





Java: public static int[][] Zig_Zag(final int size)
{
 int[][] data = new int[size][size];
 int i = 1;
 int j = 1;
 for (int element = 0; element < size * size; element++)
 {
  data[i - 1][j - 1] = element;
  if ((i + j) % 2 == 0)
  {
   
   if (j < size)
    j++;
   else
    i+= 2;
   if (i > 1)
    i--;
  }
  else
  {
   
   if (i < size)
    i++;
   else
    j+= 2;
   if (j > 1)
    j--;
  }
 }
 return data;
}
Task: Zig-zag matrix

C: #include <stdio.h>
#include <stdlib.h>
 
int main(int c, char **v)
{
	int i, j, m, n, *s;
 
	
	if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
 
	
	s = malloc(sizeof(int) * m * m);
 
	for (i = n = 0; i < m * 2; i++)
		for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
			s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
 
	for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
		printf("%3d", s[i]);

	
	return 0;
}








VB: ZigZag(Cint(WScript.Arguments(0)))

Function ZigZag(n)
	Dim arrZ()
	ReDim arrZ(n-1,n-1)
	i = 1
	j = 1
	For e = 0 To (n^2) - 1
		arrZ(i-1,j-1) = e
		If ((i + j ) And 1) = 0 Then
			If j < n Then
				j = j + 1
			Else
				i = i + 2
			End If
			If i > 1 Then
				i = i - 1
			End If
		Else
			If i < n Then
				i = i + 1
			Else
				j = j + 2
			End If
			If j > 1 Then
				j = j - 1
			End If
		End If
	Next
	For k = 0 To n-1
		For l = 0 To n-1
			WScript.StdOut.Write Right("  " & arrZ(k,l),3)
		Next
		WScript.StdOut.WriteLine
	Next
End Function
Task: Zig-zag matrix


C++: #include <vector>
#include <memory>	
#include <cmath>	
#include <iostream>
#include <iomanip>	

using namespace std;

typedef vector< int > IntRow;
typedef vector< IntRow > IntTable;

auto_ptr< IntTable > getZigZagArray( int dimension )
{
	auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
		dimension, IntRow( dimension ) ) );

	
	int lastValue = dimension * dimension - 1;
	int currNum = 0;
	int currDiag = 0;
	int loopFrom;
	int loopTo;
	int i;
	int row;
	int col;
	do
	{
		if ( currDiag < dimension ) 
		{
			loopFrom = 0;
			loopTo = currDiag;
		}
		else 
		{
			loopFrom = currDiag - dimension + 1;
			loopTo = dimension - 1;
		}

		for ( i = loopFrom; i <= loopTo; i++ )
		{
			if ( currDiag % 2 == 0 ) 
			{
				row = loopTo - i + loopFrom;
				col = i;
			}
			else 
			{
				row = i;
				col = loopTo - i + loopFrom;
			}

			( *zigZagArrayPtr )[ row ][ col ] = currNum++;
		}

		currDiag++;
	}
	while ( currDiag <= lastValue );

	return zigZagArrayPtr;
}

void printZigZagArray( const auto_ptr< IntTable >& zigZagArrayPtr )
{
	size_t dimension = zigZagArrayPtr->size();

	int fieldWidth = static_cast< int >( floor( log10(
		static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;

	size_t col;
	for ( size_t row = 0; row < dimension; row++ )
	{
		for ( col = 0; col < dimension; col++ )
			cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
		cout << endl;
	}
}

int main()
{
	printZigZagArray( getZigZagArray( 5 ) );
}


C#: public static int[,] ZigZag(int n)
{
    int[,] result = new int[n, n];
    int i = 0, j = 0;
    int d = -1; 
    int start = 0, end = n * n - 1;
    do
    {
        result[i, j] = start++;
        result[n - i - 1, n - j - 1] = end--;

        i += d; j -= d;
        if (i < 0)
        {
            i++; d = -d; 
        }
        else if (j < 0)
        {
            j++; d = -d; 
        }
    } while (start < end);
    if (start == end)
        result[i, j] = start;
    return result;
}
Task: Zig-zag matrix


C++: #include <vector>
#include <memory>	
#include <cmath>	
#include <iostream>
#include <iomanip>	

using namespace std;

typedef vector< int > IntRow;
typedef vector< IntRow > IntTable;

auto_ptr< IntTable > getZigZagArray( int dimension )
{
	auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
		dimension, IntRow( dimension ) ) );

	
	int lastValue = dimension * dimension - 1;
	int currNum = 0;
	int currDiag = 0;
	int loopFrom;
	int loopTo;
	int i;
	int row;
	int col;
	do
	{
		if ( currDiag < dimension ) 
		{
			loopFrom = 0;
			loopTo = currDiag;
		}
		else 
		{
			loopFrom = currDiag - dimension + 1;
			loopTo = dimension - 1;
		}

		for ( i = loopFrom; i <= loopTo; i++ )
		{
			if ( currDiag % 2 == 0 ) 
			{
				row = loopTo - i + loopFrom;
				col = i;
			}
			else 
			{
				row = i;
				col = loopTo - i + loopFrom;
			}

			( *zigZagArrayPtr )[ row ][ col ] = currNum++;
		}

		currDiag++;
	}
	while ( currDiag <= lastValue );

	return zigZagArrayPtr;
}

void printZigZagArray( const auto_ptr< IntTable >& zigZagArrayPtr )
{
	size_t dimension = zigZagArrayPtr->size();

	int fieldWidth = static_cast< int >( floor( log10(
		static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;

	size_t col;
	for ( size_t row = 0; row < dimension; row++ )
	{
		for ( col = 0; col < dimension; col++ )
			cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
		cout << endl;
	}
}

int main()
{
	printZigZagArray( getZigZagArray( 5 ) );
}



Go: package main

import (
    "fmt"
    "strconv"
)

func zz(n int) []int {
    r := make([]int, n*n)
    i := 0
    n2 := n * 2
    for d := 1; d <= n2; d++ {
        x := d - n
        if x < 0 {
            x = 0
        }
        y := d - 1
        if y > n-1 {
            y = n - 1
        }
        j := n2 - d
        if j > d {
            j = d
        }
        for k := 0; k < j; k++ {
            if d&1 == 0 {
                r[(x+k)*n+y-k] = i
            } else {
                r[(y-k)*n+x+k] = i
            }
            i++
        }
    }

    return r
}

func main() {
    const n = 5
    w := len(strconv.Itoa(n*n - 1))
    for i, e := range zz(n) {
        fmt.Printf("%*d ", w, e)
        if i%n == n-1 {
            fmt.Println("")
        }
    }
}
Task: Zig-zag matrix


C++: #include <vector>
#include <memory>	
#include <cmath>	
#include <iostream>
#include <iomanip>	

using namespace std;

typedef vector< int > IntRow;
typedef vector< IntRow > IntTable;

auto_ptr< IntTable > getZigZagArray( int dimension )
{
	auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
		dimension, IntRow( dimension ) ) );

	
	int lastValue = dimension * dimension - 1;
	int currNum = 0;
	int currDiag = 0;
	int loopFrom;
	int loopTo;
	int i;
	int row;
	int col;
	do
	{
		if ( currDiag < dimension ) 
		{
			loopFrom = 0;
			loopTo = currDiag;
		}
		else 
		{
			loopFrom = currDiag - dimension + 1;
			loopTo = dimension - 1;
		}

		for ( i = loopFrom; i <= loopTo; i++ )
		{
			if ( currDiag % 2 == 0 ) 
			{
				row = loopTo - i + loopFrom;
				col = i;
			}
			else 
			{
				row = i;
				col = loopTo - i + loopFrom;
			}

			( *zigZagArrayPtr )[ row ][ col ] = currNum++;
		}

		currDiag++;
	}
	while ( currDiag <= lastValue );

	return zigZagArrayPtr;
}

void printZigZagArray( const auto_ptr< IntTable >& zigZagArrayPtr )
{
	size_t dimension = zigZagArrayPtr->size();

	int fieldWidth = static_cast< int >( floor( log10(
		static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;

	size_t col;
	for ( size_t row = 0; row < dimension; row++ )
	{
		for ( col = 0; col < dimension; col++ )
			cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
		cout << endl;
	}
}

int main()
{
	printZigZagArray( getZigZagArray( 5 ) );
}




Java: public static int[][] Zig_Zag(final int size)
{
 int[][] data = new int[size][size];
 int i = 1;
 int j = 1;
 for (int element = 0; element < size * size; element++)
 {
  data[i - 1][j - 1] = element;
  if ((i + j) % 2 == 0)
  {
   
   if (j < size)
    j++;
   else
    i+= 2;
   if (i > 1)
    i--;
  }
  else
  {
   
   if (i < size)
    i++;
   else
    j+= 2;
   if (j > 1)
    j--;
  }
 }
 return data;
}
Task: Zig-zag matrix


C++: #include <vector>
#include <memory>	
#include <cmath>	
#include <iostream>
#include <iomanip>	

using namespace std;

typedef vector< int > IntRow;
typedef vector< IntRow > IntTable;

auto_ptr< IntTable > getZigZagArray( int dimension )
{
	auto_ptr< IntTable > zigZagArrayPtr( new IntTable(
		dimension, IntRow( dimension ) ) );

	
	int lastValue = dimension * dimension - 1;
	int currNum = 0;
	int currDiag = 0;
	int loopFrom;
	int loopTo;
	int i;
	int row;
	int col;
	do
	{
		if ( currDiag < dimension ) 
		{
			loopFrom = 0;
			loopTo = currDiag;
		}
		else 
		{
			loopFrom = currDiag - dimension + 1;
			loopTo = dimension - 1;
		}

		for ( i = loopFrom; i <= loopTo; i++ )
		{
			if ( currDiag % 2 == 0 ) 
			{
				row = loopTo - i + loopFrom;
				col = i;
			}
			else 
			{
				row = i;
				col = loopTo - i + loopFrom;
			}

			( *zigZagArrayPtr )[ row ][ col ] = currNum++;
		}

		currDiag++;
	}
	while ( currDiag <= lastValue );

	return zigZagArrayPtr;
}

void printZigZagArray( const auto_ptr< IntTable >& zigZagArrayPtr )
{
	size_t dimension = zigZagArrayPtr->size();

	int fieldWidth = static_cast< int >( floor( log10(
		static_cast< double >( dimension * dimension - 1 ) ) ) ) + 2;

	size_t col;
	for ( size_t row = 0; row < dimension; row++ )
	{
		for ( col = 0; col < dimension; col++ )
			cout << setw( fieldWidth ) << ( *zigZagArrayPtr )[ row ][ col ];
		cout << endl;
	}
}

int main()
{
	printZigZagArray( getZigZagArray( 5 ) );
}







VB: ZigZag(Cint(WScript.Arguments(0)))

Function ZigZag(n)
	Dim arrZ()
	ReDim arrZ(n-1,n-1)
	i = 1
	j = 1
	For e = 0 To (n^2) - 1
		arrZ(i-1,j-1) = e
		If ((i + j ) And 1) = 0 Then
			If j < n Then
				j = j + 1
			Else
				i = i + 2
			End If
			If i > 1 Then
				i = i - 1
			End If
		Else
			If i < n Then
				i = i + 1
			Else
				j = j + 2
			End If
			If j > 1 Then
				j = j - 1
			End If
		End If
	Next
	For k = 0 To n-1
		For l = 0 To n-1
			WScript.StdOut.Write Right("  " & arrZ(k,l),3)
		Next
		WScript.StdOut.WriteLine
	Next
End Function
Task: Zig-zag matrix



C#: public static int[,] ZigZag(int n)
{
    int[,] result = new int[n, n];
    int i = 0, j = 0;
    int d = -1; 
    int start = 0, end = n * n - 1;
    do
    {
        result[i, j] = start++;
        result[n - i - 1, n - j - 1] = end--;

        i += d; j -= d;
        if (i < 0)
        {
            i++; d = -d; 
        }
        else if (j < 0)
        {
            j++; d = -d; 
        }
    } while (start < end);
    if (start == end)
        result[i, j] = start;
    return result;
}


Go: package main

import (
    "fmt"
    "strconv"
)

func zz(n int) []int {
    r := make([]int, n*n)
    i := 0
    n2 := n * 2
    for d := 1; d <= n2; d++ {
        x := d - n
        if x < 0 {
            x = 0
        }
        y := d - 1
        if y > n-1 {
            y = n - 1
        }
        j := n2 - d
        if j > d {
            j = d
        }
        for k := 0; k < j; k++ {
            if d&1 == 0 {
                r[(x+k)*n+y-k] = i
            } else {
                r[(y-k)*n+x+k] = i
            }
            i++
        }
    }

    return r
}

func main() {
    const n = 5
    w := len(strconv.Itoa(n*n - 1))
    for i, e := range zz(n) {
        fmt.Printf("%*d ", w, e)
        if i%n == n-1 {
            fmt.Println("")
        }
    }
}
Task: Zig-zag matrix



C#: public static int[,] ZigZag(int n)
{
    int[,] result = new int[n, n];
    int i = 0, j = 0;
    int d = -1; 
    int start = 0, end = n * n - 1;
    do
    {
        result[i, j] = start++;
        result[n - i - 1, n - j - 1] = end--;

        i += d; j -= d;
        if (i < 0)
        {
            i++; d = -d; 
        }
        else if (j < 0)
        {
            j++; d = -d; 
        }
    } while (start < end);
    if (start == end)
        result[i, j] = start;
    return result;
}



Java: public static int[][] Zig_Zag(final int size)
{
 int[][] data = new int[size][size];
 int i = 1;
 int j = 1;
 for (int element = 0; element < size * size; element++)
 {
  data[i - 1][j - 1] = element;
  if ((i + j) % 2 == 0)
  {
   
   if (j < size)
    j++;
   else
    i+= 2;
   if (i > 1)
    i--;
  }
  else
  {
   
   if (i < size)
    i++;
   else
    j+= 2;
   if (j > 1)
    j--;
  }
 }
 return data;
}
Task: Zig-zag matrix



C#: public static int[,] ZigZag(int n)
{
    int[,] result = new int[n, n];
    int i = 0, j = 0;
    int d = -1; 
    int start = 0, end = n * n - 1;
    do
    {
        result[i, j] = start++;
        result[n - i - 1, n - j - 1] = end--;

        i += d; j -= d;
        if (i < 0)
        {
            i++; d = -d; 
        }
        else if (j < 0)
        {
            j++; d = -d; 
        }
    } while (start < end);
    if (start == end)
        result[i, j] = start;
    return result;
}






VB: ZigZag(Cint(WScript.Arguments(0)))

Function ZigZag(n)
	Dim arrZ()
	ReDim arrZ(n-1,n-1)
	i = 1
	j = 1
	For e = 0 To (n^2) - 1
		arrZ(i-1,j-1) = e
		If ((i + j ) And 1) = 0 Then
			If j < n Then
				j = j + 1
			Else
				i = i + 2
			End If
			If i > 1 Then
				i = i - 1
			End If
		Else
			If i < n Then
				i = i + 1
			Else
				j = j + 2
			End If
			If j > 1 Then
				j = j - 1
			End If
		End If
	Next
	For k = 0 To n-1
		For l = 0 To n-1
			WScript.StdOut.Write Right("  " & arrZ(k,l),3)
		Next
		WScript.StdOut.WriteLine
	Next
End Function