127,379 views
40 votes
40 votes
On a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * rn, where n is the distance (number of keys) from that key, and r is 2(1/12). Given an initial key frequency, output that frequency and the next 4 higher key frequencies.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:

print('{:.2f} {:.2f} {:.2f} {:.2f} {:.2f}'.format(your_value1, your_value2, your_value3, your_value4, your_value5))

Ex: If the input is:

440
(which is the A key near the middle of a piano keyboard), the output is:

440.00 466.16 493.88 523.25 554.37

User Moshen
by
2.9k points

1 Answer

17 votes
17 votes

Answer:

In C:

#include <stdio.h>

#include <math.h>

int main(){

float f0,r,temp;

r = pow(2.0,1.0/12);

printf("f0: "); scanf("%f", &f0);

temp = f0;

for(int i = 0; i<=4;i++){

f0 = f0 * pow(r,i);

printf("%.2lf ", f0);

f0 = temp; }

return 0;

}

Step-by-step explanation:

This declares f0, r and temp as float

float f0,r,temp;

This initializes r to 2^(1/12)

r = pow(2.0,1.0/12);

This prompts the user for f0

printf("f0: "); scanf("%f", &f0);

This saves f0 in temp

temp = f0;

This iterates the number of keys from 0 to 4

for(int i = 0; i<=4;i++){

This calculates each key

f0 = f0 * pow(r,i);

This prints the key

printf("%.2lf ", f0);

This gets the initial value of f0

f0 = temp; }

return 0;

User Alexdor
by
3.0k points