Please work in pairs, unless you really, really don’t want to (it’s okay). Submit in a private GitHub repository. Private repositories are free for students.
$ piano_keys A 27.5000 A# 29.1352 B 30.8677 C 32.7032 C# 34.6478 D 36.7081 D# 38.8909 . . . A# 3729.3101 B 3951.0664 C 4186.0090
Your expected output should be exactly the same as the following Python script. You can use the Python script as a guide for forming your solution in C, but notice a tiny step-by-step translation is likely a bad idea, as the two languages are very different):
KEY_NAMES = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'] STEPS = len(KEY_NAMES) NUMBER_OF_KEYS = 88; INITIAL_FREQUENCY = 27.5; for i in range(NUMBER_OF_KEYS): key_name = KEY_NAMES[i % STEPS] frequency = INITIAL_FREQUENCY * (2.0 ** (i / STEPS)) print(f'{key_name}\t{frequency:10.4f}')
$ piano_scales F# F# major: F# G# A# B C# D# E# F# minor: F# G# A B C# D E
Your expected output should be exactly the same as the following Python script. You can use the Python script as a guide for forming your solution in C, but notice a tiny step-by-step translation is likely a bad idea, as the two languages are very different. C doesn’t have exceptions, not a direct transation of Python’s index
method, just to name a couple differences (there are more):
import sys KEY_NAMES = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'] NUMBER_OF_NOTES = len(KEY_NAMES) MAJOR_STEPS = [2,2,1,2,2,2] MINOR_STEPS = [2,1,2,2,1,2] NUMBER_OF_STEPS = len(MAJOR_STEPS) def print_scale(note_index, type, steps): print(f'{KEY_NAMES[note_index]:3}{type}: {KEY_NAMES[note_index]:3}', end='') offset = 0 for step in steps: offset += step print(f'{KEY_NAMES[(note_index + offset) % NUMBER_OF_NOTES]:3}', end='') print() if __name__ == '__main__': if len(sys.argv) != 2: print("This program requires exactly one command line argument\n") sys.exit(1) try: key_index = KEY_NAMES.index(sys.argv[1].upper()) except ValueError: print(f'No such key: {sys.argv[1]}') sys.exit(2) print_scale(key_index, "major", MAJOR_STEPS) print_scale(key_index, "minor", MINOR_STEPS)
silly("phone", "booth") ⇒ "pphphophonphoneboothbootboobob" silly("", "") ⇒ "" silly("abc", "") ⇒ "aababc" silly("", "abc") ⇒ "abcaba" silly("too", "real") ⇒ "ttotoorealrearer"
"doghouse"
and 3
will return "housedog"
. More examples:
rotate("doghouse", 0) ⇒ "doghouse" rotate("doghouse", 1) ⇒ "oghoused" rotate("doghouse", 2) ⇒ "ghousedo" rotate("doghouse", 3) ⇒ "housedog" rotate("doghouse", 4) ⇒ "ousedogh" rotate("doghouse", 5) ⇒ "usedogho"
Make sure to consider inputs that are negative, zero, in the billions (or even greater).