Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions 06a-advanced-exercises/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ package main

import (
"fmt"
"strconv"
)

func main() {
printTable(2, 7)
multiplicationTable(2, 7)
}

// BEGIN OMIT
func printTable(a, b int) {
// Print header
fmt.Printf(" X|")
for j := a; j <= b; j++ {
fmt.Printf("%4d", j)
func multiplicationTable(low, high int) {
maxwidth := len(strconv.Itoa(high*high)) + 1
highwidth := len(strconv.Itoa(high))
fmt.Printf("%*s |", highwidth, "X")
for i := low; i <= high; i++ {
fmt.Printf("%*d", maxwidth, i)
}
fmt.Printf("\n")

// Print divider
fmt.Printf("----+")
for j := a; j <= b; j++ {
fmt.Printf("----")
// print lines
for i := 0; i < highwidth+(maxwidth*(high-low+1))+2; i++ {
fmt.Printf("-")
}
fmt.Printf("\n")

// Print numbers
for i := a; i <= b; i++ {
fmt.Printf("%4d|", i)
for j := a; j <= b; j++ {
fmt.Printf("%4d", i*j)
for i := low; i <= high; i++ {
fmt.Printf("%*d |", highwidth, i)
for j := low; j <= high; j++ {
fmt.Printf("%*d", maxwidth, i*j)
}
fmt.Printf("\n")
}
Expand Down