221k views
5 votes
On a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * r^n, where n is the distance (number of keys) from that key, and r is 2^(1.0/12.0). 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:
printf("%0.21f", yourValue);
Ex: If the input is 440 (the A key near the middle of a piano keyboard), the output is:
440.0, 466.16, 493.88, 523.25, 554.36

User Ruwantha
by
4.8k points

1 Answer

1 vote

Answer:

#include<iostream>

#include<cmath>

using namespace std;

int main()

{

float f0;

//Prompt user for input

cout<<"Enter Initial Key Frequency: ";

cin>>f0;

//Initialize number of keys

int numkey = 1;

//Print first key frequency

printf("%0.2f", f0);

cout<<" ";

while(numkey<=4)

{

f0*= pow(2,(1.0/12.0));

printf("%0.2f", f0);

cout<<" ";

numkey++;

}

return 0;

}

Step-by-step explanation:

Line 4 declares fo (the key frequency) as float

Line 6 prompts user for input

Line 7 accepts input

Line 9 initializes number of keys to 1

Line 11 prints first key frequency (the input from the user)

Line 12 - 18 is an iteration that calculates and print key frequencies

Line 14 calculates the next 4 key frequencies

Line 15 prints the corresponding key frequency

User GSazheniuk
by
4.8k points