From f9882bc46b6fb58b5aa8bbff382518e59ec74eab Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:29:45 +0300 Subject: [PATCH 01/21] cpp implementation(?) --- cpp/main.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 cpp/main.cpp diff --git a/cpp/main.cpp b/cpp/main.cpp new file mode 100644 index 0000000..d0ea745 --- /dev/null +++ b/cpp/main.cpp @@ -0,0 +1,127 @@ +#include +#include +#include + +const unsigned int ROWS = 10; +const unsigned int COLUMNS = 10; + +void randomValue(unsigned int &, unsigned int &, unsigned int &); + +int main() +{ + unsigned int rValue; + unsigned int cValue; + unsigned int pValue; + + unsigned int rowError; + unsigned int columnError; + unsigned int productError; + + unsigned int userRowGuess; + unsigned int userColumnGuess; + + char choice; + + do + { + randomValue(rValue, cValue, pValue); + + std::cout << "\033[2J\033[0;0H"; // This code clears the screen + std::cout << std::endl << std::endl; + std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; + std::cout << std::setw(35) << "+ Multiplication Table +" << std::endl; + std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; + std::cout << std::endl << std::endl; + + while (true) + { + rowError = rValue; + columnError = cValue; + productError = pValue; + + if (productError == rowError * columnError) + { + productError--; + } + + for (unsigned int row = 1; row <= ROWS; row++) + { + for (unsigned int column = 1; column <= COLUMNS; column++) + { + if (row == rowError && column == columnError) + { + std::cout << "\t" << "" << productError; + } + else + { + std::cout << "\t" << row * column; + } + } + std::cout << std::endl; + } + break; + } + + + std::cout << std::endl << std::endl; + std::cout << "\t\t" << "Type in the column number " << std::endl; + std::cout << "\t\t" << "of the location of the " << std::endl; + std::cout << "\t\t" << "error & then press " << std::endl; + std::cout << "\t\t" << "[Enter]: "; + std::cin >> userColumnGuess; + std::cout << std::endl; + + std::cout << "\t\t" << "Type in the row number " << std::endl; + std::cout << "\t\t" << "of the location of the " << std::endl; + std::cout << "\t\t" << "error & then press " << std::endl; + std::cout << "\t\t" << "[Enter]: "; + std::cin >> userRowGuess; + std::cout << std::endl; + + if (userRowGuess != rowError && userColumnGuess != columnError) + { + std::cout << "\t\t" << "Your answer was incorrect!" << std::endl << std::endl; + std::cout << "\t\t" << "Error value '" << productError << "' is located" << std::endl; + std::cout << "\t\t" << "on row " << rowError << ", column " << columnError << "." << std::endl; + } + else + { + std::cout << "\t\t" << "You are correct! You win!" << std::endl; + } + + + std::cout << std::endl; + std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; + std::cout << "\t\t" << "Type in 'Y' for yes or 'N'" << std::endl; + std::cout << "\t\t" << "for no & then press [Enter]: "; + std::cin >> choice; + + while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N') + { + std::cout << std::endl; + std::cout << "\t\t" << "Invalid entry. Only 'Y' or 'N'" << std::endl; + std::cout << "\t\t" << "are accepted answers." << std::endl << std::endl; + std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; + std::cout << "\t\t" << "Type in 'Y' for yes or 'N' for" << std::endl; + std::cout << "\t\t" << "no & then press [Enter]: "; + std::cin >> choice; + } + std::cout << std::endl; + + } while (choice == 'y' || choice == 'Y'); + + std::cout << "\t\t" << "Press [Enter] to continue....." << std::endl; + std::cin.get(); + std::cin.get(); + + return 0; +} + +void randomValue(unsigned int &rValue, unsigned int &cValue, unsigned int &pValue) +{ + srand((unsigned)time(NULL)); + + unsigned int r = rValue = (rand() % ROWS) + 1; + unsigned int c = cValue = (rand() % COLUMNS) + 1; + unsigned int p = pValue = (rand() % (ROWS * COLUMNS)) + 1; +} From 014c22c5aa1a2c669e57c1d19978c4f17611ea24 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:36:08 +0300 Subject: [PATCH 02/21] Revamp --- cpp/main.cpp | 195 +++++++++++++++++++-------------------------------- 1 file changed, 73 insertions(+), 122 deletions(-) diff --git a/cpp/main.cpp b/cpp/main.cpp index d0ea745..9755a47 100644 --- a/cpp/main.cpp +++ b/cpp/main.cpp @@ -1,127 +1,78 @@ #include -#include #include - -const unsigned int ROWS = 10; -const unsigned int COLUMNS = 10; - -void randomValue(unsigned int &, unsigned int &, unsigned int &); - -int main() -{ - unsigned int rValue; - unsigned int cValue; - unsigned int pValue; - - unsigned int rowError; - unsigned int columnError; - unsigned int productError; - - unsigned int userRowGuess; - unsigned int userColumnGuess; - - char choice; - - do - { - randomValue(rValue, cValue, pValue); - - std::cout << "\033[2J\033[0;0H"; // This code clears the screen - std::cout << std::endl << std::endl; - std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; - std::cout << std::setw(35) << "+ Multiplication Table +" << std::endl; - std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; - std::cout << std::endl << std::endl; - - while (true) - { - rowError = rValue; - columnError = cValue; - productError = pValue; - - if (productError == rowError * columnError) - { - productError--; - } - - for (unsigned int row = 1; row <= ROWS; row++) - { - for (unsigned int column = 1; column <= COLUMNS; column++) - { - if (row == rowError && column == columnError) - { - std::cout << "\t" << "" << productError; - } - else - { - std::cout << "\t" << row * column; - } - } - std::cout << std::endl; - } - break; - } - - +#include // for log10() +#include // for max() + +size_t table_column_width(const int min, const int max) +{ + unsigned int abs_max = std::max(max*max, min*min); + + // abs_max is the largest absolute value we might see. + // If we take the log10 and add one, we get the string width + // of the largest possible absolute value. + // Add one more for a little whitespace guarantee. + size_t colwidth = 2 + std::log10(abs_max); + + // If only one of them is less than 0, then some will + // be negative. If some values may be negative, then we need to add some space + // for a sign indicator (-) + if (min < 0 && max > 0) + ++colwidth; + return colwidth; +} + +struct Writer_ +{ + decltype(std::setw(1)) fmt_; + Writer_(size_t w) : fmt_(std::setw(w)) {} + template Writer_& operator()(const T_& info) { std::cout << fmt_ << info; return *this; } +}; + +void print_table_header(const int min, const int max) +{ + Writer_ write(table_column_width(min, max)); + + // table corner + write(" "); + for(int col = min; col <= max; ++col) + write(col); + + // End header with a newline and blank line. + std::cout << std::endl << std::endl; +} + +void print_table_row(const int num, const int min, const int max) +{ + Writer_ write(table_column_width(min, max)); + + // Header column + write(num); + + // Spacing to ensure only the top half is printed + for(int multiplicand = min; multiplicand < num; ++multiplicand) + write(" "); + + // Remaining multiplicands for the row. + for(int multiplicand = num; multiplicand <= max; ++multiplicand) + write(num * multiplicand); + + // End row with a newline and blank line. std::cout << std::endl << std::endl; - std::cout << "\t\t" << "Type in the column number " << std::endl; - std::cout << "\t\t" << "of the location of the " << std::endl; - std::cout << "\t\t" << "error & then press " << std::endl; - std::cout << "\t\t" << "[Enter]: "; - std::cin >> userColumnGuess; - std::cout << std::endl; - - std::cout << "\t\t" << "Type in the row number " << std::endl; - std::cout << "\t\t" << "of the location of the " << std::endl; - std::cout << "\t\t" << "error & then press " << std::endl; - std::cout << "\t\t" << "[Enter]: "; - std::cin >> userRowGuess; - std::cout << std::endl; - - if (userRowGuess != rowError && userColumnGuess != columnError) - { - std::cout << "\t\t" << "Your answer was incorrect!" << std::endl << std::endl; - std::cout << "\t\t" << "Error value '" << productError << "' is located" << std::endl; - std::cout << "\t\t" << "on row " << rowError << ", column " << columnError << "." << std::endl; - } - else - { - std::cout << "\t\t" << "You are correct! You win!" << std::endl; - } - - - std::cout << std::endl; - std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; - std::cout << "\t\t" << "Type in 'Y' for yes or 'N'" << std::endl; - std::cout << "\t\t" << "for no & then press [Enter]: "; - std::cin >> choice; - - while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N') - { - std::cout << std::endl; - std::cout << "\t\t" << "Invalid entry. Only 'Y' or 'N'" << std::endl; - std::cout << "\t\t" << "are accepted answers." << std::endl << std::endl; - std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; - std::cout << "\t\t" << "Type in 'Y' for yes or 'N' for" << std::endl; - std::cout << "\t\t" << "no & then press [Enter]: "; - std::cin >> choice; - } - std::cout << std::endl; - - } while (choice == 'y' || choice == 'Y'); - - std::cout << "\t\t" << "Press [Enter] to continue....." << std::endl; - std::cin.get(); - std::cin.get(); - - return 0; } - -void randomValue(unsigned int &rValue, unsigned int &cValue, unsigned int &pValue) -{ - srand((unsigned)time(NULL)); - - unsigned int r = rValue = (rand() % ROWS) + 1; - unsigned int c = cValue = (rand() % COLUMNS) + 1; - unsigned int p = pValue = (rand() % (ROWS * COLUMNS)) + 1; + +void print_table(const int min, const int max) +{ + // Header row + print_table_header(min, max); + + // Table body + for(int row = min; row <= max; ++row) + print_table_row(row, min, max); +} + +int main() +{ + print_table(1, 22); + return 0; } + From 9fe058313ab25f629d54c9afd80c6ea31f255c72 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:37:20 +0300 Subject: [PATCH 03/21] Add files via upload --- Ruby/main.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Ruby/main.rb diff --git a/Ruby/main.rb b/Ruby/main.rb new file mode 100644 index 0000000..d360798 --- /dev/null +++ b/Ruby/main.rb @@ -0,0 +1,12 @@ +def multiplication_table(n) + puts " |" + (" %3d" * n) % [*1..n] + puts "----+" + "----" * n + 1.upto(n) do |x| + print "%3d |" % x + 1.upto(x-1) {|y| print " "} + x.upto(n) {|y| print " %3d" % (x*y)} + puts + end +end + +multiplication_table 22 \ No newline at end of file From ea4d37d79ee60376b2c0f0001f3628899763c437 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:40:00 +0300 Subject: [PATCH 04/21] ARM ASM implemnt --- arm_asm/main.s | 160 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 arm_asm/main.s diff --git a/arm_asm/main.s b/arm_asm/main.s new file mode 100644 index 0000000..ed9190d --- /dev/null +++ b/arm_asm/main.s @@ -0,0 +1,160 @@ + + + +.equ STDOUT, 1 @ Linux output console +.equ EXIT, 1 @ Linux syscall +.equ WRITE, 4 @ Linux syscall +.equ MAXI, 12 + +.data +sMessValeur: .fill 11, 1, ' ' @ size => 11 +szCarriageReturn: .asciz "\n" +sBlanc1: .asciz " " +sBlanc2: .asciz " " +sBlanc3: .asciz " " + +.bss + +.text +.global main +main: @ entry of program + push {fp,lr} @ saves 2 registers + @ display first line + mov r4,#0 +1: @ begin loop + mov r0,r4 + ldr r1,iAdrsMessValeur @ display value + bl conversion10 @ call function + mov r2,#0 @ final zéro + strb r2,[r1,r0] @ on display value + ldr r0,iAdrsMessValeur + bl affichageMess @ display message + cmp r4,#10 @ one or two digit in résult + ldrgt r0,iAdrsBlanc2 @ two display two spaces + ldrle r0,iAdrsBlanc3 @ one display 3 spaces + bl affichageMess @ display message + add r4,#1 @ increment counter + cmp r4,#MAXI + ble 1b @ loop + ldr r0,iAdrszCarriageReturn + bl affichageMess @ display carriage return + + mov r5,#1 @ line counter +2: @ begin loop lines + mov r0,r5 @ display column 1 with N° line + ldr r1,iAdrsMessValeur @ display value + bl conversion10 @ call function + mov r2,#0 @ final zéro + strb r2,[r1,r0] + ldr r0,iAdrsMessValeur + bl affichageMess @ display message + cmp r5,#10 @ one or two digit in N° line + ldrge r0,iAdrsBlanc2 + ldrlt r0,iAdrsBlanc3 + bl affichageMess + mov r4,#1 @ counter column +3: @ begin loop columns + mul r0,r4,r5 @ multiplication + mov r3,r0 @ save résult + ldr r1,iAdrsMessValeur @ display value + bl conversion10 @ call function + mov r2,#0 + strb r2,[r1,r0] + ldr r0,iAdrsMessValeur + bl affichageMess @ display message + cmp r3,#100 @ 3 digits in résult ? + ldrge r0,iAdrsBlanc1 @ yes, display one space + bge 4f + cmp r3,#10 @ 2 digits in result + ldrge r0,iAdrsBlanc2 @ yes display 2 spaces + ldrlt r0,iAdrsBlanc3 @ no display 3 spaces +4: + bl affichageMess @ display message + add r4,#1 @ increment counter column + cmp r4,r5 @ < counter lines + ble 3b @ loop + ldr r0,iAdrszCarriageReturn + bl affichageMess @ display carriage return + add r5,#1 @ increment line counter + cmp r5,#MAXI @ MAXI ? + ble 2b @ loop + +100: @ standard end of the program + mov r0, #0 @ return code + pop {fp,lr} @restaur 2 registers + mov r7, #EXIT @ request to exit program + svc #0 @ perform the system call + +iAdrsMessValeur: .int sMessValeur +iAdrszCarriageReturn: .int szCarriageReturn +iAdrsBlanc1: .int sBlanc1 +iAdrsBlanc2: .int sBlanc2 +iAdrsBlanc3: .int sBlanc3 + +affichageMess: + push {r0,r1,r2,r7,lr} @ save registres + mov r2,#0 @ counter length +1: @ loop length calculation + ldrb r1,[r0,r2] @ read octet start position + index + cmp r1,#0 @ if 0 its over + addne r2,r2,#1 @ else add 1 in the length + bne 1b @ and loop + @ so here r2 contains the length of the message + mov r1,r0 @ address message in r1 + mov r0,#STDOUT @ code to write to the standard output Linux + mov r7, #WRITE @ code call system "write" + svc #0 @ call systeme + pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */ + bx lr @ return + +.equ LGZONECAL, 10 +conversion10: + push {r1-r4,lr} @ save registers + mov r3,r1 + mov r2,#LGZONECAL + +1: @ start loop + bl divisionpar10U @unsigned r0 <- dividende. quotient ->r0 reste -> r1 + add r1,#48 @ digit + strb r1,[r3,r2] @ store digit on area + cmp r0,#0 @ stop if quotient = 0 */ + subne r2,#1 @ else previous position + bne 1b @ and loop + @ and move digit from left of area + mov r4,#0 +2: + ldrb r1,[r3,r2] + strb r1,[r3,r4] + add r2,#1 + add r4,#1 + cmp r2,#LGZONECAL + ble 2b + @ and move spaces in end on area + mov r0,r4 @ result length + mov r1,#' ' @ space +3: + strb r1,[r3,r4] @ store space in area + add r4,#1 @ next position + cmp r4,#LGZONECAL + ble 3b @ loop if r4 <= area size + +100: + pop {r1-r4,lr} @ restaur registres + bx lr @return + + +divisionpar10U: + push {r2,r3,r4, lr} + mov r4,r0 @ save value + mov r3,#0xCCCD @ r3 <- magic_number lower + movt r3,#0xCCCC @ r3 <- magic_number upper + umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0) + mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3 + add r2,r0,r0, lsl #2 @ r2 <- r0 * 5 + sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) + pop {r2,r3,r4,lr} + bx lr @ leave function + + + + From 0d7075bddf24e5b736f6ebb12b32105af1594ff4 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:41:18 +0300 Subject: [PATCH 05/21] Golang implem --- golang/main.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 golang/main.go diff --git a/golang/main.go b/golang/main.go new file mode 100644 index 0000000..3464282 --- /dev/null +++ b/golang/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Print(" x |") + for i := 1; i <= 12; i++ { + fmt.Printf("%4d", i) + } + fmt.Print("\n---+") + for i := 1; i <= 12; i++ { + fmt.Print("----") + } + for j := 1; j <= 12; j++ { + fmt.Printf("\n%2d |", j) + for i := 1; i <= 12; i++ { + if i >= j { + fmt.Printf("%4d", i*j) + } else { + fmt.Print(" ") + } + } + } + fmt.Println("") +} + From 14dfffd3be0bf09997267602f8c9d8589036056d Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:41:45 +0300 Subject: [PATCH 06/21] Corrections to golang --- golang/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/golang/main.go b/golang/main.go index 3464282..cbd1143 100644 --- a/golang/main.go +++ b/golang/main.go @@ -6,16 +6,16 @@ import ( func main() { fmt.Print(" x |") - for i := 1; i <= 12; i++ { + for i := 1; i <= 22; i++ { fmt.Printf("%4d", i) } fmt.Print("\n---+") - for i := 1; i <= 12; i++ { + for i := 1; i <= 22; i++ { fmt.Print("----") } - for j := 1; j <= 12; j++ { + for j := 1; j <= 2; j++ { fmt.Printf("\n%2d |", j) - for i := 1; i <= 12; i++ { + for i := 1; i <= 22; i++ { if i >= j { fmt.Printf("%4d", i*j) } else { From 244c970b80c6ce56ba9232dab323ec7ea4399077 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:42:43 +0300 Subject: [PATCH 07/21] Update main.s --- arm_asm/main.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arm_asm/main.s b/arm_asm/main.s index ed9190d..54c6e37 100644 --- a/arm_asm/main.s +++ b/arm_asm/main.s @@ -4,7 +4,7 @@ .equ STDOUT, 1 @ Linux output console .equ EXIT, 1 @ Linux syscall .equ WRITE, 4 @ Linux syscall -.equ MAXI, 12 +.equ MAXI, 22 .data sMessValeur: .fill 11, 1, ' ' @ size => 11 From f6718e05efd6a95a6724c582c9889fc4ed383739 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:44:26 +0300 Subject: [PATCH 08/21] Add java --- java/main.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 java/main.java diff --git a/java/main.java b/java/main.java new file mode 100644 index 0000000..6daedfa --- /dev/null +++ b/java/main.java @@ -0,0 +1,20 @@ +public class MultiplicationTable { + public static void main(String[] args) { + for (int i = 1; i <= 22; i++) + System.out.print("\t" + i); + + System.out.println(); + for (int i = 0; i < 100; i++) + System.out.print("-"); + System.out.println(); + for (int i = 1; i <= 22; i++) { + System.out.print(i + "|"); + for(int j = 1; j <= 22; j++) { + System.out.print("\t"); + if (j >= i) + System.out.print("\t" + i * j); + } + System.out.println(); + } + } +} From e8d0877707cd4862ec89d6a2384eda17e578e0fe Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:45:53 +0300 Subject: [PATCH 09/21] Added scala --- scala/main.scl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 scala/main.scl diff --git a/scala/main.scl b/scala/main.scl new file mode 100644 index 0000000..cfb77ac --- /dev/null +++ b/scala/main.scl @@ -0,0 +1,17 @@ +print("%5s".format("|")) +for (i <- 1 to 22) print("%5d".format(i)) +println() +println("-----" * 23) + +for (i <- 1 to 22) { + print("%4d|".format(i)) + + for (j <- 1 to 22) { + if (i <= j) + print("%5d".format(i * j)) + else + print("%5s".format("")) + } + + println("") +} From 7a1353ea4fdffb4329709ae1b0e98508a8c5ec1f Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:48:12 +0300 Subject: [PATCH 10/21] Hilarious Lua has been added --- lua/main.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lua/main.lua diff --git a/lua/main.lua b/lua/main.lua new file mode 100644 index 0000000..f632f0f --- /dev/null +++ b/lua/main.lua @@ -0,0 +1,18 @@ +io.write( " |" ) +for i = 1, 22 do + io.write( string.format( "%#5d", i ) ) +end +io.write( "\n", string.rep( "-", 22*5+4 ), "\n" ) + +for i = 1, 22 do + io.write( string.format( "%#2d |", i ) ) + + for j = 1, 22 do + if j < i then + io.write( " " ) + else + io.write( string.format( "%#5d", i*j ) ) + end + end + io.write( "\n" ) +end \ No newline at end of file From 20e1082211c9c1da724fe6ed870e569effb73dfc Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:31:48 +0300 Subject: [PATCH 11/21] Add files via upload --- fsharp/main.fs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 fsharp/main.fs diff --git a/fsharp/main.fs b/fsharp/main.fs new file mode 100644 index 0000000..45685ad --- /dev/null +++ b/fsharp/main.fs @@ -0,0 +1,19 @@ +open System + +let multTable () = + Console.Write (" X".PadRight (4)) + for i = 1 to 22 do Console.Write ((i.ToString "####").PadLeft 4) + Console.Write "\n ___" + for i = 1 to 22 do Console.Write " ___" + Console.WriteLine () + for row = 1 to 22 do + Console.Write (row.ToString("###").PadLeft(3).PadRight(4)) + for col = 1 to 22 do + if row <= col then Console.Write ((row * col).ToString("###").PadLeft(4)) + else + Console.Write ("".PadLeft 4) + Console.WriteLine () + Console.WriteLine () + Console.ReadKey () |> ignore + +multTable () From 91357476eeb6e24fffb13a793651136876ce66ed Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:32:13 +0300 Subject: [PATCH 12/21] Delete main.rb --- Ruby/main.rb | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 Ruby/main.rb diff --git a/Ruby/main.rb b/Ruby/main.rb deleted file mode 100644 index d360798..0000000 --- a/Ruby/main.rb +++ /dev/null @@ -1,12 +0,0 @@ -def multiplication_table(n) - puts " |" + (" %3d" * n) % [*1..n] - puts "----+" + "----" * n - 1.upto(n) do |x| - print "%3d |" % x - 1.upto(x-1) {|y| print " "} - x.upto(n) {|y| print " %3d" % (x*y)} - puts - end -end - -multiplication_table 22 \ No newline at end of file From baddc5a71c87f24508e16da69f0db2e3c9f851ad Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:32:46 +0300 Subject: [PATCH 13/21] Rename ruby folder --- ruby/main.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ruby/main.rb diff --git a/ruby/main.rb b/ruby/main.rb new file mode 100644 index 0000000..d360798 --- /dev/null +++ b/ruby/main.rb @@ -0,0 +1,12 @@ +def multiplication_table(n) + puts " |" + (" %3d" * n) % [*1..n] + puts "----+" + "----" * n + 1.upto(n) do |x| + print "%3d |" % x + 1.upto(x-1) {|y| print " "} + x.upto(n) {|y| print " %3d" % (x*y)} + puts + end +end + +multiplication_table 22 \ No newline at end of file From 6bef48a598eec75bb693b9c85f7ae43adeb89fb0 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:33:01 +0300 Subject: [PATCH 14/21] Rename ruby folder From 8bc88fb5c26a1994f071660abaea357c3ef9e1b1 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:34:19 +0300 Subject: [PATCH 15/21] Rust implem --- rust/main.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rust/main.rs diff --git a/rust/main.rs b/rust/main.rs new file mode 100644 index 0000000..89a0db5 --- /dev/null +++ b/rust/main.rs @@ -0,0 +1,17 @@ +fn main() { + let xs = (1..=22) + .map(|a| { + (1..=22) + .map(|b| { + if a > b { + String::from(" ") + } else { + format!("{:4}", a * b) + } + }) + .collect::() + }) + .collect::>(); + + println!("{}", xs.join("\n")) +} \ No newline at end of file From d59d03c292e1981392741cf70d72f08952668a94 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:36:10 +0300 Subject: [PATCH 16/21] Added perl --- perl/main.pl | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 perl/main.pl diff --git a/perl/main.pl b/perl/main.pl new file mode 100644 index 0000000..d106b66 --- /dev/null +++ b/perl/main.pl @@ -0,0 +1,10 @@ +our $max = 22; +our $width = length($max**2) + 1; + +printf "%*s", $width, $_ foreach 'x|', 1..$max; +print "\n", '-' x ($width - 1), '+', '-' x ($max*$width), "\n"; +foreach my $i (1..$max) { + printf "%*s", $width, $_ + foreach "$i|", map { $_ >= $i and $_*$i } 1..$max; + print "\n"; +} \ No newline at end of file From 34a0b8cbcae95e187dcaaf495989d63a7e81b8ce Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:38:20 +0300 Subject: [PATCH 17/21] powershell --- powershell/main.ps1 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 powershell/main.ps1 diff --git a/powershell/main.ps1 b/powershell/main.ps1 new file mode 100644 index 0000000..de6573d --- /dev/null +++ b/powershell/main.ps1 @@ -0,0 +1,20 @@ +# For clarity +$Tab = "`t" + + +$Tab + ( 1..22 -join $Tab ) + + +ForEach ( $i in 1..22 ) + { + $( + $i + + @( "" ) * ( $i - 1 ) + + # Calculate + $i..22 | ForEach { $i * $_ } + + # Combine + ) -join $Tab + } \ No newline at end of file From 2b094389f8b1111bd114ff0f7e95ddea85917308 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:39:43 +0300 Subject: [PATCH 18/21] Added R language --- rlang/main.r | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 rlang/main.r diff --git a/rlang/main.r b/rlang/main.r new file mode 100644 index 0000000..85b81d4 --- /dev/null +++ b/rlang/main.r @@ -0,0 +1,10 @@ +multiplication_table <- function(n=22) +{ + one_to_n <- 1:n + x <- matrix(one_to_n) %*% t(one_to_n) + x[lower.tri(x)] <- 0 + rownames(x) <- colnames(x) <- one_to_n + print(as.table(x), zero.print="") + invisible(x) +} +multiplication_table() \ No newline at end of file From 0a2c42c85020797af78271cd455965ae27ce9bca Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:41:25 +0300 Subject: [PATCH 19/21] VB6 Standard added --- vb/main.vb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 vb/main.vb diff --git a/vb/main.vb b/vb/main.vb new file mode 100644 index 0000000..5e333ac --- /dev/null +++ b/vb/main.vb @@ -0,0 +1,24 @@ +Sub Main() + Const nmax = 22, xx = 3 + Const x = xx + 1 + Dim i As Integer, j As Integer, s As String + s = String(xx, " ") & " |" + For j = 1 To nmax + s = s & Right(String(x, " ") & j, x) + Next j + Debug.Print s + s = String(xx, "-") & " +" + For j = 1 To nmax + s = s & " " & String(xx, "-") + Next j + Debug.Print s + For i = 1 To nmax + s = Right(String(xx, " ") & i, xx) & " |" + For j = 1 To nmax + If j >= i _ + Then s = s & Right(String(x, " ") & i * j, x) _ + Else s = s & String(x, " ") + Next j + Debug.Print s + Next i +End Sub 'Main \ No newline at end of file From cf04300b84030262d123e1753c5ed23146b31706 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Mon, 18 Oct 2021 23:59:31 +0300 Subject: [PATCH 20/21] Redo (again) --- cpp/main.cpp | 195 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 122 insertions(+), 73 deletions(-) diff --git a/cpp/main.cpp b/cpp/main.cpp index 9755a47..d0ea745 100644 --- a/cpp/main.cpp +++ b/cpp/main.cpp @@ -1,78 +1,127 @@ #include +#include #include -#include // for log10() -#include // for max() - -size_t table_column_width(const int min, const int max) -{ - unsigned int abs_max = std::max(max*max, min*min); - - // abs_max is the largest absolute value we might see. - // If we take the log10 and add one, we get the string width - // of the largest possible absolute value. - // Add one more for a little whitespace guarantee. - size_t colwidth = 2 + std::log10(abs_max); - - // If only one of them is less than 0, then some will - // be negative. If some values may be negative, then we need to add some space - // for a sign indicator (-) - if (min < 0 && max > 0) - ++colwidth; - return colwidth; -} - -struct Writer_ -{ - decltype(std::setw(1)) fmt_; - Writer_(size_t w) : fmt_(std::setw(w)) {} - template Writer_& operator()(const T_& info) { std::cout << fmt_ << info; return *this; } -}; - -void print_table_header(const int min, const int max) -{ - Writer_ write(table_column_width(min, max)); - - // table corner - write(" "); - for(int col = min; col <= max; ++col) - write(col); - - // End header with a newline and blank line. - std::cout << std::endl << std::endl; -} - -void print_table_row(const int num, const int min, const int max) -{ - Writer_ write(table_column_width(min, max)); - - // Header column - write(num); - - // Spacing to ensure only the top half is printed - for(int multiplicand = min; multiplicand < num; ++multiplicand) - write(" "); - - // Remaining multiplicands for the row. - for(int multiplicand = num; multiplicand <= max; ++multiplicand) - write(num * multiplicand); - - // End row with a newline and blank line. + +const unsigned int ROWS = 10; +const unsigned int COLUMNS = 10; + +void randomValue(unsigned int &, unsigned int &, unsigned int &); + +int main() +{ + unsigned int rValue; + unsigned int cValue; + unsigned int pValue; + + unsigned int rowError; + unsigned int columnError; + unsigned int productError; + + unsigned int userRowGuess; + unsigned int userColumnGuess; + + char choice; + + do + { + randomValue(rValue, cValue, pValue); + + std::cout << "\033[2J\033[0;0H"; // This code clears the screen + std::cout << std::endl << std::endl; + std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; + std::cout << std::setw(35) << "+ Multiplication Table +" << std::endl; + std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; + std::cout << std::endl << std::endl; + + while (true) + { + rowError = rValue; + columnError = cValue; + productError = pValue; + + if (productError == rowError * columnError) + { + productError--; + } + + for (unsigned int row = 1; row <= ROWS; row++) + { + for (unsigned int column = 1; column <= COLUMNS; column++) + { + if (row == rowError && column == columnError) + { + std::cout << "\t" << "" << productError; + } + else + { + std::cout << "\t" << row * column; + } + } + std::cout << std::endl; + } + break; + } + + std::cout << std::endl << std::endl; + std::cout << "\t\t" << "Type in the column number " << std::endl; + std::cout << "\t\t" << "of the location of the " << std::endl; + std::cout << "\t\t" << "error & then press " << std::endl; + std::cout << "\t\t" << "[Enter]: "; + std::cin >> userColumnGuess; + std::cout << std::endl; + + std::cout << "\t\t" << "Type in the row number " << std::endl; + std::cout << "\t\t" << "of the location of the " << std::endl; + std::cout << "\t\t" << "error & then press " << std::endl; + std::cout << "\t\t" << "[Enter]: "; + std::cin >> userRowGuess; + std::cout << std::endl; + + if (userRowGuess != rowError && userColumnGuess != columnError) + { + std::cout << "\t\t" << "Your answer was incorrect!" << std::endl << std::endl; + std::cout << "\t\t" << "Error value '" << productError << "' is located" << std::endl; + std::cout << "\t\t" << "on row " << rowError << ", column " << columnError << "." << std::endl; + } + else + { + std::cout << "\t\t" << "You are correct! You win!" << std::endl; + } + + + std::cout << std::endl; + std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; + std::cout << "\t\t" << "Type in 'Y' for yes or 'N'" << std::endl; + std::cout << "\t\t" << "for no & then press [Enter]: "; + std::cin >> choice; + + while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N') + { + std::cout << std::endl; + std::cout << "\t\t" << "Invalid entry. Only 'Y' or 'N'" << std::endl; + std::cout << "\t\t" << "are accepted answers." << std::endl << std::endl; + std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; + std::cout << "\t\t" << "Type in 'Y' for yes or 'N' for" << std::endl; + std::cout << "\t\t" << "no & then press [Enter]: "; + std::cin >> choice; + } + std::cout << std::endl; + + } while (choice == 'y' || choice == 'Y'); + + std::cout << "\t\t" << "Press [Enter] to continue....." << std::endl; + std::cin.get(); + std::cin.get(); + + return 0; } - -void print_table(const int min, const int max) -{ - // Header row - print_table_header(min, max); - - // Table body - for(int row = min; row <= max; ++row) - print_table_row(row, min, max); -} - -int main() -{ - print_table(1, 22); - return 0; + +void randomValue(unsigned int &rValue, unsigned int &cValue, unsigned int &pValue) +{ + srand((unsigned)time(NULL)); + + unsigned int r = rValue = (rand() % ROWS) + 1; + unsigned int c = cValue = (rand() % COLUMNS) + 1; + unsigned int p = pValue = (rand() % (ROWS * COLUMNS)) + 1; } - From 114333d9c0fe00e994c3f20f76ce27eb91421ce2 Mon Sep 17 00:00:00 2001 From: Mello <67687232+Mellozx@users.noreply.github.com> Date: Tue, 19 Oct 2021 00:00:51 +0300 Subject: [PATCH 21/21] Simplify --- cpp/main.cpp | 188 ++++++++++++++++++--------------------------------- 1 file changed, 66 insertions(+), 122 deletions(-) diff --git a/cpp/main.cpp b/cpp/main.cpp index d0ea745..d5bb635 100644 --- a/cpp/main.cpp +++ b/cpp/main.cpp @@ -1,127 +1,71 @@ #include -#include #include - -const unsigned int ROWS = 10; -const unsigned int COLUMNS = 10; - -void randomValue(unsigned int &, unsigned int &, unsigned int &); - -int main() -{ - unsigned int rValue; - unsigned int cValue; - unsigned int pValue; - - unsigned int rowError; - unsigned int columnError; - unsigned int productError; - - unsigned int userRowGuess; - unsigned int userColumnGuess; - - char choice; - - do - { - randomValue(rValue, cValue, pValue); - - std::cout << "\033[2J\033[0;0H"; // This code clears the screen - std::cout << std::endl << std::endl; - std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; - std::cout << std::setw(35) << "+ Multiplication Table +" << std::endl; - std::cout << std::setw(35) << "++++++++++++++++++++++++" << std::endl; - std::cout << std::endl << std::endl; - - while (true) - { - rowError = rValue; - columnError = cValue; - productError = pValue; - - if (productError == rowError * columnError) - { - productError--; - } - - for (unsigned int row = 1; row <= ROWS; row++) - { - for (unsigned int column = 1; column <= COLUMNS; column++) - { - if (row == rowError && column == columnError) - { - std::cout << "\t" << "" << productError; - } - else - { - std::cout << "\t" << row * column; - } - } - std::cout << std::endl; - } - break; - } - - +#include // for log10() +#include // for max() + +size_t table_column_width(const int min, const int max) +{ + unsigned int abs_max = std::max(max*max, min*min); + + size_t colwidth = 2 + std::log10(abs_max); + + if (min < 0 && max > 0) + ++colwidth; + return colwidth; +} + +struct Writer_ +{ + decltype(std::setw(1)) fmt_; + Writer_(size_t w) : fmt_(std::setw(w)) {} + template Writer_& operator()(const T_& info) { std::cout << fmt_ << info; return *this; } +}; + +void print_table_header(const int min, const int max) +{ + Writer_ write(table_column_width(min, max)); + + // table corner + write(" "); + for(int col = min; col <= max; ++col) + write(col); + + // End header with a newline and blank line. std::cout << std::endl << std::endl; - std::cout << "\t\t" << "Type in the column number " << std::endl; - std::cout << "\t\t" << "of the location of the " << std::endl; - std::cout << "\t\t" << "error & then press " << std::endl; - std::cout << "\t\t" << "[Enter]: "; - std::cin >> userColumnGuess; - std::cout << std::endl; - - std::cout << "\t\t" << "Type in the row number " << std::endl; - std::cout << "\t\t" << "of the location of the " << std::endl; - std::cout << "\t\t" << "error & then press " << std::endl; - std::cout << "\t\t" << "[Enter]: "; - std::cin >> userRowGuess; - std::cout << std::endl; - - if (userRowGuess != rowError && userColumnGuess != columnError) - { - std::cout << "\t\t" << "Your answer was incorrect!" << std::endl << std::endl; - std::cout << "\t\t" << "Error value '" << productError << "' is located" << std::endl; - std::cout << "\t\t" << "on row " << rowError << ", column " << columnError << "." << std::endl; - } - else - { - std::cout << "\t\t" << "You are correct! You win!" << std::endl; - } - - - std::cout << std::endl; - std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; - std::cout << "\t\t" << "Type in 'Y' for yes or 'N'" << std::endl; - std::cout << "\t\t" << "for no & then press [Enter]: "; - std::cin >> choice; - - while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N') - { - std::cout << std::endl; - std::cout << "\t\t" << "Invalid entry. Only 'Y' or 'N'" << std::endl; - std::cout << "\t\t" << "are accepted answers." << std::endl << std::endl; - std::cout << "\t\t" << "Would you like to play again?" << std::endl << std::endl; - std::cout << "\t\t" << "Type in 'Y' for yes or 'N' for" << std::endl; - std::cout << "\t\t" << "no & then press [Enter]: "; - std::cin >> choice; - } - std::cout << std::endl; - - } while (choice == 'y' || choice == 'Y'); - - std::cout << "\t\t" << "Press [Enter] to continue....." << std::endl; - std::cin.get(); - std::cin.get(); - - return 0; } - -void randomValue(unsigned int &rValue, unsigned int &cValue, unsigned int &pValue) -{ - srand((unsigned)time(NULL)); - - unsigned int r = rValue = (rand() % ROWS) + 1; - unsigned int c = cValue = (rand() % COLUMNS) + 1; - unsigned int p = pValue = (rand() % (ROWS * COLUMNS)) + 1; + +void print_table_row(const int num, const int min, const int max) +{ + Writer_ write(table_column_width(min, max)); + + // Header column + write(num); + + // Spacing to ensure only the top half is printed + for(int multiplicand = min; multiplicand < num; ++multiplicand) + write(" "); + + // Remaining multiplicands for the row. + for(int multiplicand = num; multiplicand <= max; ++multiplicand) + write(num * multiplicand); + + // End row with a newline and blank line. + std::cout << std::endl << std::endl; +} + +void print_table(const int min, const int max) +{ + // Header row + print_table_header(min, max); + + // Table body + for(int row = min; row <= max; ++row) + print_table_row(row, min, max); +} + +int main() +{ + print_table(1, 22); + return 0; } +