From f638888995bfeb86399580bce13a710707d29e44 Mon Sep 17 00:00:00 2001 From: Bobby Hamblin Date: Sun, 21 Jan 2024 18:18:34 -0700 Subject: [PATCH] add chapter 8 exercises --- 08/average/Makefile | 13 + 08/average/average.cpp | 39 ++ 08/average/average.md | 19 + 08/checkerboard/Makefile | 13 + 08/checkerboard/checkerboard.cpp | 37 ++ 08/checkerboard/checkerboard.md | 19 + 08/multiplication-table/Makefile | 13 + .../multiplication-table.cpp | 19 + .../multiplication-table.md | 24 ++ 08/numbers-to-words/Makefile | 13 + 08/numbers-to-words/numbers-to-words.cpp | 332 ++++++++++++++++++ 08/numbers-to-words/numbers-to-words.md | 20 ++ 08/resistance-calculator/Makefile | 13 + .../resistance-calculator.cpp | 45 +++ .../resistance-calculator.md | 22 ++ 08/vowel-or-consonant/Makefile | 13 + 08/vowel-or-consonant/vowel-or-consonant.cpp | 85 +++++ 08/vowel-or-consonant/vowel-or-consonant.md | 17 + 18 files changed, 756 insertions(+) create mode 100644 08/average/Makefile create mode 100644 08/average/average.cpp create mode 100644 08/average/average.md create mode 100644 08/checkerboard/Makefile create mode 100644 08/checkerboard/checkerboard.cpp create mode 100644 08/checkerboard/checkerboard.md create mode 100644 08/multiplication-table/Makefile create mode 100644 08/multiplication-table/multiplication-table.cpp create mode 100644 08/multiplication-table/multiplication-table.md create mode 100644 08/numbers-to-words/Makefile create mode 100644 08/numbers-to-words/numbers-to-words.cpp create mode 100644 08/numbers-to-words/numbers-to-words.md create mode 100644 08/resistance-calculator/Makefile create mode 100644 08/resistance-calculator/resistance-calculator.cpp create mode 100644 08/resistance-calculator/resistance-calculator.md create mode 100644 08/vowel-or-consonant/Makefile create mode 100644 08/vowel-or-consonant/vowel-or-consonant.cpp create mode 100644 08/vowel-or-consonant/vowel-or-consonant.md diff --git a/08/average/Makefile b/08/average/Makefile new file mode 100644 index 0000000..86a45aa --- /dev/null +++ b/08/average/Makefile @@ -0,0 +1,13 @@ +# Makefile for program average + +# Turn on debugging +CXXFLAGS=-g + +average:average.cpp + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean:average + rm $^ + +run:average + ./$^ diff --git a/08/average/average.cpp b/08/average/average.cpp new file mode 100644 index 0000000..3becda2 --- /dev/null +++ b/08/average/average.cpp @@ -0,0 +1,39 @@ +/**************************************** +* average -- average numbers * +* Usage: Input numbers at the prompt, * +* and enter 0 to exit * +****************************************/ + +#include + +int i; // counts how many numbers have been input +float number; // number being input by user +float running_total = 0; // running total of all input numbers +float average; // final average + +int main() { + std::cout << "average\n"; + + while(true) { + // user input + std::cout << "Input number to average: "; + std::cin >> number; + + // exit condition + if(number == 0) { + break; + } else { + // add number to running total + running_total += number; + // increment counter + i++; + } + } + + // calculate average + average = running_total / i; + + // print average + std::cout << "Average of the numbers input: " << average << "\n"; +} + diff --git a/08/average/average.md b/08/average/average.md new file mode 100644 index 0000000..22eda5a --- /dev/null +++ b/08/average/average.md @@ -0,0 +1,19 @@ +# average + +> Averages numbers +> Users' Specification +> Jan 20, 2023 Bobby Hamblin + +Averages however many numbers the user pleases. Enter numbers at the +prompt, and 0 to stop totalling. + +## Example + +> Input number to average: *8* +> Input number to average: *19.5* +> Input number to average: *2.91* +> Input number to average: *-20.9* +> Input number to average: *89.19* +> Input number to average: *0* +> Average of the numbers input: 19.74 + diff --git a/08/checkerboard/Makefile b/08/checkerboard/Makefile new file mode 100644 index 0000000..239150f --- /dev/null +++ b/08/checkerboard/Makefile @@ -0,0 +1,13 @@ +# Makefile for program checkerboard + +# Turn on debugging +CXXFLAGS=-g + +checkerboard:checkerboard.cpp + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean:checkerboard + rm $^ + +run:checkerboard + ./$^ diff --git a/08/checkerboard/checkerboard.cpp b/08/checkerboard/checkerboard.cpp new file mode 100644 index 0000000..cd424c8 --- /dev/null +++ b/08/checkerboard/checkerboard.cpp @@ -0,0 +1,37 @@ +/**************************************** +* checkerboard -- print an 8x8 checker * +* board to the terminal * +****************************************/ + +#include + +int i; // counter 1 +int j; // counter 2 +int k; // counter 3 + +int main() { + std::cout << "checkerboard\n"; + + // print 8 checkerboard sqares + for(i = 0; i < 8; i++) { + // print 1 horizontal line + for(j = 0; j < 8; j++) { + std::cout << "+-----"; + } + std::cout << "+\n"; + + // print 3 vertical lines + for(j = 0; j < 3; j++) { + for(k = 0; k < 8; k++) { + std:cout << "| "; + } + std::cout << "|\n"; + } + } + + // print final horizontal line + for(i = 0; i < 8; i++) { + std::cout << "+-----"; + } + std::cout << "+\n"; +} diff --git a/08/checkerboard/checkerboard.md b/08/checkerboard/checkerboard.md new file mode 100644 index 0000000..61aa7db --- /dev/null +++ b/08/checkerboard/checkerboard.md @@ -0,0 +1,19 @@ +# checkerboard + +> Print an 8 by 8 checkerboard +> Users' Specification +> Jan 20, 2023 Bobby Hamblin + +Prints an 8x8 grid of squares, each 5 by 3 characters wide + +## Example + +> +-----+-----+ +> | | | +> | | | +> | | | +> +-----+-----+ +> | | | +> | | | +> | | | +> +-----+-----+ diff --git a/08/multiplication-table/Makefile b/08/multiplication-table/Makefile new file mode 100644 index 0000000..a1cb05d --- /dev/null +++ b/08/multiplication-table/Makefile @@ -0,0 +1,13 @@ +# Makefile for program multiplication-table + +# Turn on debugging +CXXFLAGS=-g + +multiplication-table:multiplication-table.cpp + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean:multiplication-table + rm $^ + +run:multiplication-table + ./$^ diff --git a/08/multiplication-table/multiplication-table.cpp b/08/multiplication-table/multiplication-table.cpp new file mode 100644 index 0000000..aefea4d --- /dev/null +++ b/08/multiplication-table/multiplication-table.cpp @@ -0,0 +1,19 @@ +/**************************************** +* multiplication-table -- print a * +* multiplication table * +****************************************/ + +#include + +int main() { + std::cout << "multiplication-table\n"; + + for(int i = 0; i <= 12; i++){ + for(int j = 0; j <= 12; j++) { + std::cout << i * j << " "; + } + + std::cout << "\n"; + } +} + diff --git a/08/multiplication-table/multiplication-table.md b/08/multiplication-table/multiplication-table.md new file mode 100644 index 0000000..470ace3 --- /dev/null +++ b/08/multiplication-table/multiplication-table.md @@ -0,0 +1,24 @@ +# multiplication-table + +> Print a multiplication table +> Users' Specification +> Jan 20, 2023 Bobby Hamblin + +Print a multiplication table to the terminal. That's it. + +## Example + +> 0 0 0 0 0 0 0 0 0 0 0 0 0 +> 0 1 2 3 4 5 6 7 8 9 10 11 12 +> 0 2 4 6 8 10 12 14 16 18 20 22 24 +> 0 3 6 9 12 15 18 21 24 27 30 33 36 +> 0 4 8 12 16 20 24 28 32 36 40 44 48 +> 0 5 10 15 20 25 30 35 40 45 50 55 60 +> 0 6 12 18 24 30 36 42 48 54 60 66 72 +> 0 7 14 21 28 35 42 49 56 63 70 77 84 +> 0 8 16 24 32 40 48 56 64 72 80 88 96 +> 0 9 18 27 36 45 54 63 72 81 90 99 108 +> 0 10 20 30 40 50 60 70 80 90 100 110 120 +> 0 11 22 33 44 55 66 77 88 99 110 121 132 +> 0 12 24 36 48 60 72 84 96 108 120 132 144 + diff --git a/08/numbers-to-words/Makefile b/08/numbers-to-words/Makefile new file mode 100644 index 0000000..038f680 --- /dev/null +++ b/08/numbers-to-words/Makefile @@ -0,0 +1,13 @@ +# Makefile for program numbers-to-words + +# Turn on debugging +CXXFLAGS=-g + +numbers-to-words:numbers-to-words.cpp + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean:numbers-to-words + rm $^ + +run:numbers-to-words + ./$^ diff --git a/08/numbers-to-words/numbers-to-words.cpp b/08/numbers-to-words/numbers-to-words.cpp new file mode 100644 index 0000000..dca6716 --- /dev/null +++ b/08/numbers-to-words/numbers-to-words.cpp @@ -0,0 +1,332 @@ +/**************************************** +* numbers-to-words -- print word forms * +* of numbers +* Usage: Enter a number between 0-100 * +* at the prompt. Enter a number * +* outside that range to exit * +****************************************/ + +#include +#include + +int number; // number to convert + +int main() { + std::cout << "numbers-to-words\n"; + + while(true) { + // user input + std::cout << "Enter a number between 0 and 100: "; + std::cin >> number; + + // print word form + switch(number) { + case 0: + std::cout << "zero\n"; + break; + case 1: + std::cout << "one\n"; + break; + case 2: + std::cout << "two\n"; + break; + case 3: + std::cout << "three\n"; + break; + case 4: + std::cout << "four\n"; + break; + case 5: + std::cout << "five\n"; + break; + case 6: + std::cout << "six\n"; + break; + case 7: + std::cout << "seven\n"; + break; + case 8: + std::cout << "eight\n"; + break; + case 9: + std::cout << "nine\n"; + break; + case 10: + std::cout << "ten\n"; + break; + case 11: + std::cout << "eleven\n"; + break; + case 12: + std::cout << "twelve\n"; + break; + case 13: + std::cout << "thirteen\n"; + break; + case 14: + std::cout << "fourteen\n"; + break; + case 15: + std::cout << "fifteen\n"; + break; + case 16: + std::cout << "sixteen\n"; + break; + case 17: + std::cout << "seventeen\n"; + break; + case 18: + std::cout << "eighteen\n"; + break; + case 19: + std::cout << "nineteen\n"; + break; + case 20: + std::cout << "twenty\n"; + break; + case 21: + std::cout << "twenty-one\n"; + break; + case 22: + std::cout << "twenty-two\n"; + break; + case 23: + std::cout << "twenty-three\n"; + break; + case 24: + std::cout << "twenty-four\n"; + break; + case 25: + std::cout << "twenty-five\n"; + break; + case 26: + std::cout << "twenty-six\n"; + break; + case 27: + std::cout << "twenty-seven\n"; + break; + case 28: + std::cout << "twenty-eight\n"; + break; + case 29: + std::cout << "twenty-nine\n"; + break; + case 30: + std::cout << "thirty\n"; + break; + case 31: + std::cout << "thirty-one\n"; + break; + case 32: + std::cout << "thirty-two\n"; + break; + case 33: + std::cout << "thirty-three\n"; + break; + case 34: + std::cout << "thirty-four\n"; + break; + case 35: + std::cout << "thirty-five\n"; + break; + case 36: + std::cout << "thirty-six\n"; + break; + case 37: + std::cout << "thirty-seven\n"; + break; + case 38: + std::cout << "thirty-eight\n"; + break; + case 39: + std::cout << "thirty-nine\n"; + break; + case 40: + std::cout << "forty\n"; + break; + case 41: + std::cout << "forty-one\n"; + break; + case 42: + std::cout << "forty-two\n"; + break; + case 43: + std::cout << "forty-three\n"; + break; + case 44: + std::cout << "forty-four\n"; + break; + case 45: + std::cout << "forty-five\n"; + break; + case 46: + std::cout << "forty-six\n"; + break; + case 47: + std::cout << "forty-seven\n"; + break; + case 48: + std::cout << "forty-eight\n"; + break; + case 49: + std::cout << "forty-nine\n"; + break; + case 50: + std::cout << "fifty\n"; + break; + case 51: + std::cout << "fifty-one\n"; + break; + case 52: + std::cout << "fifty-two\n"; + break; + case 53: + std::cout << "fifty-three\n"; + break; + case 54: + std::cout << "fifty-four\n"; + break; + case 55: + std::cout << "fifty-five\n"; + break; + case 56: + std::cout << "fifty-six\n"; + break; + case 57: + std::cout << "fifty-seven\n"; + break; + case 58: + std::cout << "fifty-eight\n"; + break; + case 59: + std::cout << "fifty-nine\n"; + break; + case 60: + std::cout << "sixty\n"; + break; + case 61: + std::cout << "sixty-one\n"; + break; + case 62: + std::cout << "sixty-two\n"; + break; + case 63: + std::cout << "sixty-three\n"; + break; + case 64: + std::cout << "sixty-four\n"; + break; + case 65: + std::cout << "sixty-five\n"; + break; + case 66: + std::cout << "sixty-six\n"; + break; + case 67: + std::cout << "sixty-seven\n"; + break; + case 68: + std::cout << "sixty-eight\n"; + break; + case 69: + std::cout << "sixty-nine\n"; + break; + case 70: + std::cout << "seventy\n"; + break; + case 71: + std::cout << "seventy-one\n"; + break; + case 72: + std::cout << "seventy-two\n"; + break; + case 73: + std::cout << "seventy-three\n"; + break; + case 74: + std::cout << "seventy-four\n"; + break; + case 75: + std::cout << "seventy-five\n"; + break; + case 76: + std::cout << "seventy-six\n"; + break; + case 77: + std::cout << "seventy-seven\n"; + break; + case 78: + std::cout << "seventy-eight\n"; + break; + case 79: + std::cout << "seventy-nine\n"; + break; + case 80: + std::cout << "eighty\n"; + break; + case 81: + std::cout << "eighty-one\n"; + break; + case 82: + std::cout << "eighty-two\n"; + break; + case 83: + std::cout << "eighty-three\n"; + break; + case 84: + std::cout << "eighty-four\n"; + break; + case 85: + std::cout << "eighty-five\n"; + break; + case 86: + std::cout << "eighty-six\n"; + break; + case 87: + std::cout << "eighty-seven\n"; + break; + case 88: + std::cout << "eighty-eight\n"; + break; + case 89: + std::cout << "eighty-nine\n"; + break; + case 90: + std::cout << "ninety\n"; + break; + case 91: + std::cout << "ninety-one\n"; + break; + case 92: + std::cout << "ninety-two\n"; + break; + case 93: + std::cout << "ninety-three\n"; + break; + case 94: + std::cout << "ninety-four\n"; + break; + case 95: + std::cout << "ninety-five\n"; + break; + case 96: + std::cout << "ninety-six\n"; + break; + case 97: + std::cout << "ninety-seven\n"; + break; + case 98: + std::cout << "ninety-eight\n"; + break; + case 99: + std::cout << "ninety-nine\n"; + break; + case 100: + std::cout << "one hundred\n"; + break; + default: + return(0); + } + } +} + diff --git a/08/numbers-to-words/numbers-to-words.md b/08/numbers-to-words/numbers-to-words.md new file mode 100644 index 0000000..9346333 --- /dev/null +++ b/08/numbers-to-words/numbers-to-words.md @@ -0,0 +1,20 @@ +# numbers-to-words + +> Prints numbers as words +> Users' Specification +> Jan 20, 2023 Bobby Hamblin + +For numbers 0-100, this program prints the number input as a word. +Enter a number at the prompt, or a number outside that range to exit. + +## Example + +> Enter a number between 0 and 100: **0** +> zero +> Enter a number between 0 and 100: **17** +> seventeen +> Enter a number between 0 and 100: **58** +> fifty-eight +> Enter a number between 0 and 100: **100** +> one hundred + diff --git a/08/resistance-calculator/Makefile b/08/resistance-calculator/Makefile new file mode 100644 index 0000000..8ed7955 --- /dev/null +++ b/08/resistance-calculator/Makefile @@ -0,0 +1,13 @@ +# Makefile for program resistance-calculator + +# Turn on debugging +CXXFLAGS=-g + +resistance-calculator:resistance-calculator.cpp + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean:resistance-calculator + rm $^ + +run:resistance-calculator + ./$^ diff --git a/08/resistance-calculator/resistance-calculator.cpp b/08/resistance-calculator/resistance-calculator.cpp new file mode 100644 index 0000000..6382a3c --- /dev/null +++ b/08/resistance-calculator/resistance-calculator.cpp @@ -0,0 +1,45 @@ +/**************************************** +* resistance-calculator -- calculate * +* resistance of parallel resistors * +* Usage: Enter resistance in ohms at * +* the prompt. Enter 0 to calculate * +* and exit. * +****************************************/ + +#include + +#define MAX_RESISTORS 100 + +int i = 0; // counter +float resistor; // resistor being input +float biggest_resistor = 0; // resistance of most resistive resistor +float total_resistance; // total resistance across network in ohms + +int main() { + std::cout << "resistance-calculator\n"; + + while(true) { + // user input + std::cout << "Resistance of R" << i+1 << " in ohms: "; + std::cin >> resistor; + + // exit condition + if(resistor == 0) { + break; + } + + // determine biggest resistor + if(resistor > biggest_resistor) + biggest_resistor = resistor; + + // increment counter + i++; + } + + // calculate total resistance + total_resistance = biggest_resistor / (i+1); + + // print total resistance + std::cout << "Total resistance across network: " << total_resistance << " ohms\n"; + +} diff --git a/08/resistance-calculator/resistance-calculator.md b/08/resistance-calculator/resistance-calculator.md new file mode 100644 index 0000000..5281ac2 --- /dev/null +++ b/08/resistance-calculator/resistance-calculator.md @@ -0,0 +1,22 @@ +# resistance-calculator + +> Calculate resistance across parallel resistors +> Users' Specification +> Jan 20, 2023 Bobby Hamblin + +Computes the total resistance of any number of parallel resistors +in ohms according to the following equation: + +1/R = 1/R1 + 1/R2 + 1/R3 ... + 1/Rn + +Enter the resistance in ohms of each resistor wired in parallel at the +prompt. Enter 0 for the resistance to stop input and begin calculation. + +## Example + +> resistance-calculator +> Resistance of R1 in ohms: **400** +> Resistance of R2 in ohms: **200** +> Resistance of R3 in ohms: **0** +> Total resistance across network: 133.33 ohms + diff --git a/08/vowel-or-consonant/Makefile b/08/vowel-or-consonant/Makefile new file mode 100644 index 0000000..3379219 --- /dev/null +++ b/08/vowel-or-consonant/Makefile @@ -0,0 +1,13 @@ +# Makefile for the program vowel-or-consonant + +# Turn on debugging +CXXFLAGS=-g + +vowel-or-consonant:vowel-or-consonant.cpp + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean:vowel-or-consonant + rm $^ + +run:vowel-or-consonant + ./$^ diff --git a/08/vowel-or-consonant/vowel-or-consonant.cpp b/08/vowel-or-consonant/vowel-or-consonant.cpp new file mode 100644 index 0000000..a934485 --- /dev/null +++ b/08/vowel-or-consonant/vowel-or-consonant.cpp @@ -0,0 +1,85 @@ +/**************************************** +* vowel_or_consonant - determine if a * +* letter is a vowel or a consonant * +* Usage: Type in character at prompt * +****************************************/ + +#include + +char letter; /* character input */ + +int main(void) { + while(true) { + /* receive user input */ + std::cout << "Enter a letter (a-z, A-Z): "; + std::cin >> letter; + + /* check for vowel, consonant, or non-letter */ + switch (letter) { + case 'b': + case 'B': + case 'c': + case 'C': + case 'd': + case 'D': + case 'f': + case 'F': + case 'g': + case 'G': + case 'h': + case 'H': + case 'j': + case 'J': + case 'k': + case 'K': + case 'l': + case 'L': + case 'm': + case 'M': + case 'n': + case 'N': + case 'p': + case 'P': + case 'q': + case 'Q': + case 'r': + case 'R': + case 's': + case 'S': + case 't': + case 'T': + case 'v': + case 'V': + case 'w': + case 'W': + case 'x': + case 'X': + case 'z': + case 'Z': + std::cout << letter << " is a consonant\n"; + break; + case 'a': + case 'A': + case 'e': + case 'E': + case 'i': + case 'I': + case 'o': + case 'O': + case 'u': + case 'U': + std::cout << letter << " is a vowel\n"; + break; + case 'y': + case 'Y': + std::cout << letter << " is sometimes a vowel\n"; + break; + case '0': + return(0); + default: + std::cout << "The character input was not a letter. Try again!\n"; + break; + } + } +} + diff --git a/08/vowel-or-consonant/vowel-or-consonant.md b/08/vowel-or-consonant/vowel-or-consonant.md new file mode 100644 index 0000000..d7d8eb7 --- /dev/null +++ b/08/vowel-or-consonant/vowel-or-consonant.md @@ -0,0 +1,17 @@ +# vowel or consonant + +> Determine whether a letter is a vowel or a consonant +> Users' Specification +> Jan 20, 2023 Bobby Hamblin + +Prints whether a letter is a vowel or a consonant to the terminal. +Enter a 0 to quit. + +## Example + +> Enter a letter: *A* +> A is a vowel +> Enter a letter: *b* +> b is a consonant +> Enter a letter: *Y* +> Y is sometimes a vowel -- 2.45.2