WeixiangYan/CodeTransOcean huggingface dataset:
Task: Unbias a random generator







Python: from __future__ import print_function
import random

def randN(N):
    " 1,0 random generator factory with 1 appearing 1/N'th of the time"
    return lambda: random.randrange(N) == 0

def unbiased(biased):
    'uses a biased() generator of 1 or 0, to create an unbiased one'
    this, that = biased(), biased()
    while this == that: 
        this, that = biased(), biased()
    return this         

if __name__ == '__main__':
    from collections import namedtuple

    Stats = namedtuple('Stats', 'count1 count0 percent')

    for N in range(3, 7):
        biased = randN(N)
        v = [biased() for x in range(1000000)]
        v1, v0 = v.count(1), v.count(0)
        print ( "Biased(%i)  = %r" % (N, Stats(v1, v0, 100. * v1/(v1 + v0))) )

        v = [unbiased(biased) for x in range(1000000)]
        v1, v0 = v.count(1), v.count(0)
        print ( "  Unbiased = %r" % (Stats(v1, v0, 100. * v1/(v1 + v0)), ) )


VB: Module Module1
    Dim random As New Random()

    Function RandN(n As Integer) As Boolean
        Return random.Next(0, n) = 0
    End Function

    Function Unbiased(n As Integer) As Boolean
        Dim flip1 As Boolean
        Dim flip2 As Boolean

        Do
            flip1 = RandN(n)
            flip2 = RandN(n)
        Loop While flip1 = flip2

        Return flip1
    End Function

    Sub Main()
        For n = 3 To 6
            Dim biasedZero = 0
            Dim biasedOne = 0
            Dim unbiasedZero = 0
            Dim unbiasedOne = 0
            For i = 1 To 100000
                If RandN(n) Then
                    biasedOne += 1
                Else
                    biasedZero += 1
                End If
                If Unbiased(n) Then
                    unbiasedOne += 1
                Else
                    unbiasedZero += 1
                End If
            Next

            Console.WriteLine("(N = {0}):".PadRight(17) + "# of 0" + vbTab + "# of 1" + vbTab + "% of 0" + vbTab + "% of 1", n)
            Console.WriteLine("(Biased: {0}):".PadRight(15) + "{0}" + vbTab + "{1}" + vbTab + "{2}" + vbTab + "{3}", biasedZero, biasedOne, biasedZero / 1000, biasedOne / 1000)
            Console.WriteLine("(UnBiased: {0}):".PadRight(15) + "{0}" + vbTab + "{1}" + vbTab + "{2}" + vbTab + "{3}", unbiasedZero, unbiasedOne, unbiasedZero / 1000, unbiasedOne / 1000)
        Next
    End Sub

End Module
Task: Morse code

C: 

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))


char
  
  
  dih[50],dah[50],medium[30],word[30],
  *dd[2] = {dih,dah};
const char
  *ascii = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?'!/()&:;=+-_\"$@",
  *itu[] = {
     "13","3111","3131","311","1","1131","331","1111","11","1333","313","1311","33","31","333","1331","3313","131","111","3","113","1113","133","3113","3133","3311","33333","13333","11333","11133","11113","11111","31111","33111","33311","33331","131313","331133","113311","133331","313133","31131","31331","313313","13111","333111","313131","31113","13131","311113","113313","131131","1113113","133131"
  };

void append(char*s,const char*morse) {
  for (; *morse; ++morse)
    strcat(s,dd['3'==*morse]);
  strcat(s,medium);
}

char*translate(const char*i,char*o) {
  const char*pc;
  sprintf(o,"beep");
  for (; *i; ++i)
    if (NULL == (pc = strchr(ascii,toupper(*i))))
      strcat(o,word);
    else
      append(o,itu[pc-ascii]);
  strcat(o,word);
  return o;
}

int main(int ac,char*av[]) {
  char
    sin[73],sout[100000];
  int
    dit = 100;
  if (1 < ac) {
    if (strlen(av[1]) != strspn(av[1],"0123456789"))
      return 0*fprintf(stderr,"use: %s [duration]   dit in ms, default %d\n",*av,dit);
    dit = BIND(atoi(av[1]),1,1000);
  }
  sprintf(dah," -n -f 440 -l %d -D %d",3*dit,dit);
  sprintf(dih," -n -f 440 -l %d -D %d",dit,dit);
  sprintf(medium," -n -D %d",(3-1)*dit);
  sprintf(word," -n -D %d",(7-(3-1)-1)*dit);
  while (NULL != fgets(sin,72,stdin))
    puts(translate(sin,sout));
  return 0;
}



C#: using System;
using System.Collections.Generic;

namespace Morse
{
    class Morse
    {
        static void Main(string[] args)
        {
            string word = "sos";
            Dictionary<string, string> Codes = new Dictionary<string, string>
            {
                {"a", ".-   "}, {"b", "-... "}, {"c", "-.-. "}, {"d", "-..  "}, 
                {"e", ".    "}, {"f", "..-. "}, {"g", "--.  "}, {"h", ".... "},
                {"i", "..   "}, {"j", ".--- "}, {"k", "-.-  "}, {"l", ".-.. "},
                {"m", "--   "}, {"n", "-.   "}, {"o", "---  "}, {"p", ".--. "}, 
                {"q", "--.- "}, {"r", ".-.  "}, {"s", "...  "}, {"t", "-    "}, 
                {"u", "..-  "}, {"v", "...- "}, {"w", ".--  "}, {"x", "-..- "}, 
                {"y", "-.-- "}, {"z", "--.. "}, {"0", "-----"}, {"1", ".----"}, 
                {"2", "..---"}, {"3", "...--"}, {"4", "....-"}, {"5", "....."}, 
                {"6", "-...."}, {"7", "--..."}, {"8", "---.."}, {"9", "----."}    
            };

            foreach (char c in word.ToCharArray())
            {
                string rslt = Codes[c.ToString()].Trim();
                foreach (char c2 in rslt.ToCharArray())
                {
                    if (c2 == '.')
                        Console.Beep(1000, 250);
                    else
                        Console.Beep(1000, 750);
                }
                System.Threading.Thread.Sleep(50);
            }
        }
    }
}
Task: Input_Output for lines of text

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

#define LEN 100  

int main()
{
	char **list;
	int num, i;
	
	scanf("%d",&num);
	
	list = (char**)malloc(num*sizeof(char*));
	
	for(i=0;i<num;i++)
	{
	   list[i] = (char*)malloc(LEN*sizeof(char));  
	   fflush(stdin);
	   fgets(list[i],LEN,stdin);
	} 
	
	printf("\n");
	
	for(i=0;i<num;i++)
	{
		printf("%s",list[i]);
	}
	
	return 0;
}



C#: #include <iostream>
#include <vector>

int main()
{
    
    int numberOfLines;
    std::cin >> numberOfLines;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    
    
    std::vector<std::string> lines(numberOfLines);    
    for(int i = 0; i < numberOfLines; ++i)
    {
        std::getline(std::cin, lines[i]);
    }

    
    for(const auto& value : lines)
    {
        std::cout << value << "\n";
    }
}
Task: Input_Output for lines of text

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

#define LEN 100  

int main()
{
	char **list;
	int num, i;
	
	scanf("%d",&num);
	
	list = (char**)malloc(num*sizeof(char*));
	
	for(i=0;i<num;i++)
	{
	   list[i] = (char*)malloc(LEN*sizeof(char));  
	   fflush(stdin);
	   fgets(list[i],LEN,stdin);
	} 
	
	printf("\n");
	
	for(i=0;i<num;i++)
	{
		printf("%s",list[i]);
	}
	
	return 0;
}





Java: import java.util.Scanner;

public class Main {
	public static void doStuff(String word){
	   System.out.println(word);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());  
		for(int i=0; i<n; i++){		
			String word = in.nextLine();
			doStuff(word);
		}
	}
}
Task: Input_Output for lines of text

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

#define LEN 100  

int main()
{
	char **list;
	int num, i;
	
	scanf("%d",&num);
	
	list = (char**)malloc(num*sizeof(char*));
	
	for(i=0;i<num;i++)
	{
	   list[i] = (char*)malloc(LEN*sizeof(char));  
	   fflush(stdin);
	   fgets(list[i],LEN,stdin);
	} 
	
	printf("\n");
	
	for(i=0;i<num;i++)
	{
		printf("%s",list[i]);
	}
	
	return 0;
}







Python: try: input = raw_input
except: pass

def do_stuff(words):
	print(words)

linecount = int(input())
for x in range(linecount):
	line = input()
	do_stuff(line)
Task: Input_Output for lines of text


C++: #include <iostream>
#include <vector>

int main()
{
    
    int numberOfLines;
    std::cin >> numberOfLines;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    
    
    std::vector<std::string> lines(numberOfLines);    
    for(int i = 0; i < numberOfLines; ++i)
    {
        std::getline(std::cin, lines[i]);
    }

    
    for(const auto& value : lines)
    {
        std::cout << value << "\n";
    }
}



Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
)

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

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines) 
	for ; scanner.Scan() && lines > 0; lines-- {
		doStuff(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
	
	if lines > 0 {
		log.Fatalln("early", io.EOF)
	}
}

func doStuff(line string) {
	fmt.Println(line)
}
Task: Input_Output for lines of text


C++: #include <iostream>
#include <vector>

int main()
{
    
    int numberOfLines;
    std::cin >> numberOfLines;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    
    
    std::vector<std::string> lines(numberOfLines);    
    for(int i = 0; i < numberOfLines; ++i)
    {
        std::getline(std::cin, lines[i]);
    }

    
    for(const auto& value : lines)
    {
        std::cout << value << "\n";
    }
}




Java: import java.util.Scanner;

public class Main {
	public static void doStuff(String word){
	   System.out.println(word);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());  
		for(int i=0; i<n; i++){		
			String word = in.nextLine();
			doStuff(word);
		}
	}
}
Task: Input_Output for lines of text


C++: #include <iostream>
#include <vector>

int main()
{
    
    int numberOfLines;
    std::cin >> numberOfLines;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    
    
    std::vector<std::string> lines(numberOfLines);    
    for(int i = 0; i < numberOfLines; ++i)
    {
        std::getline(std::cin, lines[i]);
    }

    
    for(const auto& value : lines)
    {
        std::cout << value << "\n";
    }
}






Python: try: input = raw_input
except: pass

def do_stuff(words):
	print(words)

linecount = int(input())
for x in range(linecount):
	line = input()
	do_stuff(line)
Task: Input_Output for lines of text




Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
)

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

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines) 
	for ; scanner.Scan() && lines > 0; lines-- {
		doStuff(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
	
	if lines > 0 {
		log.Fatalln("early", io.EOF)
	}
}

func doStuff(line string) {
	fmt.Println(line)
}


Java: import java.util.Scanner;

public class Main {
	public static void doStuff(String word){
	   System.out.println(word);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());  
		for(int i=0; i<n; i++){		
			String word = in.nextLine();
			doStuff(word);
		}
	}
}
Task: Input_Output for lines of text





Java: import java.util.Scanner;

public class Main {
	public static void doStuff(String word){
	   System.out.println(word);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());  
		for(int i=0; i<n; i++){		
			String word = in.nextLine();
			doStuff(word);
		}
	}
}



Python: try: input = raw_input
except: pass

def do_stuff(words):
	print(words)

linecount = int(input())
for x in range(linecount):
	line = input()
	do_stuff(line)
Task: Input_Output for lines of text




Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
)

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

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines) 
	for ; scanner.Scan() && lines > 0; lines-- {
		doStuff(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
	
	if lines > 0 {
		log.Fatalln("early", io.EOF)
	}
}

func doStuff(line string) {
	fmt.Println(line)
}




Python: try: input = raw_input
except: pass

def do_stuff(words):
	print(words)

linecount = int(input())
for x in range(linecount):
	line = input()
	do_stuff(line)
Task: Input_Output for lines of text

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

#define LEN 100  

int main()
{
	char **list;
	int num, i;
	
	scanf("%d",&num);
	
	list = (char**)malloc(num*sizeof(char*));
	
	for(i=0;i<num;i++)
	{
	   list[i] = (char*)malloc(LEN*sizeof(char));  
	   fflush(stdin);
	   fgets(list[i],LEN,stdin);
	} 
	
	printf("\n");
	
	for(i=0;i<num;i++)
	{
		printf("%s",list[i]);
	}
	
	return 0;
}


C++: #include <iostream>
#include <vector>

int main()
{
    
    int numberOfLines;
    std::cin >> numberOfLines;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    
    
    std::vector<std::string> lines(numberOfLines);    
    for(int i = 0; i < numberOfLines; ++i)
    {
        std::getline(std::cin, lines[i]);
    }

    
    for(const auto& value : lines)
    {
        std::cout << value << "\n";
    }
}
Task: Input_Output for lines of text

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

#define LEN 100  

int main()
{
	char **list;
	int num, i;
	
	scanf("%d",&num);
	
	list = (char**)malloc(num*sizeof(char*));
	
	for(i=0;i<num;i++)
	{
	   list[i] = (char*)malloc(LEN*sizeof(char));  
	   fflush(stdin);
	   fgets(list[i],LEN,stdin);
	} 
	
	printf("\n");
	
	for(i=0;i<num;i++)
	{
		printf("%s",list[i]);
	}
	
	return 0;
}




Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
)

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

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines) 
	for ; scanner.Scan() && lines > 0; lines-- {
		doStuff(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
	
	if lines > 0 {
		log.Fatalln("early", io.EOF)
	}
}

func doStuff(line string) {
	fmt.Println(line)
}
Task: Hello world_Newline omission

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

int main(int argc, char *argv[]) {
  (void) printf("Goodbye, World!");    
  return EXIT_SUCCESS;
}



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        
        Console.WriteLine("Goodbye, World!");

        
        Console.Write("Goodbye, World!");
    }
}
Task: Hello world_Newline omission

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

int main(int argc, char *argv[]) {
  (void) printf("Goodbye, World!");    
  return EXIT_SUCCESS;
}


C++: #include <iostream>

int main() {
  std::cout << "Goodbye, World!";
  return 0;
}
Task: Hello world_Newline omission

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

int main(int argc, char *argv[]) {
  (void) printf("Goodbye, World!");    
  return EXIT_SUCCESS;
}







Python: import sys
sys.stdout.write("Goodbye, World!")
Task: Hello world_Newline omission

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

int main(int argc, char *argv[]) {
  (void) printf("Goodbye, World!");    
  return EXIT_SUCCESS;
}








VB: Module Module1

    Sub Main()
        Console.Write("Goodbye, World!")
    End Sub

End Module
Task: Hello world_Newline omission


C++: #include <iostream>

int main() {
  std::cout << "Goodbye, World!";
  return 0;
}



Go: package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }
Task: Hello world_Newline omission


C++: #include <iostream>

int main() {
  std::cout << "Goodbye, World!";
  return 0;
}






Python: import sys
sys.stdout.write("Goodbye, World!")
Task: Hello world_Newline omission




Go: package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }





VB: Module Module1

    Sub Main()
        Console.Write("Goodbye, World!")
    End Sub

End Module
Task: Hello world_Newline omission







Python: import sys
sys.stdout.write("Goodbye, World!")


VB: Module Module1

    Sub Main()
        Console.Write("Goodbye, World!")
    End Sub

End Module
Task: Hello world_Newline omission

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

int main(int argc, char *argv[]) {
  (void) printf("Goodbye, World!");    
  return EXIT_SUCCESS;
}




Go: package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }
Task: Hello world_Newline omission


C++: #include <iostream>

int main() {
  std::cout << "Goodbye, World!";
  return 0;
}


C#: using System;

class Program
{
    static void Main(string[] args)
    {
        
        Console.WriteLine("Goodbye, World!");

        
        Console.Write("Goodbye, World!");
    }
}
Task: Hello world_Newline omission



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        
        Console.WriteLine("Goodbye, World!");

        
        Console.Write("Goodbye, World!");
    }
}


Go: package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }
Task: Hello world_Newline omission



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        
        Console.WriteLine("Goodbye, World!");

        
        Console.Write("Goodbye, World!");
    }
}





Python: import sys
sys.stdout.write("Goodbye, World!")
Task: Hello world_Newline omission




Go: package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }




Python: import sys
sys.stdout.write("Goodbye, World!")
Task: Hello world_Newline omission


C++: #include <iostream>

int main() {
  std::cout << "Goodbye, World!";
  return 0;
}







VB: Module Module1

    Sub Main()
        Console.Write("Goodbye, World!")
    End Sub

End Module
Task: Hello world_Newline omission



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        
        Console.WriteLine("Goodbye, World!");

        
        Console.Write("Goodbye, World!");
    }
}






VB: Module Module1

    Sub Main()
        Console.Write("Goodbye, World!")
    End Sub

End Module
Task: Hello world_Newline omission

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

int main(int argc, char *argv[]) {
  (void) printf("Goodbye, World!");    
  return EXIT_SUCCESS;
}






PHP: echo "Goodbye, World !";
Task: Hello world_Newline omission



C#: using System;

class Program
{
    static void Main(string[] args)
    {
        
        Console.WriteLine("Goodbye, World!");

        
        Console.Write("Goodbye, World!");
    }
}




PHP: echo "Goodbye, World !";
Task: Hello world_Newline omission


C++: #include <iostream>

int main() {
  std::cout << "Goodbye, World!";
  return 0;
}





PHP: echo "Goodbye, World !";
Task: Hello world_Newline omission




Go: package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }



PHP: echo "Goodbye, World !";
Task: Hello world_Newline omission






PHP: echo "Goodbye, World !";



VB: Module Module1

    Sub Main()
        Console.Write("Goodbye, World!")
    End Sub

End Module
Task: Matrix multiplication

C: #include <stdio.h>

#define MAT_ELEM(rows,cols,r,c) (r*cols+c)



#ifdef __cplusplus
    typedef double * const __restrict MAT_OUT_t;
#else
    typedef double * const restrict MAT_OUT_t;
#endif
typedef const double * const MAT_IN_t;

static inline void mat_mult(
    const int m,
    const int n,
    const int p, 
    MAT_IN_t a,
    MAT_IN_t b,
    MAT_OUT_t c)
{
    for (int row=0; row<m; row++) {
        for (int col=0; col<p; col++) {
            c[MAT_ELEM(m,p,row,col)] = 0;
            for (int i=0; i<n; i++) {
                c[MAT_ELEM(m,p,row,col)] += a[MAT_ELEM(m,n,row,i)]*b[MAT_ELEM(n,p,i,col)];
            }
        }
    }
}

static inline void mat_show(
    const int m,
    const int p,
    MAT_IN_t a)
{
    for (int row=0; row<m;row++) {
        for (int col=0; col<p;col++) {
            printf("\t%7.3f", a[MAT_ELEM(m,p,row,col)]);
        }
        putchar('\n');
    }
}

int main(void)
{
    double a[4*4] = {1, 1,   1,   1,
                     2, 4,   8,  16,
                     3, 9,  27,  81,
                     4, 16, 64, 256};

    double b[4*3] = {    4.0,   -3.0,  4.0/3,
                     -13.0/3, 19.0/4, -7.0/3,
                       3.0/2,   -2.0,  7.0/6,
                      -1.0/6,  1.0/4, -1.0/6};

    double c[4*3] = {0};
    
    mat_mult(4,4,3,a,b,c);
    mat_show(4,3,c);
    return 0;
}



C#: public class Matrix
{
	int n;
	int m;
	double[,] a;

	public Matrix(int n, int m)
	{
		if (n <= 0 || m <= 0)
			throw new ArgumentException("Matrix dimensions must be positive");
		this.n = n;
		this.m = m;
		a = new double[n, m];
	}

	
	public double this[int i, int j]
	{
		get { return a[i - 1, j - 1]; }
		set { a[i - 1, j - 1] = value; }
	}

	public int N { get { return n; } }
	public int M { get { return m; } }

	public static Matrix operator*(Matrix _a, Matrix b)
	{
		int n = _a.N;
		int m = b.M;
		int l = _a.M;
		if (l != b.N)
			throw new ArgumentException("Illegal matrix dimensions for multiplication. _a.M must be equal b.N");
		Matrix result = new Matrix(_a.N, b.M);
		for(int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				double sum = 0.0;
				for (int k = 0; k < l; k++)
					sum += _a.a[i, k]*b.a[k, j];
				result.a[i, j] = sum;
			}
		return result;
	}
}
Task: Matrix multiplication

C: #include <stdio.h>

#define MAT_ELEM(rows,cols,r,c) (r*cols+c)



#ifdef __cplusplus
    typedef double * const __restrict MAT_OUT_t;
#else
    typedef double * const restrict MAT_OUT_t;
#endif
typedef const double * const MAT_IN_t;

static inline void mat_mult(
    const int m,
    const int n,
    const int p, 
    MAT_IN_t a,
    MAT_IN_t b,
    MAT_OUT_t c)
{
    for (int row=0; row<m; row++) {
        for (int col=0; col<p; col++) {
            c[MAT_ELEM(m,p,row,col)] = 0;
            for (int i=0; i<n; i++) {
                c[MAT_ELEM(m,p,row,col)] += a[MAT_ELEM(m,n,row,i)]*b[MAT_ELEM(n,p,i,col)];
            }
        }
    }
}

static inline void mat_show(
    const int m,
    const int p,
    MAT_IN_t a)
{
    for (int row=0; row<m;row++) {
        for (int col=0; col<p;col++) {
            printf("\t%7.3f", a[MAT_ELEM(m,p,row,col)]);
        }
        putchar('\n');
    }
}

int main(void)
{
    double a[4*4] = {1, 1,   1,   1,
                     2, 4,   8,  16,
                     3, 9,  27,  81,
                     4, 16, 64, 256};

    double b[4*3] = {    4.0,   -3.0,  4.0/3,
                     -13.0/3, 19.0/4, -7.0/3,
                       3.0/2,   -2.0,  7.0/6,
                      -1.0/6,  1.0/4, -1.0/6};

    double c[4*3] = {0};
    
    mat_mult(4,4,3,a,b,c);
    mat_show(4,3,c);
    return 0;
}


C++: #include <iostream>
#include <blitz/tinymat.h>

int main()
{
  using namespace blitz;

  TinyMatrix<double,3,3> A, B, C;

  A = 1, 2, 3,
      4, 5, 6,
      7, 8, 9;

  B = 1, 0, 0,
      0, 1, 0,
      0, 0, 1;

  C = product(A, B);

  std::cout << C << std::endl;
}